ExportExcel.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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. end;
  752. end;
  753. end;
  754. procedure TBillsPosExcelExportor.ConverseTreeNode(
  755. ANode: TBillsIDTreeNode; AParent: TBillsPosTreeNode);
  756. var
  757. vCur: TBillsPosTreeNode;
  758. begin
  759. if not Assigned(ANode) then Exit;
  760. if (ANode.Rec.B_Code.AsString = '') then
  761. begin
  762. if (Pos('1-1-', ANode.Rec.Code.AsString) = 1) or (not HasGclChild(ANode)) then
  763. begin
  764. vCur := ConverseNodeData(ANode, AParent);
  765. ConverseTreeNode(TBillsIDTreeNode(ANode.FirstChild), vCur);
  766. end
  767. else
  768. ConversePosNode(ANode, AParent);
  769. end
  770. else if (Assigned(ANode.Parent) And (Pos('1-1-', TBillsIDTreeNode(ANode.Parent).Rec.Code.AsString) = 1)) then
  771. begin
  772. vCur := ConverseNodeData(ANode, AParent);
  773. vCur.Quantity := ANode.Rec.Quantity.AsFloat;
  774. ConverseTreeNode(TBillsIDTreeNode(ANode.FirstChild), vCur);
  775. end;
  776. ConverseTreeNode(TBillsIDTreeNode(ANode.NextSibling), AParent);
  777. end;
  778. function TBillsPosExcelExportor.ConverseNodeData(ANode: TBillsIDTreeNode;
  779. AParent: TBillsPosTreeNode): TBillsPosTreeNode;
  780. begin
  781. Result := TBillsPosTreeNode(FTree.AddNode(AParent));
  782. Result.Code := ANode.Rec.Code.AsString;
  783. Result.B_Code := ANode.Rec.B_Code.AsString;
  784. Result.Name := ANode.Rec.Name.AsString;
  785. Result.Units := ANode.Rec.Units.AsString;
  786. Result.DrawingCode := ANode.Rec.DrawingCode.AsString;
  787. Result.MemoStr := ANode.Rec.MemoStr.AsString;
  788. Result.DgnQty1 := ANode.Rec.DgnQuantity1.AsFloat;
  789. Result.DgnQty2 := ANode.Rec.DgnQuantity2.AsFloat;
  790. Result.Price := ANode.Rec.Price.AsFloat;
  791. Result.TotalPrice := ANode.Rec.TotalPrice.AsFloat;
  792. end;
  793. constructor TBillsPosExcelExportor.Create;
  794. begin
  795. FOExport := TOExport.Create;
  796. FTempFile := GetTempFileName;
  797. FCodeCol := 0;
  798. FB_CodeCol := 1;
  799. FP_COdeCol := 2;
  800. FNameCol := 3;
  801. FUnitsCol := 4;
  802. FQuantityCol := 5;
  803. FDgnQty1 := 6;
  804. FDgnQty2 := 7;
  805. FPriceCol := 8;
  806. FTotalPriceCol := 9;
  807. FDrawingCodeCol := 10;
  808. FMemoStrCol := 11;
  809. end;
  810. procedure TBillsPosExcelExportor.DefineHeader(ASheet: TExportWorkSheet);
  811. procedure AddHeaderCell(ARow: TExportRow; iCol: Integer; const AText: String; AWidth: Integer);
  812. var
  813. vCell: TExportCell;
  814. begin
  815. vCell := ARow.AddCellString(AText);
  816. vCell.SetAlignment(cahCenter);
  817. vCell.SetVAlignment(cavCenter);
  818. vCell.Font.Name := '黑体';
  819. vCell.Font.Size := 10;
  820. vCell.Width := AWidth;
  821. end;
  822. var
  823. iCol: Integer;
  824. ColInfo: TColInfo;
  825. vRow: TExportRow;
  826. vCell: TExportCell;
  827. begin
  828. vRow := ASheet.AddRow;
  829. vRow.Height := 20;
  830. AddHeaderCell(vRow, FCodeCol, '项目节编号', 100);
  831. AddHeaderCell(vRow, FB_CodeCol, '清单编号', 80);
  832. AddHeaderCell(vRow, FP_CodeCol, '计量单元', 80);
  833. AddHeaderCell(vRow, FNameCol, '名称', 300);
  834. AddHeaderCell(vRow, FUnitsCol, '单位', 60);
  835. AddHeaderCell(vRow, FQuantityCol, '清单数量', 70);
  836. AddHeaderCell(vRow, FDgnQty1, '设计数量1', 70);
  837. AddHeaderCell(vRow, FDgnQty2, '设计数量2', 70);
  838. AddHeaderCell(vRow, FPriceCol, '单价', 70);
  839. AddHeaderCell(vRow, FTotalPriceCol, '合价', 70);
  840. AddHeaderCell(vRow, FDrawingCodeCol, '图号', 85);
  841. AddHeaderCell(vRow, FMemoStrCol, '备注', 100);
  842. end;
  843. destructor TBillsPosExcelExportor.Destroy;
  844. begin
  845. if FileExists(FTempFile) then
  846. DeleteFileOrFolder(FTempFile);
  847. FOExport.Free;
  848. inherited;
  849. end;
  850. procedure TBillsPosExcelExportor.ExportData(ASheet: TExportWorkSheet);
  851. begin
  852. ExportNode(ASheet, TBillsPosTreeNode(FTree.FirstNode));
  853. end;
  854. procedure TBillsPosExcelExportor.ExportNode(ASheet: TExportWorkSheet;
  855. ANode: TBillsPosTreeNode);
  856. var
  857. vRow: TExportRow;
  858. vCell: TExportCellNumber;
  859. begin
  860. if not Assigned(ANode) then Exit;
  861. vRow := ASheet.AddRow;
  862. vRow.AddCellString(ANode.Code);
  863. vRow.AddCellString(ANode.B_Code);
  864. vRow.AddCellString('');
  865. vRow.AddCellString(ANode.Name);
  866. vRow.AddCellString(ANode.Units);
  867. vCell := vRow.AddCellNumber(ANode.Quantity);
  868. vCell.EmptyIfZero := True;
  869. vCell := vRow.AddCellNumber(ANode.DgnQty1);
  870. vCell.EmptyIfZero := True;
  871. vCell := vRow.AddCellNumber(ANode.DgnQty2);
  872. vCell.EmptyIfZero := True;
  873. vCell := vRow.AddCellNumber(ANode.Price);
  874. vCell.EmptyIfZero := True;
  875. vCell := vRow.AddCellNumber(ANode.TotalPrice);
  876. vCell.EmptyIfZero := True;
  877. vRow.AddCellString(ANode.DrawingCode);
  878. vRow.AddCellString(ANode.MemoStr);
  879. ExportPos(ASheet, ANode);
  880. ExportNode(ASheet, TBillsPosTreeNode(ANode.FirstChild));
  881. ExportNode(ASheet, TBillsPosTreeNode(ANode.NextSibling));
  882. end;
  883. procedure TBillsPosExcelExportor.ExportPos(ASheet: TExportWorkSheet;
  884. ANode: TBillsPosTreeNode);
  885. var
  886. i: Integer;
  887. vPos: TPosNode;
  888. vRow: TExportRow;
  889. vCell: TExportCellNumber;
  890. begin
  891. if not Assigned(ANode) then Exit;
  892. for i := 0 to ANode.PosCount - 1 do
  893. begin
  894. vPos := ANode.Pos[i];
  895. vRow := ASheet.AddRow;
  896. vRow.AddCellString();
  897. vRow.AddCellString();
  898. vRow.AddCellString(IntToStr(i+1));
  899. vRow.AddCellString(vPos.Name);
  900. vRow.AddCellString();
  901. vCell := vRow.AddCellNumber(vPos.Quantity);
  902. vCell.EmptyIfZero := True;
  903. vCell := vRow.AddCellNumber();
  904. vCell.EmptyIfZero := True;
  905. vCell := vRow.AddCellNumber();
  906. vCell.EmptyIfZero := True;
  907. vCell := vRow.AddCellNumber();
  908. vCell.EmptyIfZero := True;
  909. vCell := vRow.AddCellNumber();
  910. vCell.EmptyIfZero := True;
  911. vRow.AddCellString();
  912. vRow.AddCellString(vPos.MemoStr);
  913. end;
  914. end;
  915. procedure TBillsPosExcelExportor.ExportToFile(ABillsIDTree: TBillsIDTree; const AFileName: string);
  916. var
  917. vExportor: TOCustomExporter;
  918. begin
  919. BeforeExport;
  920. try
  921. vExportor := GetExportor(ExtractFileExt(AFileName));
  922. ExportToSheet(ABillsIDTree, FOExport.AddWorkSheet);
  923. FOExport.SaveToFile(FTempFile, vExportor);
  924. if not FileExists(AFileName) or QuestMessage('存在同名文件,是否替换?') then
  925. CopyFile(PChar(FTempFile), PChar(AFileName), False);
  926. finally
  927. vExportor.Free;
  928. AfterExport;
  929. end;
  930. end;
  931. procedure TBillsPosExcelExportor.ExportToSheet(ABillsIDTree: TBillsIDTree; ASheet: TExportWorkSheet);
  932. begin
  933. DefineHeader(ASheet);
  934. FTree := TBillsPosTree.Create;
  935. try
  936. ConverseTreeNode(TBillsIDTreeNode(ABillsIDTree.FirstNode));
  937. FTree.CalculateAll;
  938. ExportData(ASheet);
  939. finally
  940. FTree.Free;
  941. end;
  942. end;
  943. function TBillsPosExcelExportor.HasGclChild(
  944. ANode: TBillsIDTreeNode): Boolean;
  945. var
  946. i: Integer;
  947. vChild: TBillsIDTreeNode;
  948. begin
  949. Result := False;
  950. for i := 0 to ANode.ChildCount - 1 do
  951. begin
  952. vChild := TBillsIDTreeNode(ANode.ChildNodes[i]);
  953. if (vChild.Rec.B_Code.AsString <> '') then
  954. begin
  955. Result := True;
  956. Exit;
  957. end;
  958. end;
  959. end;
  960. function TBillsPosExcelExportor.FindNode(AParent: TBillsPosTreeNode;
  961. AMatch: TBillsIDTreeNode): TBillsPosTreeNode;
  962. var
  963. i: Integer;
  964. vChild: TBillsPosTreeNode;
  965. begin
  966. Result := nil;
  967. if not Assigned(AParent) then Exit;
  968. for i := 0 to AParent.Children.Count - 1 do
  969. begin
  970. vChild := TBillsPosTreeNode(AParent.Children.Items[i]);
  971. if ((vChild.B_Code = AMatch.Rec.B_Code.AsString) And
  972. (vChild.Name = AMatch.Rec.Name.AsString) And
  973. (vChild.Units = AMatch.Rec.Units.AsString) And
  974. (PriceRoundTo(vChild.Price - AMatch.Rec.Price.AsFloat) = 0)) then
  975. begin
  976. Result := vChild;
  977. Break;
  978. end;
  979. end;
  980. end;
  981. end.