ExportExcel.pas 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. unit ExportExcel;
  2. interface
  3. uses
  4. Classes, ZjGrid, ScXlsOutput, ScXlsCustomUD, Windows, StdCtrls,
  5. sdIDTree, sdDB, Graphics, SysUtils, ProgressHintFrm, Forms, Controls,
  6. OExport, OExport_Vcl, OExport_VclForms, BillsPosTree, BillsTree;
  7. type
  8. TExcelExportor = class
  9. private
  10. FOExport: TOExport;
  11. FGrid: TZJGrid;
  12. FTempFile: string;
  13. FFileName: string;
  14. procedure InitialPage(AGrid: TZJGrid; ASheet: TExportWorkSheet);
  15. protected
  16. procedure BeforeExport;
  17. procedure EndExport;
  18. public
  19. constructor Create;
  20. destructor Destroy; override;
  21. procedure ExportToSheet(AGrid: TZJGrid; ASheet: TExportWorkSheet);
  22. procedure ExportToFile(AGrid: TZJGrid; const AFileName: string);
  23. end;
  24. PColInfo = ^TColInfo;
  25. TColInfo = record
  26. // 字段名
  27. FieldName: string;
  28. // 查询字段名,应用于联合几个数据库的情况,参照dataset的Lookup方式
  29. KeyField: string;
  30. LookupKeyField: string;
  31. // 查询数据库ID
  32. LookupDataSetIndex: Integer;
  33. // 列名
  34. TitleCaption: string;
  35. // 列宽
  36. Width: Integer;
  37. // 对齐方式
  38. HorTextAlign: TCellHAlignment;
  39. //VerTextAlign: TUDVTextAlign;
  40. end;
  41. PColInfos = ^TColInfos;
  42. TColInfos = array [0..30] of TColInfo;
  43. // 仿照DataSet的Lookup以及数据库的AutoUpdate, 以达到关于sdIDTree导出数据至Excel的普适性
  44. // 导出前须根据所需列信息,以及查询数据库(列信息须与查询数据库对等,否则将会报错,并不检查列与数据库是否匹配)
  45. TIDTreeExcelExportor = class
  46. private
  47. FOExport: TOExport;
  48. FDataSetList: TList;
  49. FColInfos: PColInfos;
  50. FColCount: Integer;
  51. FHasLevelCode: Boolean;
  52. FTree: TsdIDTree;
  53. FTempFile: string;
  54. // 当清单数超过3w3k行时,使用Variant会内存溢出
  55. function GetCellValue(ANode: TsdIDTreeNode; ColInfo: TColInfo): Variant;
  56. // 故换成直接使用String
  57. function GetCellStr(ANode: TsdIDTreeNode; ColInfo: TColInfo): string;
  58. procedure ExportNodeData(ANode: TsdIDTreeNode; ASheet: TExportWorkSheet; const ALevelCode: string);
  59. procedure ExportTreeNode(ANode: TsdIDTreeNode; ASheet: TExportWorkSheet; const ALevelCode: string);
  60. procedure DefineHeader(ASheet: TExportWorkSheet);
  61. protected
  62. procedure BeforeExport;
  63. procedure AfterExport;
  64. public
  65. constructor Create;
  66. destructor Destroy; override;
  67. procedure AddLookupDataSet(ADataSet: TsdDataSet);
  68. procedure DefineCol(AColInfos: PColInfos; AColCount: Integer);
  69. procedure ExportToSheet(ATree: TsdIDTree; ASheet: TExportWorkSheet);
  70. procedure ExportToFile(ATree: TsdIDTree; const AFileName: string);
  71. property HasLevelCode: Boolean read FHasLevelCode write FHasLevelCode;
  72. end;
  73. TMasterExcelExportor = class
  74. private
  75. FOExport: TOExport;
  76. FColInfos: PColInfos;
  77. FRelaColInfos: PColInfos;
  78. FColCount: Integer;
  79. FTempFile: string;
  80. FMasterDataSet: TsdDataSet;
  81. FKeyFieldName: string;
  82. FRelaDataSet: TsdDataSet;
  83. FMasterFieldName: string;
  84. function GetCellValue(ARec: TsdDataRecord; ColInfo: TColInfo): Variant;
  85. procedure ExportRecord(ARec: TsdDataRecord; ASheet: TExportWorkSheet; AColInfos: PColInfos);
  86. procedure ExportData(ASheet: TExportWorkSheet);
  87. procedure DefineHeader(ASheet: TExportWorkSheet);
  88. protected
  89. procedure BeforeExport;
  90. procedure AfterExport;
  91. public
  92. constructor Create;
  93. destructor Destroy; override;
  94. procedure DefineCol(AColInfos: PColInfos; AColCount: Integer; ARelaColInfo: PColInfos = nil);
  95. procedure DefineMasterDataSet(ADataSet: TsdDataSet; const AKeyFieldName: string);
  96. procedure DefineRelaDataSet(ADataSet: TsdDataSet; const AMasterFieldName: string);
  97. procedure ExportToSheet(ASheet: TExportWorkSheet);
  98. procedure ExportToFile(const AFileName: string);
  99. end;
  100. TBillsPosExcelExportor = class
  101. private
  102. FOExport: TOExport;
  103. FTempFile: string;
  104. FTree: TBillsPosTree;
  105. FCodeCol: Integer;
  106. FB_CodeCol: Integer;
  107. FP_CodeCol: Integer;
  108. FNameCol: Integer;
  109. FUnitsCol: Integer;
  110. FQuantityCol: Integer;
  111. FDgnQty1: Integer;
  112. FDgnQty2: Integer;
  113. FPriceCol: Integer;
  114. FTotalPriceCol: Integer;
  115. FDrawingCodeCol: Integer;
  116. FMemoStrCol: Integer;
  117. procedure DefineHeader(ASheet: TExportWorkSheet);
  118. function HasGclChild(ANode: TBillsIDTreeNode): Boolean;
  119. function FindNode(AParent: TBillsPosTreeNode; AMatch: TBillsIDTreeNode): TBillsPosTreeNode;
  120. procedure ConversePosNode(ANode: TBillsIDTreeNode; AParent: TBillsPosTreeNode = nil);
  121. function ConverseNodeData(ANode: TBillsIDTreeNode; AParent: TBillsPosTreeNode = nil): TBillsPosTreeNode;
  122. procedure ConverseTreeNode(ANode: TBillsIDTreeNode; AParent: TBillsPosTreeNode = nil);
  123. procedure ExportPos(ASheet: TExportWorkSheet; ANode: TBillsPosTreeNode);
  124. procedure ExportNode(ASheet: TExportWorkSheet; ANode: TBillsPosTreeNode);
  125. procedure ExportData(ASheet: TExportWorkSheet);
  126. protected
  127. procedure BeforeExport;
  128. procedure AfterExport;
  129. public
  130. constructor Create;
  131. destructor Destroy; override;
  132. procedure ExportToSheet(ABillsIDTree: TBillsIDTree; ASheet: TExportWorkSheet);
  133. procedure ExportToFile(ABillsIDTree: TBillsIDTree; const AFileName: string);
  134. end;
  135. const
  136. ciLedger: array [0..8] of TColInfo =(
  137. (FieldName: 'Code'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '项目节编号'; Width: 120; HorTextAlign: cahLeft),
  138. (FieldName: 'B_Code'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '清单编号'; Width: 80; HorTextAlign: cahLeft),
  139. (FieldName: 'Name'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '名称'; Width: 200; HorTextAlign: cahLeft),
  140. (FieldName: 'Units'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '单位'; Width: 40; HorTextAlign: cahCenter),
  141. (FieldName: 'Price'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '单价'; Width: 60; HorTextAlign: cahRight),
  142. (FieldName: 'OrgQuantity'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '数量'; Width: 80; HorTextAlign: cahRight),
  143. (FieldName: 'OrgTotalPrice'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '金额'; Width: 80; HorTextAlign: cahRight),
  144. (FieldName: 'DrawingCode'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '图号'; Width: 100; HorTextAlign: cahLeft),
  145. (FieldName: 'MemoStr'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '备注'; Width: 80; HorTextAlign: cahLeft)
  146. );
  147. ciLedgerWithMis: array [0..10] of TColInfo =(
  148. (FieldName: 'Code'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '项目节编号'; Width: 120; HorTextAlign: cahLeft),
  149. (FieldName: 'B_Code'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '清单编号'; Width: 80; HorTextAlign: cahLeft),
  150. (FieldName: 'Name'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '名称'; Width: 200; HorTextAlign: cahLeft),
  151. (FieldName: 'Units'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '单位'; Width: 40; HorTextAlign: cahCenter),
  152. (FieldName: 'Price'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '单价'; Width: 60; HorTextAlign: cahRight),
  153. (FieldName: 'OrgQuantity'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '施工图数量'; Width: 90; HorTextAlign: cahRight),
  154. (FieldName: 'MisQuantity'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '设计错漏数量'; Width: 90; HorTextAlign: cahRight),
  155. (FieldName: 'OthQuantity'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '其他错漏数量'; Width: 90; HorTextAlign: cahRight),
  156. (FieldName: 'TotalPrice'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '金额'; Width: 80; HorTextAlign: cahRight),
  157. (FieldName: 'DrawingCode'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '图号'; Width: 100; HorTextAlign: cahLeft),
  158. (FieldName: 'MemoStr'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '备注'; Width: 80; HorTextAlign: cahLeft)
  159. );
  160. ciFxBills: array [0..10] of TColInfo =(
  161. (FieldName: 'Code'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '预算项目节'; Width: 120; HorTextAlign: cahLeft),
  162. (FieldName: 'B_Code'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '清单子目号'; Width: 80; HorTextAlign: cahLeft),
  163. (FieldName: 'Name'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '名称'; Width: 200; HorTextAlign: cahLeft),
  164. (FieldName: 'Units'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '单位'; Width: 40; HorTextAlign: cahCenter),
  165. (FieldName: 'OrgQuantity'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '清单数量'; Width: 80; HorTextAlign: cahRight),
  166. (FieldName: 'DgnQuantity1'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '设计数量1'; Width: 80; HorTextAlign: cahRight),
  167. (FieldName: 'DgnQuantity2'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '设计数量2'; Width: 80; HorTextAlign: cahRight),
  168. (FieldName: 'Price'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '单价'; Width: 60; HorTextAlign: cahRight),
  169. (FieldName: 'OrgTotalPrice'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '合价'; Width: 80; HorTextAlign: cahRight),
  170. (FieldName: 'DrawingCode'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '图号'; Width: 100; HorTextAlign: cahLeft),
  171. (FieldName: 'MemoStr'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '备注'; Width: 80; HorTextAlign: cahLeft)
  172. );
  173. ciFxBillsWithMis: array [0..12] of TColInfo =(
  174. (FieldName: 'Code'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '预算项目节'; Width: 120; HorTextAlign: cahLeft),
  175. (FieldName: 'B_Code'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '清单子目号'; Width: 80; HorTextAlign: cahLeft),
  176. (FieldName: 'Name'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '名称'; Width: 200; HorTextAlign: cahLeft),
  177. (FieldName: 'Units'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '单位'; Width: 40; HorTextAlign: cahCenter),
  178. (FieldName: 'OrgQuantity'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '清单数量'; Width: 90; HorTextAlign: cahRight),
  179. (FieldName: 'MisQuantity'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '设计错漏数量'; Width: 90; HorTextAlign: cahRight),
  180. (FieldName: 'OthQuantity'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '其他错漏数量'; Width: 90; HorTextAlign: cahRight),
  181. (FieldName: 'DgnQuantity1'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '设计数量1'; Width: 80; HorTextAlign: cahRight),
  182. (FieldName: 'DgnQuantity2'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '设计数量2'; Width: 80; HorTextAlign: cahRight),
  183. (FieldName: 'Price'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '单价'; Width: 60; HorTextAlign: cahRight),
  184. (FieldName: 'TotalPrice'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '合价'; Width: 80; HorTextAlign: cahRight),
  185. (FieldName: 'DrawingCode'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '图号'; Width: 100; HorTextAlign: cahLeft),
  186. (FieldName: 'MemoStr'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '备注'; Width: 80; HorTextAlign: cahLeft)
  187. );
  188. ciTpPegGcl: array [0..9] of TColInfo =(
  189. (FieldName: 'Code'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '项目节编号'; Width: 120; HorTextAlign: cahLeft),
  190. (FieldName: 'B_Code'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '清单编号'; Width: 80; HorTextAlign: cahLeft),
  191. (FieldName: 'Name'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '名称'; Width: 200; HorTextAlign: cahLeft),
  192. (FieldName: 'Units'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '单位'; Width: 40; HorTextAlign: cahCenter),
  193. (FieldName: 'Price'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '单价'; Width: 60; HorTextAlign: cahRight),
  194. (FieldName: 'Quantity'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '数量'; Width: 90; HorTextAlign: cahRight),
  195. (FieldName: 'TotalPrice'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '金额'; Width: 80; HorTextAlign: cahRight),
  196. (FieldName: 'Peg'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '桩号'; Width: 80; HorTextAlign: cahLeft),
  197. (FieldName: 'DrawingCode'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '图号'; Width: 80; HorTextAlign: cahLeft),
  198. (FieldName: 'MemoStr'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '备注'; Width: 80; HorTextAlign: cahLeft)
  199. );
  200. ciTpGclPeg_Gcl: array [0..9] of TColInfo =(
  201. (FieldName: 'B_Code'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '清单编号'; Width: 80; HorTextAlign: cahLeft),
  202. (FieldName: 'Code'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '项目节编号'; Width: 120; HorTextAlign: cahLeft),
  203. (FieldName: 'Name'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '名称'; Width: 200; HorTextAlign: cahLeft),
  204. (FieldName: 'Units'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '单位'; Width: 40; HorTextAlign: cahCenter),
  205. (FieldName: 'Price'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '单价'; Width: 60; HorTextAlign: cahRight),
  206. (FieldName: 'Quantity'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '数量'; Width: 90; HorTextAlign: cahRight),
  207. (FieldName: 'TotalPrice'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '金额'; Width: 80; HorTextAlign: cahRight),
  208. (FieldName: 'Peg'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '桩号'; Width: 80; HorTextAlign: cahLeft),
  209. (FieldName: 'DrawingCode'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '图号'; Width: 80; HorTextAlign: cahLeft),
  210. (FieldName: 'MemoStr'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '备注'; Width: 80; HorTextAlign: cahLeft)
  211. );
  212. ciTpGclPeg_Peg: array [0..9] of TColInfo =(
  213. (FieldName: 'B_Code'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '清单编号'; Width: 80; HorTextAlign: cahLeft),
  214. (FieldName: 'PegXmjCode'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '项目节编号'; Width: 120; HorTextAlign: cahLeft),
  215. (FieldName: 'PegXmjName'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '名称'; Width: 200; HorTextAlign: cahLeft),
  216. (FieldName: 'PegXmjUnits'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '单位'; Width: 40; HorTextAlign: cahCenter),
  217. (FieldName: 'Price'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '单价'; Width: 60; HorTextAlign: cahRight),
  218. (FieldName: 'Quantity'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '数量'; Width: 90; HorTextAlign: cahRight),
  219. (FieldName: 'TotalPrice'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '金额'; Width: 80; HorTextAlign: cahRight),
  220. (FieldName: 'Peg'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '桩号'; Width: 80; HorTextAlign: cahLeft),
  221. (FieldName: 'DrawingCode'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '图号'; Width: 80; HorTextAlign: cahLeft),
  222. (FieldName: 'MemoStr'; KeyField: ''; LookupKeyField: ''; LookupDataSetIndex: -1; TitleCaption: '备注'; Width: 80; HorTextAlign: cahLeft)
  223. );
  224. implementation
  225. uses
  226. ZhAPI, Variants, UtilMethods, Math, CacheTree, mDataRecord;
  227. function GetExportor(const AFileType: string): TOCustomExporter;
  228. begin
  229. if SameText(AFileType, '.xls') then
  230. Result := TOCustomExporterXLS.Create
  231. else if SameText(AFileType, '.xlsx') then
  232. Result := TOCustomExporterXLSX.Create;
  233. end;
  234. { TExcelExportor }
  235. procedure TExcelExportor.BeforeExport;
  236. begin
  237. Screen.Cursor := crHourGlass;
  238. ShowProgressHint('导出Excel表格数据', FGrid.RowCount);
  239. end;
  240. constructor TExcelExportor.Create;
  241. begin
  242. FOExport := TOExport.Create;
  243. FOExport.UseProgress := False;
  244. FTempFile := GetTempFileName;
  245. end;
  246. destructor TExcelExportor.Destroy;
  247. begin
  248. if FileExists(FTempFile) then
  249. DeleteFileOrFolder(FTempFile);
  250. FOExport.Free;
  251. inherited;
  252. end;
  253. procedure TExcelExportor.EndExport;
  254. begin
  255. CloseProgressHint;
  256. Screen.Cursor := crDefault;
  257. end;
  258. procedure TExcelExportor.ExportToFile(AGrid: TZJGrid;
  259. const AFileName: string);
  260. var
  261. vExportor: TOCustomExporter;
  262. begin
  263. FFileName := AFileName;
  264. FGrid := AGrid;
  265. BeforeExport;
  266. try
  267. vExportor := GetExportor(ExtractFileExt(AFileName));
  268. ExportToSheet(AGrid, FOExport.AddWorkSheet);
  269. FOExport.SaveToFile(FTempFile, vExportor);
  270. if not FileExists(FFileName) or QuestMessage('存在同名文件,是否替换?') then
  271. CopyFileOrFolder(FTempFile, FFileName);
  272. finally
  273. vExportor.Free;
  274. EndExport;
  275. end;
  276. end;
  277. procedure TExcelExportor.ExportToSheet(AGrid: TZJGrid;
  278. ASheet: TExportWorkSheet);
  279. procedure SetXlsCellTextAlign(ACell: TExportCell; AGridCell: TzjCell);
  280. begin
  281. case AGridCell.TextAlign of
  282. gaTopLeft:
  283. begin
  284. ACell.SetVAlignment(cavTop);
  285. ACell.SetAlignment(cahLeft);
  286. end;
  287. gaTopCenter:
  288. begin
  289. ACell.SetVAlignment(cavTop);
  290. ACell.SetAlignment(cahCenter);
  291. end;
  292. gaTopRight:
  293. begin
  294. ACell.SetVAlignment(cavTop);
  295. ACell.SetAlignment(cahRight);
  296. end;
  297. gaCenterLeft:
  298. begin
  299. ACell.SetVAlignment(cavCenter);
  300. ACell.SetAlignment(cahLeft);
  301. end;
  302. gaCenterCenter:
  303. begin
  304. ACell.SetVAlignment(cavCenter);
  305. ACell.SetAlignment(cahCenter);
  306. end;
  307. gaCenterRight:
  308. begin
  309. ACell.SetVAlignment(cavCenter);
  310. ACell.SetAlignment(cahRight);
  311. end;
  312. gaBottomLeft:
  313. begin
  314. ACell.SetVAlignment(cavBottom);
  315. ACell.SetAlignment(cahLeft);
  316. end;
  317. gaBottomCenter:
  318. begin
  319. ACell.SetVAlignment(cavBottom);
  320. ACell.SetAlignment(cahCenter);
  321. end;
  322. gaBottomRight:
  323. begin
  324. ACell.SetVAlignment(cavBottom);
  325. ACell.SetAlignment(cahRight);
  326. end;
  327. end;
  328. if goWarpText in AGridCell.Grid.Options then
  329. ACell.WrapText := True;
  330. end;
  331. procedure ExportGridCell(AGridCell: TzjCell; ARow: TExportRow);
  332. var
  333. vCell: TExportCell;
  334. XlsCell: TXlsCustomCell;
  335. begin
  336. if (AGridCell = nil) then Exit;
  337. if ARow.Cells.Count >= AGridCell.Col + 1 then
  338. vCell := ARow.Cells[AGridCell.Col]
  339. else
  340. vCell := ARow.AddCellString(AGridCell.Text);
  341. SetXlsCellTextAlign(vCell, AGridCell);
  342. vCell.Font.Name := AGridCell.Font.Name;
  343. vCell.Font.Size := AGridCell.Font.Size;
  344. vCell.RowSpan := AGridCell.Height;
  345. vCell.ColSpan := AGridCell.Width;
  346. vCell.Width := FGrid.ColWidths[AGridCell.Col];
  347. vCell.Height := FGrid.RowHeights[AGridCell.Row];
  348. end;
  349. var
  350. iColumn, iRow: Integer;
  351. vRow: TExportRow;
  352. begin
  353. for iRow := 0 to AGrid.RowCount - 1 do
  354. begin
  355. UpdateProgressHint(Format('导出第%d行数据', [iRow + 1]));
  356. UpdateProgressHint(1);
  357. vRow := ASheet.AddRow;
  358. for iColumn := 0 to AGrid.ColCount - 1 do
  359. ExportGridCell(AGrid.Cells[iColumn, iRow], vRow);
  360. end;
  361. end;
  362. procedure TExcelExportor.InitialPage(AGrid: TZJGrid;
  363. ASheet: TExportWorkSheet);
  364. procedure InitialColumnWidth;
  365. var
  366. iColumn: Integer;
  367. begin
  368. for iColumn := 0 to AGrid.ColCount - 1 do
  369. ASheet.Cols[iColumn].SetWidth(AGrid.ColWidths[iColumn]);
  370. end;
  371. procedure InitialRowHeight;
  372. var
  373. iRow: Integer;
  374. begin
  375. for iRow := 0 to AGrid.RowCount - 1 do
  376. ASheet.Rows[iRow].SetHeight(AGrid.RowHeights[iRow]);
  377. end;
  378. begin
  379. InitialColumnWidth;
  380. InitialRowHeight;
  381. end;
  382. { TIDTreeExcelExportor }
  383. constructor TIDTreeExcelExportor.Create;
  384. begin
  385. FOExport := TOExport.Create;
  386. FDataSetList := TList.Create;
  387. FTempFile := GetTempFileName;
  388. end;
  389. destructor TIDTreeExcelExportor.Destroy;
  390. begin
  391. if FileExists(FTempFile) then
  392. DeleteFileOrFolder(FTempFile);
  393. FDataSetList.Free;
  394. FOExport.Free;
  395. inherited;
  396. end;
  397. procedure TIDTreeExcelExportor.ExportToFile(ATree: TsdIDTree;
  398. const AFileName: string);
  399. var
  400. vExportor: TOCustomExporter;
  401. begin
  402. FTree := ATree;
  403. BeforeExport;
  404. try
  405. vExportor := GetExportor(ExtractFileExt(AFileName));
  406. ExportToSheet(ATree, FOExport.AddWorkSheet);
  407. UpdateProgressHint('保存0号台账Excel数据');
  408. FOExport.SaveToFile(FTempFile, vExportor);
  409. if not FileExists(AFileName) or QuestMessage('存在同名文件,是否替换?') then
  410. CopyFileOrFolder(FTempFile, AFileName);
  411. finally
  412. vExportor.Free;
  413. AfterExport;
  414. end;
  415. end;
  416. procedure TIDTreeExcelExportor.ExportTreeNode(ANode: TsdIDTreeNode;
  417. ASheet: TExportWorkSheet; const ALevelCode: string);
  418. function GetFirstChildLevelCode(const ACode: string): string;
  419. begin
  420. Result := ACode + '.1';
  421. end;
  422. function GetNextSiblingLevelCode(const ACode: string): string;
  423. var
  424. strPreCode, strLastCode: string;
  425. iNextCode: Integer;
  426. begin
  427. if Pos('.', ACode) = 0 then
  428. Result := IntToStr(StrToIntDef(ACode, 1) + 1)
  429. else
  430. begin
  431. strPreCode := GetPrefixOfCode(ACode, '.');
  432. strLastCode := GetLastSetmentOfCode(ACode, '.');
  433. iNextCode := StrToIntDef(strLastCode, 1) + 1;
  434. Result := strPreCode + '.' + IntToStr(iNextCode);
  435. end;
  436. end;
  437. var
  438. sHint: string;
  439. begin
  440. if not Assigned(ANode) then Exit;
  441. if ANode.Rec.ValueByName('Code').AsString <> '' then
  442. sHint := '正在导出清单 ' + ANode.Rec.ValueByName('Code').AsString
  443. else if ANode.Rec.ValueByName('B_Code').AsString <> '' then
  444. sHint := '正在导出清单 ' + ANode.Rec.ValueByName('B_Code').AsString
  445. else
  446. sHint := '正在导出清单 ' + ANode.Rec.ValueByName('Name').AsString;
  447. UpdateProgressHint(sHint);
  448. UpdateProgressHint(1);
  449. ExportNodeData(ANode, ASheet, ALevelCode);
  450. ExportTreeNode(ANode.FirstChild, ASheet, GetFirstChildLevelCode(ALevelCode));
  451. ExportTreeNode(ANode.NextSibling, ASheet, GetNextSiblingLevelCode(ALevelCode));
  452. end;
  453. procedure TIDTreeExcelExportor.ExportNodeData(ANode: TsdIDTreeNode;
  454. ASheet: TExportWorkSheet; const ALevelCode: string);
  455. var
  456. iCol: Integer;
  457. ColInfo: TColInfo;
  458. vRow: TExportRow;
  459. vCell: TExportCell;
  460. XlsCell: TXlsCustomCell;
  461. sStr: string;
  462. begin
  463. if not Assigned(ANode) then Exit;
  464. vRow := ASheet.AddRow;
  465. vRow.Height := 20;
  466. for iCol := 0 to FColCount - 1 do
  467. begin
  468. ColInfo := FColInfos[iCol];
  469. vCell := vRow.AddCellString(GetCellStr(ANode, ColInfo));
  470. vCell.Alignment := ColInfo.HorTextAlign;
  471. vCell.Font.Name := 'SmartSimSun';
  472. vCell.Font.Size := 9;
  473. end;
  474. if HasLevelCode then
  475. begin
  476. vCell := vRow.AddCellString(ALevelCode);
  477. vCell.Font.Name := 'SmartSimSun';
  478. vCell.Font.Size := 9;
  479. end;
  480. end;
  481. procedure TIDTreeExcelExportor.ExportToSheet(ATree: TsdIDTree;
  482. ASheet: TExportWorkSheet);
  483. begin
  484. DefineHeader(ASheet);
  485. ExportTreeNode(ATree.FirstNode, ASheet, '1');
  486. end;
  487. function TIDTreeExcelExportor.GetCellValue(ANode: TsdIDTreeNode;
  488. ColInfo: TColInfo): Variant;
  489. function GetRec: TsdDataRecord;
  490. var
  491. DataSet: TsdDataSet;
  492. begin
  493. Result := nil;
  494. DataSet := TsdDataSet(FDataSetList.Items[ColInfo.LookupDataSetIndex]);
  495. if not Assigned(DataSet) then Exit;
  496. Result := DataSet.Locate(ColInfo.LookupKeyField, ANode.Rec.ValueByName(ColInfo.KeyField).Value);
  497. end;
  498. var
  499. ARec: TsdDataRecord;
  500. begin
  501. Result := '';
  502. if not Assigned(ANode) then Exit;
  503. if ColInfo.LookupDataSetIndex = -1 then
  504. ARec := ANode.Rec
  505. else
  506. ARec := GetRec;
  507. if Assigned(ARec) then
  508. Result := ARec.ValueByName(ColInfo.FieldName).Value;
  509. end;
  510. procedure TIDTreeExcelExportor.DefineHeader(ASheet: TExportWorkSheet);
  511. var
  512. iCol: Integer;
  513. ColInfo: TColInfo;
  514. vRow: TExportRow;
  515. vCell: TExportCell;
  516. XlsCell: TXlsCustomCell;
  517. begin
  518. vRow := ASheet.AddRow;
  519. vRow.Height := 20;
  520. for iCol := 0 to FColCount - 1 do
  521. begin
  522. ColInfo := FColInfos[iCol];
  523. vCell := vRow.AddCellString(ColInfo.TitleCaption);
  524. vCell.SetAlignment(cahCenter);
  525. vCell.SetVAlignment(cavCenter);
  526. vCell.Font.Name := '黑体';
  527. vCell.Font.Size := 10;
  528. vCell.Width := ColInfo.Width;
  529. end;
  530. if HasLevelCode then
  531. begin
  532. vCell := vRow.AddCellString('层次编号');
  533. vCell.SetAlignment(cahCenter);
  534. vCell.SetVAlignment(cavCenter);
  535. vCell.Font.Name := '黑体';
  536. vCell.Font.Size := 10;
  537. end;
  538. end;
  539. procedure TIDTreeExcelExportor.AddLookupDataSet(ADataSet: TsdDataSet);
  540. begin
  541. FDataSetList.Add(ADataSet);
  542. end;
  543. procedure TIDTreeExcelExportor.DefineCol(AColInfos: PColInfos;
  544. AColCount: Integer);
  545. begin
  546. FColInfos := AColInfos;
  547. FColCount := AColCount;
  548. end;
  549. procedure TIDTreeExcelExportor.BeforeExport;
  550. begin
  551. Screen.Cursor := crHourGlass;
  552. ShowProgressHint('导出0号台账Excel数据', FTree.Count);
  553. end;
  554. procedure TIDTreeExcelExportor.AfterExport;
  555. begin
  556. CloseProgressHint;
  557. Screen.Cursor := crDefault;
  558. end;
  559. function TIDTreeExcelExportor.GetCellStr(ANode: TsdIDTreeNode;
  560. ColInfo: TColInfo): string;
  561. function GetRec: TsdDataRecord;
  562. var
  563. DataSet: TsdDataSet;
  564. begin
  565. Result := nil;
  566. DataSet := TsdDataSet(FDataSetList.Items[ColInfo.LookupDataSetIndex]);
  567. if not Assigned(DataSet) then Exit;
  568. Result := DataSet.Locate(ColInfo.LookupKeyField, ANode.Rec.ValueByName(ColInfo.KeyField).Value);
  569. end;
  570. var
  571. ARec: TsdDataRecord;
  572. begin
  573. Result := '';
  574. if not Assigned(ANode) then Exit;
  575. if ColInfo.LookupDataSetIndex = -1 then
  576. ARec := ANode.Rec
  577. else
  578. ARec := GetRec;
  579. if Assigned(ARec) then
  580. Result := ARec.ValueByName(ColInfo.FieldName).AsString;
  581. if SameText(Result, '0') then
  582. Result := '';
  583. end;
  584. { TMasterExcelExportor }
  585. procedure TMasterExcelExportor.BeforeExport;
  586. begin
  587. Screen.Cursor := crHourGlass;
  588. end;
  589. constructor TMasterExcelExportor.Create;
  590. begin
  591. FOExport := TOExport.Create;
  592. FTempFile := GetTempFileName;
  593. end;
  594. procedure TMasterExcelExportor.DefineCol(AColInfos: PColInfos;
  595. AColCount: Integer; ARelaColInfo: PColInfos);
  596. begin
  597. FColInfos := AColInfos;
  598. FColCount := AColCount;
  599. FRelaColInfos := ARelaColInfo;
  600. end;
  601. procedure TMasterExcelExportor.DefineHeader(ASheet: TExportWorkSheet);
  602. var
  603. iCol: Integer;
  604. ColInfo: TColInfo;
  605. vRow: TExportRow;
  606. vCell: TExportCell;
  607. begin
  608. vRow := ASheet.AddRow;
  609. vRow.Height := 20;
  610. for iCol := 0 to FColCount - 1 do
  611. begin
  612. ColInfo := FColInfos[iCol];
  613. vCell := vRow.AddCellString(ColInfo.TitleCaption);
  614. vCell.SetAlignment(cahCenter);
  615. vCell.SetVAlignment(cavCenter);
  616. vCell.Font.Name := '黑体';
  617. vCell.Font.Size := 10;
  618. vCell.Width := ColInfo.Width;
  619. end;
  620. end;
  621. procedure TMasterExcelExportor.DefineMasterDataSet(ADataSet: TsdDataSet;
  622. const AKeyFieldName: string);
  623. begin
  624. FMasterDataSet := ADataSet;
  625. FKeyFieldName := AKeyFieldName;
  626. end;
  627. procedure TMasterExcelExportor.DefineRelaDataSet(ADataSet: TsdDataSet;
  628. const AMasterFieldName: string);
  629. begin
  630. FRelaDataSet := ADataSet;
  631. FMasterFieldName := AMasterFieldName;
  632. end;
  633. destructor TMasterExcelExportor.Destroy;
  634. begin
  635. if FileExists(FTempFile) then
  636. DeleteFileOrFolder(FTempFile);
  637. FOExport.Free;
  638. inherited;
  639. end;
  640. procedure TMasterExcelExportor.AfterExport;
  641. begin
  642. Screen.Cursor := crDefault;
  643. end;
  644. procedure TMasterExcelExportor.ExportData(ASheet: TExportWorkSheet);
  645. var
  646. i, j: Integer;
  647. Rec, RelaRec: TsdDataRecord;
  648. begin
  649. for i := 0 to FMasterDataSet.RecordCount - 1 do
  650. begin
  651. Rec := FMasterDataSet.Records[i];
  652. ExportRecord(Rec, ASheet, FColInfos);
  653. for j := 0 to FRelaDataSet.RecordCount - 1 do
  654. begin
  655. RelaRec := FRelaDataSet.Records[j];
  656. if (RelaRec.ValueByName(FMasterFieldName).Value = Rec.ValueByName(FKeyFieldName).Value) then
  657. begin
  658. if Assigned(FRelaColInfos) then
  659. ExportRecord(RelaRec, ASheet, FRelaColInfos)
  660. else
  661. ExportRecord(RelaRec, ASheet, FColInfos);
  662. end;
  663. end;
  664. end;
  665. end;
  666. procedure TMasterExcelExportor.ExportRecord(ARec: TsdDataRecord;
  667. ASheet: TExportWorkSheet; AColInfos: PColInfos);
  668. var
  669. iCol: Integer;
  670. ColInfo: TColInfo;
  671. vRow: TExportRow;
  672. vCell: TExportCell;
  673. sStr: string;
  674. begin
  675. if not Assigned(ARec) then Exit;
  676. vRow := ASheet.AddRow;
  677. for iCol := 0 to FColCount - 1 do
  678. begin
  679. ColInfo := AColInfos[iCol];
  680. vCell := vRow.AddCellVariant(GetCellValue(ARec, ColInfo));
  681. vCell.Font.Name := 'SmartSimSun';
  682. vCell.Font.Size := 9;
  683. end;
  684. end;
  685. procedure TMasterExcelExportor.ExportToFile(const AFileName: string);
  686. var
  687. vExportor: TOCustomExporter;
  688. begin
  689. if not Assigned(FMasterDataSet) then Exit;
  690. BeforeExport;
  691. try
  692. vExportor := GetExportor(ExtractFileExt(AFileName));
  693. ExportToSheet(FOExport.AddWorkSheet);
  694. FOExport.SaveToFile(FTempFile, vExportor);
  695. if not FileExists(AFileName) or QuestMessage('存在同名文件,是否替换?') then
  696. CopyFile(PChar(FTempFile), PChar(AFileName), False);
  697. finally
  698. vExportor.Free;
  699. AfterExport;
  700. end;
  701. end;
  702. procedure TMasterExcelExportor.ExportToSheet(ASheet: TExportWorkSheet);
  703. begin
  704. if not Assigned(FMasterDataSet) then Exit;
  705. DefineHeader(ASheet);
  706. ExportData(ASheet);
  707. end;
  708. function TMasterExcelExportor.GetCellValue(ARec: TsdDataRecord;
  709. ColInfo: TColInfo): Variant;
  710. var
  711. Value: TsdValue;
  712. begin
  713. Result := '';
  714. if Assigned(ARec) then
  715. begin
  716. Value := ARec.ValueByName(ColInfo.FieldName);
  717. if Assigned(Value) then
  718. Result := Value.AsVariant;
  719. end;
  720. end;
  721. { TBillsPosExcelExportor }
  722. procedure TBillsPosExcelExportor.AfterExport;
  723. begin
  724. Screen.Cursor := crDefault;
  725. end;
  726. procedure TBillsPosExcelExportor.BeforeExport;
  727. begin
  728. Screen.Cursor := crHourGlass;
  729. end;
  730. procedure TBillsPosExcelExportor.ConversePosNode(ANode: TBillsIDTreeNode;
  731. AParent: TBillsPosTreeNode);
  732. var
  733. i: Integer;
  734. vChild: TBillsIDTreeNode;
  735. vCur: TBillsPosTreeNode;
  736. vPos: TPosNode;
  737. begin
  738. if ANode.ChildCount = 0 then Exit;
  739. for i := 0 to ANode.ChildCount - 1 do
  740. begin
  741. vChild := TBillsIDTreeNode(ANode.ChildNodes[i]);
  742. if (vChild.Rec.B_Code.AsString <> '') then
  743. begin
  744. vCur := FindNode(AParent, vChild);
  745. if not Assigned(vCur) then
  746. vCur := ConverseNodeData(vChild, AParent);
  747. vPos := vCur.AddPos;
  748. vPos.Name := ANode.Rec.Name.AsString;
  749. vPos.Quantity := vChild.Rec.Quantity.AsFloat;
  750. vPos.MemoStr := ANode.Rec.MemoStr.AsString;
  751. vPos.DrawingCode := ANode.Rec.DrawingCode.AsString;
  752. end;
  753. end;
  754. end;
  755. procedure TBillsPosExcelExportor.ConverseTreeNode(
  756. ANode: TBillsIDTreeNode; AParent: TBillsPosTreeNode);
  757. var
  758. vCur: TBillsPosTreeNode;
  759. begin
  760. if not Assigned(ANode) then Exit;
  761. if (ANode.Rec.B_Code.AsString = '') then
  762. begin
  763. if (Pos('1-1-', ANode.Rec.Code.AsString) = 1) or (not HasGclChild(ANode)) then
  764. begin
  765. vCur := ConverseNodeData(ANode, AParent);
  766. ConverseTreeNode(TBillsIDTreeNode(ANode.FirstChild), vCur);
  767. end
  768. else
  769. ConversePosNode(ANode, AParent);
  770. end
  771. else if (Assigned(ANode.Parent) And (Pos('1-1-', TBillsIDTreeNode(ANode.Parent).Rec.Code.AsString) = 1)) then
  772. begin
  773. vCur := ConverseNodeData(ANode, AParent);
  774. vCur.Quantity := ANode.Rec.Quantity.AsFloat;
  775. ConverseTreeNode(TBillsIDTreeNode(ANode.FirstChild), vCur);
  776. end;
  777. ConverseTreeNode(TBillsIDTreeNode(ANode.NextSibling), AParent);
  778. end;
  779. function TBillsPosExcelExportor.ConverseNodeData(ANode: TBillsIDTreeNode;
  780. AParent: TBillsPosTreeNode): TBillsPosTreeNode;
  781. begin
  782. Result := TBillsPosTreeNode(FTree.AddNode(AParent));
  783. Result.Code := ANode.Rec.Code.AsString;
  784. Result.B_Code := ANode.Rec.B_Code.AsString;
  785. Result.Name := ANode.Rec.Name.AsString;
  786. Result.Units := ANode.Rec.Units.AsString;
  787. Result.DrawingCode := ANode.Rec.DrawingCode.AsString;
  788. Result.MemoStr := ANode.Rec.MemoStr.AsString;
  789. Result.DgnQty1 := ANode.Rec.DgnQuantity1.AsFloat;
  790. Result.DgnQty2 := ANode.Rec.DgnQuantity2.AsFloat;
  791. Result.Price := ANode.Rec.Price.AsFloat;
  792. Result.TotalPrice := ANode.Rec.TotalPrice.AsFloat;
  793. end;
  794. constructor TBillsPosExcelExportor.Create;
  795. begin
  796. FOExport := TOExport.Create;
  797. FTempFile := GetTempFileName;
  798. FCodeCol := 0;
  799. FB_CodeCol := 1;
  800. FP_COdeCol := 2;
  801. FNameCol := 3;
  802. FUnitsCol := 4;
  803. FQuantityCol := 5;
  804. FDgnQty1 := 6;
  805. FDgnQty2 := 7;
  806. FPriceCol := 8;
  807. FTotalPriceCol := 9;
  808. FDrawingCodeCol := 10;
  809. FMemoStrCol := 11;
  810. end;
  811. procedure TBillsPosExcelExportor.DefineHeader(ASheet: TExportWorkSheet);
  812. procedure AddHeaderCell(ARow: TExportRow; iCol: Integer; const AText: String; AWidth: Integer);
  813. var
  814. vCell: TExportCell;
  815. begin
  816. vCell := ARow.AddCellString(AText);
  817. vCell.SetAlignment(cahCenter);
  818. vCell.SetVAlignment(cavCenter);
  819. vCell.Font.Name := '黑体';
  820. vCell.Font.Size := 10;
  821. vCell.Width := AWidth;
  822. end;
  823. var
  824. iCol: Integer;
  825. ColInfo: TColInfo;
  826. vRow: TExportRow;
  827. vCell: TExportCell;
  828. begin
  829. vRow := ASheet.AddRow;
  830. vRow.Height := 20;
  831. AddHeaderCell(vRow, FCodeCol, '项目节编号', 100);
  832. AddHeaderCell(vRow, FB_CodeCol, '清单编号', 80);
  833. AddHeaderCell(vRow, FP_CodeCol, '计量单元', 80);
  834. AddHeaderCell(vRow, FNameCol, '名称', 300);
  835. AddHeaderCell(vRow, FUnitsCol, '单位', 60);
  836. AddHeaderCell(vRow, FQuantityCol, '清单数量', 70);
  837. AddHeaderCell(vRow, FDgnQty1, '设计数量1', 70);
  838. AddHeaderCell(vRow, FDgnQty2, '设计数量2', 70);
  839. AddHeaderCell(vRow, FPriceCol, '单价', 70);
  840. AddHeaderCell(vRow, FTotalPriceCol, '合价', 70);
  841. AddHeaderCell(vRow, FDrawingCodeCol, '图号', 85);
  842. AddHeaderCell(vRow, FMemoStrCol, '备注', 100);
  843. end;
  844. destructor TBillsPosExcelExportor.Destroy;
  845. begin
  846. if FileExists(FTempFile) then
  847. DeleteFileOrFolder(FTempFile);
  848. FOExport.Free;
  849. inherited;
  850. end;
  851. procedure TBillsPosExcelExportor.ExportData(ASheet: TExportWorkSheet);
  852. begin
  853. ExportNode(ASheet, TBillsPosTreeNode(FTree.FirstNode));
  854. end;
  855. procedure TBillsPosExcelExportor.ExportNode(ASheet: TExportWorkSheet;
  856. ANode: TBillsPosTreeNode);
  857. var
  858. vRow: TExportRow;
  859. vCell: TExportCellNumber;
  860. begin
  861. if not Assigned(ANode) then Exit;
  862. vRow := ASheet.AddRow;
  863. vRow.AddCellString(ANode.Code);
  864. vRow.AddCellString(ANode.B_Code);
  865. vRow.AddCellString('');
  866. vRow.AddCellString(ANode.Name);
  867. vRow.AddCellString(ANode.Units);
  868. vCell := vRow.AddCellNumber(ANode.Quantity);
  869. vCell.EmptyIfZero := True;
  870. vCell := vRow.AddCellNumber(ANode.DgnQty1);
  871. vCell.EmptyIfZero := True;
  872. vCell := vRow.AddCellNumber(ANode.DgnQty2);
  873. vCell.EmptyIfZero := True;
  874. vCell := vRow.AddCellNumber(ANode.Price);
  875. vCell.EmptyIfZero := True;
  876. vCell := vRow.AddCellNumber(ANode.TotalPrice);
  877. vCell.EmptyIfZero := True;
  878. vRow.AddCellString(ANode.DrawingCode);
  879. vRow.AddCellString(ANode.MemoStr);
  880. ExportPos(ASheet, ANode);
  881. ExportNode(ASheet, TBillsPosTreeNode(ANode.FirstChild));
  882. ExportNode(ASheet, TBillsPosTreeNode(ANode.NextSibling));
  883. end;
  884. procedure TBillsPosExcelExportor.ExportPos(ASheet: TExportWorkSheet;
  885. ANode: TBillsPosTreeNode);
  886. var
  887. i: Integer;
  888. vPos: TPosNode;
  889. vRow: TExportRow;
  890. vCell: TExportCellNumber;
  891. begin
  892. if not Assigned(ANode) then Exit;
  893. for i := 0 to ANode.PosCount - 1 do
  894. begin
  895. vPos := ANode.Pos[i];
  896. vRow := ASheet.AddRow;
  897. vRow.AddCellString();
  898. vRow.AddCellString();
  899. vRow.AddCellString(IntToStr(i+1));
  900. vRow.AddCellString(vPos.Name);
  901. vRow.AddCellString();
  902. vCell := vRow.AddCellNumber(vPos.Quantity);
  903. vCell.EmptyIfZero := True;
  904. vCell := vRow.AddCellNumber();
  905. vCell.EmptyIfZero := True;
  906. vCell := vRow.AddCellNumber();
  907. vCell.EmptyIfZero := True;
  908. vCell := vRow.AddCellNumber();
  909. vCell.EmptyIfZero := True;
  910. vCell := vRow.AddCellNumber();
  911. vCell.EmptyIfZero := True;
  912. vRow.AddCellString(vPos.DrawingCode);
  913. vRow.AddCellString(vPos.MemoStr);
  914. end;
  915. end;
  916. procedure TBillsPosExcelExportor.ExportToFile(ABillsIDTree: TBillsIDTree; const AFileName: string);
  917. var
  918. vExportor: TOCustomExporter;
  919. begin
  920. BeforeExport;
  921. try
  922. vExportor := GetExportor(ExtractFileExt(AFileName));
  923. ExportToSheet(ABillsIDTree, FOExport.AddWorkSheet);
  924. FOExport.SaveToFile(FTempFile, vExportor);
  925. if not FileExists(AFileName) or QuestMessage('存在同名文件,是否替换?') then
  926. CopyFile(PChar(FTempFile), PChar(AFileName), False);
  927. finally
  928. vExportor.Free;
  929. AfterExport;
  930. end;
  931. end;
  932. procedure TBillsPosExcelExportor.ExportToSheet(ABillsIDTree: TBillsIDTree; ASheet: TExportWorkSheet);
  933. begin
  934. DefineHeader(ASheet);
  935. FTree := TBillsPosTree.Create;
  936. try
  937. ConverseTreeNode(TBillsIDTreeNode(ABillsIDTree.FirstNode));
  938. FTree.CalculateAll;
  939. ExportData(ASheet);
  940. finally
  941. FTree.Free;
  942. end;
  943. end;
  944. function TBillsPosExcelExportor.HasGclChild(
  945. ANode: TBillsIDTreeNode): Boolean;
  946. var
  947. i: Integer;
  948. vChild: TBillsIDTreeNode;
  949. begin
  950. Result := False;
  951. for i := 0 to ANode.ChildCount - 1 do
  952. begin
  953. vChild := TBillsIDTreeNode(ANode.ChildNodes[i]);
  954. if (vChild.Rec.B_Code.AsString <> '') then
  955. begin
  956. Result := True;
  957. Exit;
  958. end;
  959. end;
  960. end;
  961. function TBillsPosExcelExportor.FindNode(AParent: TBillsPosTreeNode;
  962. AMatch: TBillsIDTreeNode): TBillsPosTreeNode;
  963. var
  964. i: Integer;
  965. vChild: TBillsPosTreeNode;
  966. begin
  967. Result := nil;
  968. if not Assigned(AParent) then Exit;
  969. for i := 0 to AParent.Children.Count - 1 do
  970. begin
  971. vChild := TBillsPosTreeNode(AParent.Children.Items[i]);
  972. if ((vChild.B_Code = AMatch.Rec.B_Code.AsString) And
  973. (vChild.Name = AMatch.Rec.Name.AsString) And
  974. (vChild.Units = AMatch.Rec.Units.AsString) And
  975. (PriceRoundTo(vChild.Price - AMatch.Rec.Price.AsFloat) = 0)) then
  976. begin
  977. Result := vChild;
  978. Break;
  979. end;
  980. end;
  981. end;
  982. end.