ExportExcel.pas 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. unit ExportExcel;
  2. interface
  3. uses
  4. ScXlsOutput, ScXlsCustomUD, DataBase, ZjIDTree, DBClient, Graphics, Math,
  5. Classes, ScBillsTree, ConstVarUnit, ScFileArchiver, ConstTypeUnit,
  6. SysUtils, Variants;
  7. // 小数的精确度
  8. const DoubleAccuracy: Double = 0.0001;
  9. type
  10. TExcelExportor = class
  11. private
  12. FRow: Integer;
  13. FBillItems: TList;
  14. FBillsData: TDMDataBase;
  15. FXlsOutput: TXlsOutPut;
  16. FProjectManager: TObject;
  17. procedure AddItem(ANode: TZjIDTreeNode; aXlsPage: TXlsCustomPage; aIncludeDQ: Boolean);
  18. procedure AddDQToExcel(ABillsID: Integer; AXlsPage: TXlsCustomPage);
  19. procedure AddDQItemQty(aBillsID: Integer; aCdsDQItems: TClientDataSet);
  20. procedure AddRecordToExcel(ANode: TZjIDTreeNode; aXlsPage: TXlsCustomPage; aIncludeDQ: Boolean);
  21. procedure InitializePage(aXlsPage: TXlsCustomPage; aSheetName: string);
  22. procedure InitializeQtyPage(aXlsPage: TXlsCustomPage; aSheetName: string);
  23. procedure CustomExportItems(aXlsPage: TXlsCustomPage; aIncludeDQ: Boolean);
  24. procedure CustomExportQty(aXlsPage: TXlsCustomPage; aIncludeDQ: Boolean; AChapter800: Boolean = False);
  25. procedure ExtractFirstPartBillsQty(AList: Tlist; ANode: TZjIDTreeNode);
  26. procedure ExtractChapter800(AList: Tlist; ANode: TZjIDTreeNode);
  27. function GetTotalPrice2(AObjBills: TBillIDRecord): Double;
  28. // 获取第二部分当前项的清单数量 GiLi
  29. function GetTotalQuantity2(AObjBills: TBillIDRecord): Double;
  30. {sheets}
  31. procedure ExportItems;
  32. procedure ExportProjItemsWithDQ;
  33. procedure ExportProjItemsNoDQ;
  34. procedure ExportQtyItemsWithDQ;
  35. procedure ExportQtyItemsNoDQ;
  36. procedure ExportQuantityBillsOfChapter800;
  37. public
  38. constructor Create(aBillsData: TDMDataBase; aProjMgr: TObject);
  39. destructor Destroy; override;
  40. procedure ExportToExcel(const aFileName: string; aStrings: TStrings; aFlag: Integer);
  41. end;
  42. TFlatGclExcelExportor = class
  43. private
  44. FRowIndex: Integer;
  45. FXlsOutPut: TXlsOutPut;
  46. FBillsData: TDMDataBase;
  47. FCodeCol: Integer;
  48. FNameCol: Integer;
  49. FUnitsCol: Integer;
  50. FQuantityCol: Integer;
  51. FPriceCol: Integer;
  52. FPegCol: Integer;
  53. // 单位工程
  54. FProCol: Integer;
  55. // 分部工程
  56. FSubProCol: Integer;
  57. // 分项工程
  58. FSubSubProCol: Integer;
  59. // 部位
  60. FPositonCol: Integer;
  61. FDrawingCol: Integer;
  62. procedure InitHeadCell(AXlsPage: TXlsCustomPage; ACol, ARow: Integer; const AText: string);
  63. procedure InitialPageHead(AXlsPage: TXlsCustomPage);
  64. procedure SetCommonCellFont(AXlsCell: TXlsCustomCell);
  65. procedure AddCellExceptZero(AXlsPage: TXlsCustomPage; ACol, ARow: Integer; AValue: Variant);
  66. procedure AddCellRightAlign(AXlsPage: TXlsCustomPage; ACol, ARow: Integer; AValue: Variant);
  67. procedure AddCellCenterAlign(AXlsPage: TXlsCustomPage; ACol, ARow: Integer; AValue: Variant);
  68. function CheckPegStr(const AStr: string): Boolean;
  69. function GetPeg(ANode: TZjIDTreeNode): string;
  70. function GetPro(ANode: TZjIDTreeNode): string;
  71. function GetSubPro(ANode: TZjIDTreeNode): string;
  72. function GetSubSubPro(ANode: TZjIDTreeNode): string;
  73. function GetPositon(ANode: TZjIDTreeNode): string;
  74. function GetDrawingCode(ANode: TZjIDTreeNode): string;
  75. procedure ExportData(AXlsPage: TXlsCustomPage; ANode: TZjIDTreeNode);
  76. procedure ExportNode(AXlsPage: TXlsCustomPage; ANode: TZjIDTreeNode);
  77. procedure ExportTo(AXlsPage: TXlsCustomPage);
  78. public
  79. constructor Create(ABillsData: TDMDataBase);
  80. destructor Destroy; override;
  81. procedure ExportFile(const AFileName: string);
  82. end;
  83. implementation
  84. uses
  85. DB,
  86. ConstMethodUnit,
  87. ScUpdateDataBase,
  88. ScProjectManager,
  89. ScConfig;
  90. { TExcelExportor }
  91. procedure TExcelExportor.AddDQItemQty(aBillsID: Integer;
  92. aCdsDQItems: TClientDataSet);
  93. var
  94. bFlag: Boolean;
  95. CDS: TClientDataSet;
  96. xlsCell: TXlsCustomCell;
  97. begin
  98. CDS := TClientDataSet.Create(nil);
  99. try
  100. CDS.CloneCursor(FBillsData.cdsDrawingQuantity, True);
  101. CDS.IndexFieldNames := 'BillsID';
  102. CDS.SetRange([ABillsID], [aBillsID]);
  103. while not CDS.Eof do
  104. begin
  105. if CDS.FieldByName('Name').AsString <> '' then
  106. begin
  107. bFlag := False;
  108. aCdsDQItems.First;
  109. while not aCdsDQItems.Eof do
  110. begin
  111. if SameText(CDS.FieldByName('Name').AsString,
  112. aCdsDQItems.FieldByName('Name').AsString)
  113. then
  114. begin
  115. aCdsDQItems.Edit;
  116. aCdsDQItems.FieldByName('Quantity').AsFloat := aCdsDQItems.FieldByName('Quantity').AsFloat
  117. +
  118. CDS.FieldByName('DQuantity1').AsFloat;
  119. aCdsDQItems.Post;
  120. bFlag := True;
  121. Break;
  122. end;
  123. aCdsDQItems.Next;
  124. end;
  125. if not bFlag then
  126. begin
  127. aCdsDQItems.Append;
  128. aCdsDQItems.FieldByName('Name').AsString := CDS.FieldByName('Name').AsString;
  129. aCdsDQItems.FieldByName('Units').AsString := CDS.FieldByName('Units').AsString;
  130. aCdsDQItems.FieldByName('Quantity').AsFloat := CDS.FieldByName('DQuantity1').AsFloat;
  131. aCdsDQItems.FieldByName('MemoStr').AsString := CDS.FieldByName('MemoContext').AsString;
  132. aCdsDQItems.Post;
  133. end;
  134. end;
  135. CDS.Next;
  136. end;
  137. finally
  138. CDS.Free;
  139. end;
  140. end;
  141. procedure TExcelExportor.AddDQToExcel(ABillsID: Integer;
  142. AXlsPage: TXlsCustomPage);
  143. var
  144. CDS: TClientDataSet;
  145. xlsCell: TXlsCustomCell;
  146. begin
  147. CDS := TClientDataSet.Create(nil);
  148. try
  149. CDS.CloneCursor(FBillsData.cdsDrawingQuantity, True);
  150. CDS.IndexFieldNames := 'BillsID';
  151. CDS.SetRange([ABillsID], [ABillsID]);
  152. while not CDS.Eof do
  153. begin
  154. if CDS.FieldByName('Name').AsString <> '' then
  155. begin
  156. AXlsPage.AddCell(2, FRow, CDS.FieldByName('Name').AsString);
  157. xlsCell := AXlsPage.AddCell(3, FRow, CDS.FieldByName('Units').AsString);
  158. xlsCell.HTextAlign := htaCenter;
  159. if CDS.FieldByName('DQuantity1').AsFloat <> 0 then
  160. begin
  161. xlsCell := AXlsPage.AddCell(5, FRow, CDS.FieldByName('DQuantity1').AsFloat);
  162. xlsCell.HTextAlign := htaRight;
  163. end;
  164. AXlsPage.AddCell(9, FRow, CDS.FieldByName('MemoContext').AsString);
  165. Inc(FRow);
  166. end;
  167. CDS.Next;
  168. end;
  169. finally
  170. CDS.Free;
  171. end;
  172. end;
  173. procedure TExcelExportor.AddItem(ANode: TZjIDTreeNode; aXlsPage: TXlsCustomPage; aIncludeDQ: Boolean);
  174. var
  175. xlsCell: TXlsCustomCell;
  176. begin
  177. with FBillsData do
  178. begin
  179. if cdsBills.FindKey([ANode.ID]) then
  180. begin
  181. xlsCell := aXlsPage.AddCell(0, FRow, cdsBillsCode.AsString);
  182. xlsCell := aXlsPage.AddCell(1, FRow, cdsBillsB_Code.AsString);
  183. xlsCell := aXlsPage.AddCell(2, FRow, cdsBillsName.AsString);
  184. xlsCell := aXlsPage.AddCell(3, FRow, cdsBillsUnits.AsString);
  185. xlsCell.HTextAlign := htaCenter;
  186. if cdsBillsQuantity.AsFloat <> 0 then
  187. begin
  188. xlsCell := aXlsPage.AddCell(4, FRow, cdsBillsQuantity.AsFloat);
  189. xlsCell.HTextAlign := htaRight;
  190. end;
  191. if cdsBillsDesignQuantity.AsFloat <> 0 then
  192. begin
  193. xlsCell := aXlsPage.AddCell(5, FRow, cdsBillsDesignQuantity.AsFloat);
  194. xlsCell.HTextAlign := htaRight;
  195. end;
  196. if cdsBillsDesignQuantity2.AsFloat <> 0 then
  197. begin
  198. xlsCell := aXlsPage.AddCell(6, FRow, cdsBillsDesignQuantity2.AsFloat);
  199. xlsCell.HTextAlign := htaRight;
  200. end;
  201. if cdsBillsUnitPrice.AsFloat <> 0 then
  202. begin
  203. xlsCell := aXlsPage.AddCell(7, FRow, cdsBillsUnitPrice.AsFloat);
  204. xlsCell.HTextAlign := htaRight;
  205. end;
  206. if cdsBillsTotalPrice.AsFloat <> 0 then
  207. begin
  208. xlsCell := aXlsPage.AddCell(8, FRow, cdsBillsTotalPrice.AsFloat);
  209. xlsCell.HTextAlign := htaRight;
  210. end;
  211. xlsCell := aXlsPage.AddCell(9, FRow, cdsBillsMemoStr.AsString);
  212. Inc(FRow);
  213. // add drawingQuantity here
  214. if aIncludeDQ then
  215. AddDQToExcel(cdsBillsID.AsInteger, aXlsPage);
  216. end;
  217. end;
  218. end;
  219. procedure TExcelExportor.AddRecordToExcel(ANode: TZjIDTreeNode; aXlsPage: TXlsCustomPage; aIncludeDQ: Boolean);
  220. begin
  221. if not Assigned(ANode) then Exit;
  222. {$IF Not DEFINED(_beEncrypt) and not DEFINED(_beOnLine)}
  223. if FRow >= MaxExcelRow then Exit;
  224. {$IFEND}
  225. AddItem(ANode, aXlsPage, aIncludeDQ);
  226. if Assigned(ANode.FirstChild) then AddRecordToExcel(ANode.FirstChild, aXlsPage, aIncludeDQ);
  227. if Assigned(ANode.NextSibling) then AddRecordToExcel(ANode.NextSibling, aXlsPage, aIncludeDQ);
  228. end;
  229. constructor TExcelExportor.Create(aBillsData: TDMDataBase; aProjMgr: TObject);
  230. begin
  231. FBillsData := aBillsData;
  232. FProjectManager := aProjMgr;
  233. FXlsOutput := TXlsOutPut.Create('Template.xls');
  234. FBillItems := TList.Create;
  235. end;
  236. procedure TExcelExportor.CustomExportItems(aXlsPage: TXlsCustomPage;
  237. aIncludeDQ: Boolean);
  238. var
  239. ztnNode: TZjIDTreeNode;
  240. begin
  241. FRow := 1;
  242. ztnNode := FBillsData.BillsTree.FirstNode;
  243. // 新需求,全部导出 chenshilong, 2012-12-10
  244. while Assigned(ztnNode) do
  245. begin
  246. AddItem(ztnNode, aXlsPage, aIncludeDQ);
  247. if Assigned(ztnNode.FirstChild) then
  248. AddRecordToExcel(ztnNode.FirstChild, aXlsPage, aIncludeDQ);
  249. ztnNode := ztnNode.NextSibling;
  250. end;
  251. (*
  252. {第一部分}
  253. if Assigned(ztnNode) then
  254. AddItem(ztnNode, aXlsPage, aIncludeDQ);
  255. if Assigned(ztnNode.FirstChild) then AddRecordToExcel(ztnNode.FirstChild, aXlsPage, aIncludeDQ);
  256. {第二部分}
  257. ztnNode := ztnNode.NextSibling;
  258. AddItem(ztnNode, aXlsPage, aIncludeDQ);
  259. if Assigned(ztnNode.FirstChild) then AddRecordToExcel(ztnNode.FirstChild, aXlsPage, aIncludeDQ);
  260. {第三部分}
  261. ztnNode := ztnNode.NextSibling;
  262. AddItem(ztnNode, aXlsPage, aIncludeDQ);
  263. if Assigned(ztnNode.FirstChild) then AddRecordToExcel(ztnNode.FirstChild, aXlsPage, aIncludeDQ);
  264. *)
  265. end;
  266. function Compare(Item1, Item2: Pointer): Integer;
  267. begin
  268. Result := CompareCodeWithChar(TBillIDRecord(Item1).Code, TBillIDRecord(Item2).Code);
  269. if not ScConfigInfo.MatchCodeOnly and (Result = 0) then
  270. Result := CompareText(TBillIDRecord(Item1).Name, TBillIDRecord(Item2).Name)
  271. end;
  272. procedure TExcelExportor.CustomExportQty(aXlsPage: TXlsCustomPage;
  273. aIncludeDQ: Boolean; AChapter800: Boolean);
  274. var
  275. // cdsQtyItems : TClientDataSet;
  276. cdsDQItems : TClientDataSet;
  277. xlsCell : TXlsCustomCell;
  278. strOldCode : string;
  279. strOldName : string;
  280. strOldUnit : string;
  281. strOldMemo : string;
  282. strNewCode : string;
  283. strNewName : string;
  284. strNewUnit : string;
  285. strNewMemo : string;
  286. dQuantity : Double;
  287. dQuantity2 : Double;
  288. dUnitPrice : Double;
  289. dUnitPrice2 : Double;
  290. dTotalPrice : Double;
  291. dTotalPrice2: Double;
  292. bFirst : Boolean;
  293. lstPQ : TList;
  294. bRecord : TBillIDRecord;
  295. objBills : TBillIDRecord;
  296. qRecord : TDQRecord;
  297. iLoopPQ : Integer;
  298. iLoopDQ : Integer;
  299. iLoopBills : Integer;
  300. vSL: TStringList;
  301. begin
  302. FRow := 1;
  303. bFirst := True;
  304. lstPQ := TList.Create;
  305. // cdsQtyItems := TClientDataSet.Create(nil);
  306. cdsDQItems := TClientDataSet.Create(nil);
  307. try
  308. { create dataset }
  309. with cdsDQItems.FieldDefs.AddFieldDef do
  310. begin
  311. DataType := ftWideString;
  312. Size := 200;
  313. Name := 'Name';
  314. end;
  315. with cdsDQItems.FieldDefs.AddFieldDef do
  316. begin
  317. DataType := ftWideString;
  318. Size := 50;
  319. Name := 'Units';
  320. end;
  321. with cdsDQItems.FieldDefs.AddFieldDef do
  322. begin
  323. DataType := ftFloat;
  324. Name := 'Quantity';
  325. end;
  326. with cdsDQItems.FieldDefs.AddFieldDef do
  327. begin
  328. DataType := ftWideString;
  329. Size := 200;
  330. Name := 'MemoStr';
  331. end;
  332. cdsDQItems.CreateDataSet;
  333. if FBillItems.Count = 0 then
  334. begin
  335. ExtractFirstPartBillsQty(FBillItems, FBillsData.BillsTree.FirstNode);
  336. // 其它部分也有工程量清单 chenshilong, 2013-04-28
  337. //ExtractFirstPartBillsQty(FBillItems, FBillsData.BillsTree[2]);
  338. ExtractFirstPartBillsQty(FBillItems, FBillsData.BillsTree[3]);
  339. ExtractFirstPartBillsQty(FBillItems, FBillsData.BillsTree[7]); // 预留费用
  340. ExtractFirstPartBillsQty(FBillItems, FBillsData.BillsTree[15]); // 其他费用项目
  341. ExtractChapter800(FBillItems, FBillsData.BillsTree.FirstNode.NextSibling);
  342. FBillItems.Sort(Compare);
  343. end;
  344. { 终于搞清了李涛的原理:先对列表进行排序,然后依次跟上条比,相同则合并。
  345. 凤岗 项目编号为803-5-2B的没有合并,原因是:
  346. FBillItems.Sort(Compare) 这句不可靠,排成了:
  347. Idx123 803-5-2B 24口接入以太网交换机(含光纤模块)
  348. Idx124 803-5-2A 24口接入以太网交换机(含光纤模块)
  349. Idx125 803-5-2A 24口接入以太网交换机(含光纤模块)
  350. Idx126 803-5-2B 24口接入以太网交换机(含光纤模块)
  351. 导致中间两条可以正常合并,1、4两条不行。
  352. chenshilong, 2013-08-20 }
  353. {vSL := TStringList.Create;
  354. for iLoopBills := 0 to FBillItems.Count - 1 do
  355. begin
  356. objBills := TBillIDRecord(FBillItems.List^[iLoopBills]);
  357. vSL.Add(IntToStr(iLoopBills)+ #9 + objBills.Code + #9 + objBills.Name);
  358. end;
  359. vSL.SaveToFile('E:\ListLT.txt');
  360. vSL.Free; }
  361. for iLoopBills := 0 to FBillItems.Count - 1 do
  362. //with cdsQtyItems do
  363. begin
  364. objBills := TBillIDRecord(FBillItems.List^[iLoopBills]);
  365. if AChapter800 and (objBills.Code[1] <> '8') then
  366. Continue;
  367. {CloneCursor(FBillsData.cdsBills, True);
  368. IndexFieldNames := 'B_Code';
  369. Filter := 'B_Code<>''''';
  370. Filtered := True;
  371. while not Eof do
  372. begin
  373. strNewCode := FieldByName('B_Code').AsString;
  374. strNewName := FieldByName('Name').AsString;
  375. strNewUnit := FieldByName('Units').AsString;
  376. strNewMemo := FieldByName('MemoStr').AsString; }
  377. strNewCode := objBills.Code;
  378. strNewName := objBills.Name;
  379. strNewUnit := objBills.Units;
  380. strNewMemo := objBills.MemoStr;
  381. if bFirst then
  382. begin
  383. dQuantity := 0;
  384. dQuantity2 := 0;
  385. dUnitPrice := 0;
  386. dUnitPrice2 := 0;
  387. dTotalPrice := 0;
  388. dTotalPrice2 := 0;
  389. strOldCode := strNewCode;
  390. strOldName := strNewName;
  391. strOldUnit := strNewUnit;
  392. strOldMemo := strNewMemo;
  393. bFirst := False;
  394. end;
  395. if (ScConfigInfo.MatchCodeOnly and (strOldCode <> strNewCode)) or
  396. (not ScConfigInfo.MatchCodeOnly and ((strOldCode <> strNewCode) or (strOldName <> strNewName))) then
  397. begin
  398. bRecord := TBillIDRecord.Create;
  399. bRecord.Code := strOldCode;
  400. bRecord.Name := strOldName;
  401. bRecord.Units := strOldUnit;
  402. // 如果清单子目在第一部分的汇总数量约等于0,则取第二部分的汇总数量,否则,取第一部分的汇总数量
  403. if (dQuantity = 0) then
  404. bRecord.Quantity := dQuantity2
  405. else
  406. bRecord.Quantity := dQuantity;
  407. bRecord.Quantity2 := dQuantity2;
  408. // 加权平均求单价
  409. if bRecord.Quantity <> 0 then
  410. begin
  411. dUnitPrice := RoundTo((dTotalPrice + dTotalPrice2)/bRecord.Quantity, -2);
  412. dUnitPrice2 := RoundTo(dTotalPrice2/bRecord.Quantity, -2);
  413. end;
  414. bRecord.UnitPrice := dUnitPrice;
  415. bRecord.UnitPrice2 := dUnitPrice2;
  416. bRecord.TotalPrice := RoundTo(dUnitPrice*dQuantity, 0);// dTotalPrice;
  417. bRecord.TotalPrice2 := RoundTo(dUnitPrice2*dQuantity2, 0); //dTotalPrice2;
  418. {if dQuantity = 0 then
  419. bRecord.UnitPrice := 0
  420. else
  421. bRecord.UnitPrice := RoundTo((dTotalPrice + dTotalPrice2)/dQuantity, -2); }
  422. bRecord.MemoStr := strOldMemo;
  423. lstPQ.Add(bRecord);
  424. if aIncludeDQ then
  425. begin
  426. cdsDQItems.First;
  427. while not cdsDQItems.Eof do
  428. begin
  429. qRecord := TDQRecord.Create;
  430. qRecord.Name := cdsDQItems.FieldByName('Name').AsString;
  431. qRecord.Units := cdsDQItems.FieldByName('Units').AsString;
  432. qRecord.DQuantity := cdsDQItems.FieldByName('Quantity').AsFloat;
  433. qRecord.MemoStr := cdsDQItems.FieldByName('MemoStr').AsString;
  434. bRecord.List.Add(qRecord);
  435. cdsDQItems.Next;
  436. end;
  437. cdsDQItems.EmptyDataSet;
  438. end;
  439. {dQuantity := FieldByName('Quantity').AsFloat;
  440. dTotalPrice := FieldByName('TotalPrice').AsFloat; }
  441. dQuantity := objBills.Quantity;
  442. dQuantity2 := GetTotalQuantity2(objBills);
  443. dUnitPrice := objBills.UnitPrice;
  444. dUnitPrice2 := objBills.UnitPrice2;
  445. dTotalPrice := objBills.Quantity * objBills.UnitPrice;
  446. dTotalPrice2 := GetTotalPrice2(objBills); //objBills.Quantity*objBills.UnitPrice2;
  447. strOldCode := strNewCode;
  448. strOldName := strNewName;
  449. strOldUnit := strNewUnit;
  450. strOldMemo := strNewMemo;
  451. end
  452. else
  453. begin
  454. {dQuantity := dQuantity + FieldByName('Quantity').AsFloat;
  455. dTotalPrice := dTotalPrice + FieldByName('TotalPrice').AsFloat; }
  456. dQuantity := dQuantity + objBills.Quantity;
  457. dQuantity2 := dQuantity2 + GetTotalQuantity2(objBills);
  458. dTotalPrice := dTotalPrice + objBills.Quantity*objBills.UnitPrice;
  459. dTotalPrice2 := dTotalPrice2 + GetTotalPrice2(objBills);
  460. end;
  461. if aIncludeDQ then
  462. AddDQItemQty(objBills.NewID {FieldByName('ID').Value}, cdsDQItems);
  463. // Next;
  464. end;
  465. { last record }
  466. bRecord := TBillIDRecord.Create;
  467. bRecord.Code := strOldCode;
  468. bRecord.Name := strOldName;
  469. bRecord.Units := strOldUnit;
  470. //bRecord.Quantity := dQuantity;
  471. if (dQuantity = 0) then
  472. bRecord.Quantity := dQuantity2
  473. else
  474. bRecord.Quantity := dQuantity;
  475. bRecord.Quantity2 := dQuantity2;
  476. {
  477. if dQuantity <> 0 then
  478. begin
  479. dUnitPrice := RoundTo((dTotalPrice + dTotalPrice2)/dQuantity, -2);
  480. dUnitPrice2 := RoundTo(dTotalPrice2/dQuantity, -2);
  481. end;
  482. }
  483. // 加权平均求单价
  484. if bRecord.Quantity <> 0 then
  485. begin
  486. dUnitPrice := RoundTo((dTotalPrice + dTotalPrice2)/bRecord.Quantity, -2);
  487. dUnitPrice2 := RoundTo(dTotalPrice2/bRecord.Quantity, -2);
  488. end;
  489. bRecord.UnitPrice := dUnitPrice;
  490. bRecord.UnitPrice2 := dUnitPrice2;
  491. bRecord.TotalPrice := RoundTo(dUnitPrice*bRecord.Quantity, 0);
  492. bRecord.TotalPrice2 := RoundTo(dUnitPrice2*bRecord.Quantity, 0);
  493. { bRecord.TotalPrice := dTotalPrice;
  494. bRecord.TotalPrice2 := dTotalPrice2;
  495. if dQuantity = 0 then
  496. bRecord.UnitPrice := 0
  497. else
  498. bRecord.UnitPrice := RoundTo((dTotalPrice + dTotalPrice2) / dQuantity, -2); }
  499. bRecord.MemoStr := strOldMemo;
  500. lstPQ.Add(bRecord);
  501. lstPQ.Sort(Compare);
  502. for iLoopPQ := 0 to lstPQ.Count - 1 do
  503. begin
  504. bRecord := TBillIDRecord(lstPQ.List^[iLoopPQ]);
  505. xlsCell := aXlsPage.AddCell(0, FRow, bRecord.Code);
  506. xlsCell := aXlsPage.AddCell(1, FRow, bRecord.Name);
  507. xlsCell := aXlsPage.AddCell(2, FRow, bRecord.Units);
  508. xlsCell.HTextAlign := htaCenter;
  509. if bRecord.Quantity <> 0 then
  510. begin
  511. xlsCell := aXlsPage.AddCell(3, FRow, bRecord.Quantity);
  512. xlsCell.HTextAlign := htaRight;
  513. end;
  514. if bRecord.UnitPrice <> 0 then
  515. begin
  516. xlsCell := aXlsPage.AddCell(4, FRow, bRecord.UnitPrice);
  517. xlsCell.HTextAlign := htaRight;
  518. end;
  519. if bRecord.TotalPrice + bRecord.TotalPrice2 <> 0 then
  520. begin
  521. if AChapter800 then
  522. begin
  523. if bRecord.TotalPrice <> 0 then
  524. xlsCell := aXlsPage.AddCell(5, FRow, bRecord.TotalPrice - bRecord.TotalPrice2);
  525. end
  526. else
  527. xlsCell := aXlsPage.AddCell(5, FRow, bRecord.TotalPrice);
  528. xlsCell.HTextAlign := htaRight;
  529. end;
  530. if AChapter800 then
  531. begin
  532. if bRecord.TotalPrice2 <> 0 then
  533. begin
  534. xlsCell := aXlsPage.AddCell(6, FRow, bRecord.TotalPrice2);
  535. xlsCell.HTextAlign := htaRight;
  536. end;
  537. end
  538. else
  539. xlsCell := aXlsPage.AddCell(6, FRow, bRecord.MemoStr);
  540. Inc(FRow);
  541. {$IF Not DEFINED(_beEncrypt) and not DEFINED(_beOnLine)}
  542. if FRow >= MaxExcelRow then Exit;
  543. {$IFEND}
  544. for iLoopDQ := 0 to bRecord.List.Count - 1 do
  545. begin
  546. qRecord := TDQRecord(bRecord.List.List^[iLoopDQ]);
  547. aXlsPage.AddCell(1, FRow, qRecord.Name);
  548. xlsCell := aXlsPage.AddCell(2, FRow, qRecord.Units);
  549. xlsCell.HTextAlign := htaCenter;
  550. if qRecord.DQuantity <> 0 then
  551. begin
  552. xlsCell := aXlsPage.AddCell(3, FRow, qRecord.DQuantity);
  553. xlsCell.HTextAlign := htaRight;
  554. end;
  555. if AChapter800 then
  556. aXlsPage.AddCell(7, FRow, qRecord.MemoStr)
  557. else
  558. aXlsPage.AddCell(6, FRow, qRecord.MemoStr);
  559. Inc(FRow);
  560. end;
  561. end;
  562. finally
  563. cdsDQItems.Free;
  564. // cdsQtyItems.Free;
  565. ClearObjectList(lstPQ);
  566. lstPQ.Free;
  567. end;
  568. end;
  569. destructor TExcelExportor.Destroy;
  570. begin
  571. FXlsOutput.Free;
  572. ClearObjectList(FBillItems);
  573. FBillItems.Free;
  574. inherited;
  575. end;
  576. procedure TExcelExportor.ExportItems;
  577. var
  578. xlsPage: TXlsCustomPage;
  579. begin
  580. xlsPage := FXlsOutput.AddPage;
  581. InitializePage(xlsPage, '分项清单');
  582. CustomExportItems(xlsPage, True);
  583. end;
  584. procedure TExcelExportor.ExportProjItemsNoDQ;
  585. var
  586. xlsPage: TXlsCustomPage;
  587. begin
  588. xlsPage := FXlsOutput.AddPage;
  589. InitializePage(xlsPage, '项目清单');
  590. CustomExportItems(xlsPage, False);
  591. end;
  592. procedure TExcelExportor.ExportProjItemsWithDQ;
  593. var
  594. xlsPage: TXlsCustomPage;
  595. begin
  596. xlsPage := FXlsOutput.AddPage;
  597. InitializePage(xlsPage, '项目清单(含细目)');
  598. CustomExportItems(xlsPage, True);
  599. end;
  600. procedure TExcelExportor.ExportQtyItemsNoDQ;
  601. var
  602. xlsPage: TXlsCustomPage;
  603. begin
  604. xlsPage := FXlsOutput.AddPage;
  605. InitializeQtyPage(xlsPage, '工程量清单');
  606. CustomExportQty(xlsPage, False);
  607. end;
  608. procedure TExcelExportor.ExportQtyItemsWithDQ;
  609. var
  610. xlsPage: TXlsCustomPage;
  611. begin
  612. xlsPage := FXlsOutput.AddPage;
  613. InitializeQtyPage(xlsPage, '工程量清单(含细目)');
  614. CustomExportQty(xlsPage, True);
  615. end;
  616. procedure TExcelExportor.ExportQuantityBillsOfChapter800;
  617. var
  618. xlsPage: TXlsCustomPage;
  619. procedure SetPageStyleOfChapter800;
  620. var
  621. xlsCell: TXlsCustomCell;
  622. begin
  623. FRow := 0;
  624. XlsPage.Widths[0] := 80;
  625. XlsPage.Widths[1] := 300;
  626. XlsPage.Widths[2] := 50;
  627. //XlsPage.Widths[7] := 150;
  628. XlsPage.SheetName := '800章';
  629. xlsCell := XlsPage.AddCell(0, 0, '清单编号');
  630. xlsCell.HTextAlign := htaCenter;
  631. xlsCell.Font.Size := 10;
  632. xlsCell.Font.Style := [fsBold];
  633. xlsCell.Font.Name := '黑体';
  634. xlsCell := XlsPage.AddCell(1, 0, '名称');
  635. xlsCell.HTextAlign := htaCenter;
  636. xlsCell.Font.Size := 10;
  637. xlsCell.Font.Style := [fsBold];
  638. xlsCell.Font.Name := '黑体';
  639. xlsCell := XlsPage.AddCell(2, 0, '单位');
  640. xlsCell.HTextAlign := htaCenter;
  641. xlsCell.Font.Size := 10;
  642. xlsCell.Font.Style := [fsBold];
  643. xlsCell.Font.Name := '黑体';
  644. xlsCell := XlsPage.AddCell(3, 0, '数量');
  645. xlsCell.HTextAlign := htaCenter;
  646. xlsCell.Font.Size := 10;
  647. xlsCell.Font.Style := [fsBold];
  648. xlsCell.Font.Name := '黑体';
  649. xlsCell := XlsPage.AddCell(4, 0, '单价');
  650. xlsCell.HTextAlign := htaCenter;
  651. xlsCell.Font.Size := 10;
  652. xlsCell.Font.Style := [fsBold];
  653. xlsCell.Font.Name := '黑体';
  654. xlsCell := XlsPage.AddCell(5, 0, '安装费');
  655. xlsCell.HTextAlign := htaCenter;
  656. xlsCell.Font.Size := 10;
  657. xlsCell.Font.Style := [fsBold];
  658. xlsCell.Font.Name := '黑体';
  659. xlsCell := XlsPage.AddCell(6, 0, '器具购置费');
  660. xlsCell.HTextAlign := htaCenter;
  661. xlsCell.Font.Size := 10;
  662. xlsCell.Font.Style := [fsBold];
  663. xlsCell.Font.Name := '黑体';
  664. { xlsCell := XlsPage.AddCell(7, 0, '备注');
  665. xlsCell.HTextAlign := htaCenter;
  666. xlsCell.Font.Size := 10;
  667. xlsCell.Font.Style := [fsBold];
  668. xlsCell.Font.Name := '黑体'; }
  669. end;
  670. begin
  671. xlsPage := FXlsOutput.AddPage;
  672. SetPageStyleOfChapter800;
  673. CustomExportQty(xlsPage, False, True);
  674. end;
  675. procedure TExcelExportor.ExportToExcel(const aFileName: string; aStrings: TStrings; aFlag: Integer);
  676. var
  677. I, iIdx: Integer;
  678. sDir, sFileName: string;
  679. Project: TProject;
  680. pfaArchiver: TScProjectFileArchiver;
  681. begin
  682. if (aFlag <> 1) and (aStrings.Count > 1) then
  683. begin
  684. sDir := ExtractFilePath(ParamStr(0));
  685. for I := 0 to aStrings.Count - 1 do
  686. begin
  687. pfaArchiver := nil;
  688. sFileName := string(aStrings.Objects[I]);
  689. iIdx := TProjectManager(FProjectManager).CheckProjectExists(sDir + sFileName);
  690. if iIdx = -1 then
  691. begin
  692. pfaArchiver := TScProjectFileArchiver.Create;
  693. pfaArchiver.FileName := sDir + sFileName;
  694. pfaArchiver.OpenFile;
  695. FBillsData := TDMDataBase.Create(nil);
  696. FBillsData.Connection := pfaArchiver.Connection;
  697. UpdateDB(pfaArchiver);
  698. pfaArchiver.Save;
  699. FBillsData.Active := True;
  700. FBillsData.ConnectionBillsTree;
  701. end
  702. else
  703. FBillsData := TProjectManager(FProjectManager).Projects[iIdx].BillsData;
  704. if aStrings[I] = '1' then
  705. begin
  706. { 分项清单 } // 标段的
  707. ExportItems;
  708. end
  709. else
  710. begin
  711. { 项目清单 带图纸 } // 汇总后的
  712. ExportProjItemsWithDQ;
  713. { 项目清单 不带图纸 }
  714. ExportProjItemsNoDQ; // 汇总后的
  715. { 工程量清单 带图纸 }
  716. ExportQtyItemsWithDQ; // 标段和汇总后的一样,随便哪个
  717. { 工程量清单 不带图纸 }
  718. ExportQtyItemsNoDQ;
  719. ExportQuantityBillsOfChapter800;
  720. end;
  721. if Assigned(pfaArchiver) then
  722. begin
  723. FBillsData.Free;
  724. pfaArchiver.Free;
  725. end;
  726. end;
  727. end
  728. else
  729. begin
  730. { 分项清单 } // 标段的
  731. ExportItems;
  732. { 工程量清单 带图纸 }
  733. ExportQtyItemsWithDQ; // 标段和汇总后的一样,随便哪个
  734. { 工程量清单 不带图纸 }
  735. ExportQtyItemsNoDQ;
  736. ExportQuantityBillsOfChapter800;
  737. end;
  738. { 生成文件 }
  739. FXlsOutput.SaveToFile(aFileName);
  740. end;
  741. procedure TExcelExportor.ExtractChapter800(AList: Tlist;
  742. ANode: TZjIDTreeNode);
  743. // Added by GiLi 2012-4-25 判断第二分部的800章在第一部分是否存在(编号)
  744. function IsItemInChapter1(const ABCode: string): Boolean;
  745. var
  746. I: Integer;
  747. objBills: TBillIDRecord;
  748. begin
  749. for I := 0 to FBillItems.Count - 1 do
  750. begin
  751. objBills := TBillIDRecord(FBillItems.List^[I]);
  752. if ABCode = objBills.Code then
  753. begin
  754. Result := True;
  755. Exit;
  756. end;
  757. end;
  758. Result := False;
  759. end;
  760. function IsItemOfChapter800(ABillNode: TZjIDTreeNode): Boolean;
  761. begin
  762. Result := (TScBillsItem(ABillNode).SBillBCode <> '') and
  763. (TScBillsItem(ABillNode).SBillBCode[1] = '8');
  764. end;
  765. procedure AddTotalPriceOfChapter800(AQuantity, AUnitPrice: Double; const ABCode: string);
  766. var
  767. I: Integer;
  768. objBills: TBillIDRecord;
  769. FloatItem: TFloatItem;
  770. begin
  771. for I := 0 to FBillItems.Count - 1 do
  772. begin
  773. objBills := TBillIDRecord(FBillItems.List^[I]);
  774. if ABCode = objBills.Code then
  775. begin
  776. {if objBills.Quantity = 0 then
  777. objBills.Quantity := AQuantity; }
  778. FloatItem := TFloatItem.Create;
  779. FloatItem.Quantity2 := AQuantity;
  780. FloatItem.UnitPrice2 := AUnitPrice;
  781. objBills.List.Add(FloatItem);
  782. Break;
  783. end;
  784. end;
  785. end;
  786. var
  787. I: Integer;
  788. ztnNode: TZjIDTreeNode;
  789. objBills: TBillIDRecord;
  790. begin
  791. for I := 0 to ANode.ChildCount - 1 do
  792. begin
  793. ztnNode := ANode.ChildNodes[I];
  794. if IsItemOfChapter800(ztnNode) then
  795. begin
  796. if FBillsData.cdsBills.FindKey([ztnNode.ID]) then
  797. begin
  798. // 如果第二部分有800章,第一部分没有,那么把第二部分算入第一部分,方便处理 GiLi
  799. if not IsItemInChapter1(FBillsData.cdsBillsB_Code.AsString) then
  800. begin
  801. objBills := TBillIDRecord.Create;
  802. objBills.NewID := FBillsData.cdsBillsID.AsInteger;
  803. objBills.Code := FBillsData.cdsBillsB_Code.AsString;
  804. objBills.Name := FBillsData.cdsBillsName.AsString;
  805. objBills.Units := FBillsData.cdsBillsUnits.AsString;
  806. {
  807. if not ztnNode.HasChildren then
  808. begin
  809. objBills.Quantity := FBillsData.cdsBillsQuantity.AsFloat;
  810. objBills.UnitPrice := FBillsData.cdsBillsUnitPrice.AsFloat;
  811. objBills.TotalPrice := FBillsData.cdsBillsTotalPrice.AsFloat;
  812. end; }
  813. objBills.Quantity := 0;
  814. objBills.Quantity2 := FBillsData.cdsBillsQuantity.AsFloat;
  815. objBills.UnitPrice := 0;
  816. objBills.TotalPrice := 0;
  817. objBills.MemoStr := FBillsData.cdsBillsMemoStr.AsString;
  818. AList.Add(objBills);
  819. end;
  820. AddTotalPriceOfChapter800(FBillsData.cdsBillsQuantity.AsFloat,
  821. FBillsData.cdsBillsUnitPrice.AsFloat,
  822. FBillsData.cdsBillsB_Code.AsString);
  823. end;
  824. end;
  825. ExtractChapter800(AList, ztnNode);
  826. end;
  827. end;
  828. procedure TExcelExportor.ExtractFirstPartBillsQty(AList: Tlist;
  829. ANode: TZjIDTreeNode);
  830. var
  831. I: Integer;
  832. cNode: TZjIDTreeNode;
  833. objBills: TBillIDRecord;
  834. begin
  835. for I := 0 to ANode.ChildCount - 1 do
  836. begin
  837. cNode := ANode.ChildNodes[I];
  838. if TScBillsItem(cNode).SBillBCode <> '' then
  839. begin
  840. if FBillsData.cdsBills.FindKey([cNode.ID]) then
  841. begin
  842. objBills := TBillIDRecord.Create;
  843. objBills.NewID := FBillsData.cdsBillsID.AsInteger;
  844. objBills.Code := FBillsData.cdsBillsB_Code.AsString;
  845. objBills.Name := FBillsData.cdsBillsName.AsString;
  846. objBills.Units := FBillsData.cdsBillsUnits.AsString;
  847. if not cNode.HasChildren then
  848. begin
  849. objBills.Quantity := FBillsData.cdsBillsQuantity.AsFloat;
  850. objBills.UnitPrice := FBillsData.cdsBillsUnitPrice.AsFloat;
  851. objBills.TotalPrice := FBillsData.cdsBillsTotalPrice.AsFloat;
  852. end;
  853. objBills.MemoStr := FBillsData.cdsBillsMemoStr.AsString;
  854. AList.Add(objBills);
  855. end;
  856. end;
  857. ExtractFirstPartBillsQty(AList, cNode);
  858. end;
  859. end;
  860. function TExcelExportor.GetTotalPrice2(AObjBills: TBillIDRecord): Double;
  861. var
  862. I: Integer;
  863. FloatItem: TFloatItem;
  864. begin
  865. Result := 0;
  866. for I := 0 to AObjBills.List.Count - 1 do
  867. begin
  868. FloatItem := TFloatItem(AObjBills.List[I]);
  869. // Modified By MaiXinRong 2012-03-19 器具购置费用第二部分的数量*单价
  870. Result := Result + FloatItem.Quantity2 * FloatItem.UnitPrice2;
  871. end;
  872. end;
  873. function TExcelExportor.GetTotalQuantity2(
  874. AObjBills: TBillIDRecord): Double;
  875. var
  876. I: Integer;
  877. FloatItem: TFloatItem;
  878. begin
  879. Result := 0;
  880. for I := 0 to AObjBills.List.Count - 1 do
  881. begin
  882. FloatItem := TFloatItem(AObjBills.List[I]);
  883. Result := Result + FloatItem.Quantity2;
  884. end;
  885. end;
  886. procedure TExcelExportor.InitializePage(aXlsPage: TXlsCustomPage; aSheetName: string);
  887. var
  888. xlsCell: TXlsCustomCell;
  889. begin
  890. FRow := 0;
  891. aXlsPage.Widths[0] := 100;
  892. aXlsPage.Widths[2] := 300;
  893. aXlsPage.Widths[3] := 50;
  894. aXlsPage.Widths[9] := 100;
  895. aXlsPage.SheetName := aSheetName;
  896. xlsCell := aXlsPage.AddCell(0, FRow, '预算项目节');
  897. xlsCell.HTextAlign := htaCenter;
  898. xlsCell.Font.Size := 10;
  899. xlsCell.Font.Style := [fsBold];
  900. xlsCell.Font.Name := '黑体';
  901. xlsCell := aXlsPage.AddCell(1, FRow, '清单子目号');
  902. xlsCell.HTextAlign := htaCenter;
  903. xlsCell.Font.Size := 10;
  904. xlsCell.Font.Style := [fsBold];
  905. xlsCell.Font.Name := '黑体';
  906. xlsCell := aXlsPage.AddCell(2, FRow, '名称');
  907. xlsCell.HTextAlign := htaCenter;
  908. xlsCell.Font.Size := 10;
  909. xlsCell.Font.Style := [fsBold];
  910. xlsCell.Font.Name := '黑体';
  911. xlsCell := aXlsPage.AddCell(3, FRow, '单位');
  912. xlsCell.HTextAlign := htaCenter;
  913. xlsCell.Font.Size := 10;
  914. xlsCell.Font.Style := [fsBold];
  915. xlsCell.Font.Name := '黑体';
  916. xlsCell := aXlsPage.AddCell(4, FRow, '清单数量');
  917. xlsCell.HTextAlign := htaCenter;
  918. xlsCell.Font.Size := 10;
  919. xlsCell.Font.Style := [fsBold];
  920. xlsCell.Font.Name := '黑体';
  921. xlsCell := aXlsPage.AddCell(5, FRow, '设计数量1');
  922. xlsCell.HTextAlign := htaCenter;
  923. xlsCell.Font.Size := 10;
  924. xlsCell.Font.Style := [fsBold];
  925. xlsCell.Font.Name := '黑体';
  926. xlsCell := aXlsPage.AddCell(6, FRow, '设计数量2');
  927. xlsCell.HTextAlign := htaCenter;
  928. xlsCell.Font.Size := 10;
  929. xlsCell.Font.Style := [fsBold];
  930. xlsCell.Font.Name := '黑体';
  931. xlsCell := aXlsPage.AddCell(7, FRow, '单价');
  932. xlsCell.HTextAlign := htaCenter;
  933. xlsCell.Font.Size := 10;
  934. xlsCell.Font.Style := [fsBold];
  935. xlsCell.Font.Name := '黑体';
  936. xlsCell := aXlsPage.AddCell(8, FRow, '合价');
  937. xlsCell.HTextAlign := htaCenter;
  938. xlsCell.Font.Size := 10;
  939. xlsCell.Font.Style := [fsBold];
  940. xlsCell.Font.Name := '黑体';
  941. xlsCell := aXlsPage.AddCell(9, FRow, '备注');
  942. xlsCell.HTextAlign := htaCenter;
  943. xlsCell.Font.Size := 10;
  944. xlsCell.Font.Style := [fsBold];
  945. xlsCell.Font.Name := '黑体';
  946. end;
  947. procedure TExcelExportor.InitializeQtyPage(aXlsPage: TXlsCustomPage;
  948. aSheetName: string);
  949. var
  950. xlsCell: TXlsCustomCell;
  951. begin
  952. FRow := 0;
  953. aXlsPage.Widths[0] := 80;
  954. aXlsPage.Widths[1] := 300;
  955. aXlsPage.Widths[2] := 50;
  956. aXlsPage.Widths[6] := 200;
  957. aXlsPage.SheetName := aSheetName;
  958. xlsCell := aXlsPage.AddCell(0, FRow, '清单编号');
  959. xlsCell.HTextAlign := htaCenter;
  960. xlsCell.Font.Size := 10;
  961. xlsCell.Font.Style := [fsBold];
  962. xlsCell.Font.Name := '黑体';
  963. xlsCell := aXlsPage.AddCell(1, FRow, '名称');
  964. xlsCell.HTextAlign := htaCenter;
  965. xlsCell.Font.Size := 10;
  966. xlsCell.Font.Style := [fsBold];
  967. xlsCell.Font.Name := '黑体';
  968. xlsCell := aXlsPage.AddCell(2, FRow, '单位');
  969. xlsCell.HTextAlign := htaCenter;
  970. xlsCell.Font.Size := 10;
  971. xlsCell.Font.Style := [fsBold];
  972. xlsCell.Font.Name := '黑体';
  973. xlsCell := aXlsPage.AddCell(3, FRow, '清单数量');
  974. xlsCell.HTextAlign := htaCenter;
  975. xlsCell.Font.Size := 10;
  976. xlsCell.Font.Style := [fsBold];
  977. xlsCell.Font.Name := '黑体';
  978. xlsCell := aXlsPage.AddCell(4, FRow, '单价');
  979. xlsCell.HTextAlign := htaCenter;
  980. xlsCell.Font.Size := 10;
  981. xlsCell.Font.Style := [fsBold];
  982. xlsCell.Font.Name := '黑体';
  983. xlsCell := aXlsPage.AddCell(5, FRow, '合价');
  984. xlsCell.HTextAlign := htaCenter;
  985. xlsCell.Font.Size := 10;
  986. xlsCell.Font.Style := [fsBold];
  987. xlsCell.Font.Name := '黑体';
  988. xlsCell := aXlsPage.AddCell(6, FRow, '备注');
  989. xlsCell.HTextAlign := htaCenter;
  990. xlsCell.Font.Size := 10;
  991. xlsCell.Font.Style := [fsBold];
  992. xlsCell.Font.Name := '黑体';
  993. end;
  994. { TFlatGclExcelExportor }
  995. procedure TFlatGclExcelExportor.AddCellCenterAlign(
  996. AXlsPage: TXlsCustomPage; ACol, ARow: Integer; AValue: Variant);
  997. var
  998. xlsCell: TXlsCustomCell;
  999. begin
  1000. xlsCell := nil;
  1001. xlsCell := AXlsPage.AddCell(ACol, ARow, AValue);
  1002. xlsCell.HTextAlign := htaCenter;
  1003. SetCommonCellFont(xlsCell);
  1004. end;
  1005. procedure TFlatGclExcelExportor.AddCellExceptZero(AXlsPage: TXlsCustomPage;
  1006. ACol, ARow: Integer; AValue: Variant);
  1007. var
  1008. xlsCell: TXlsCustomCell;
  1009. begin
  1010. xlsCell := nil;
  1011. case VarType(AValue) of
  1012. varSmallInt, varInteger, varSingle, varDouble,
  1013. varCurrency, varShortInt, varByte, varWord,
  1014. varLongWord, varInt64:
  1015. begin
  1016. if AValue <> 0 then
  1017. begin
  1018. xlsCell := AXlsPage.AddCell(ACol, ARow, AValue);
  1019. xlsCell.HTextAlign := htaRight;
  1020. end;
  1021. end
  1022. else xlsCell := AXlsPage.AddCell(ACol, ARow, AValue);
  1023. end;
  1024. SetCommonCellFont(xlsCell);
  1025. end;
  1026. procedure TFlatGclExcelExportor.AddCellRightAlign(AXlsPage: TXlsCustomPage;
  1027. ACol, ARow: Integer; AValue: Variant);
  1028. var
  1029. xlsCell: TXlsCustomCell;
  1030. begin
  1031. xlsCell := nil;
  1032. xlsCell := AXlsPage.AddCell(ACol, ARow, AValue);
  1033. xlsCell.HTextAlign := htaRight;
  1034. SetCommonCellFont(xlsCell);
  1035. end;
  1036. function TFlatGclExcelExportor.CheckPegStr(const AStr: string): Boolean;
  1037. function GetPosition(const AName, AStr, AStrSpare: string): Integer;
  1038. begin
  1039. Result := Pos(AStr, AName);
  1040. if Result = 0 then
  1041. Result := Pos(AStrSpare, AName);
  1042. end;
  1043. var
  1044. iPosK, iPosPlus: Integer;
  1045. fNum: Double;
  1046. begin
  1047. Result := False;
  1048. iPosK := GetPosition(AStr, 'K', 'k');
  1049. iPosPlus := GetPosition(AStr, '+', '+');
  1050. if (iPosK = 0) or (iPosPlus = 0) or (iPosPlus < iPosK) then Exit;
  1051. Result := TryStrToFloat(Copy(AStr, iPosK + 1, iPosPlus - iPosK - 1), fNum);
  1052. end;
  1053. constructor TFlatGclExcelExportor.Create(ABillsData: TDMDataBase);
  1054. begin
  1055. FRowIndex := 0;
  1056. FXlsOutPut := TXlsOutPut.Create;
  1057. FBillsData := ABillsData;
  1058. FCodeCol := 0;
  1059. FNameCol := 1;
  1060. FUnitsCol := 2;
  1061. FQuantityCol := 3;
  1062. FPriceCol := 4;
  1063. FPegCol := 5;
  1064. FProCol := 6;
  1065. FSubProCol := 7;
  1066. FSubSubProCol := 8;
  1067. FPositonCol := 9;
  1068. FDrawingCol := 10;
  1069. end;
  1070. destructor TFlatGclExcelExportor.Destroy;
  1071. begin
  1072. FXlsOutPut.Free;
  1073. inherited;
  1074. end;
  1075. procedure TFlatGclExcelExportor.ExportData(AXlsPage: TXlsCustomPage;
  1076. ANode: TZjIDTreeNode);
  1077. begin
  1078. with FBillsData do
  1079. begin
  1080. if not cdsBills.FindKey([ANode.ID]) then Exit;
  1081. if cdsBillsB_Code.AsString = '' then Exit;
  1082. AddCellExceptZero(AXlsPage, FCodeCol, FRowIndex, cdsBillsB_Code.AsString);
  1083. AddCellExceptZero(AXlsPage, FNameCol, FRowIndex, cdsBillsName.AsString);
  1084. AddCellCenterAlign(AXlsPage, FUnitsCol, FRowIndex, cdsBillsUnits.AsString);
  1085. AddCellExceptZero(AXlsPage, FQuantityCol, FRowIndex, cdsBillsQuantity.AsFloat);
  1086. AddCellExceptZero(AXlsPage, FPriceCol, FRowIndex, cdsBillsUnitPrice.AsFloat);
  1087. // cdsBills当前节点会改变
  1088. AddCellExceptZero(AXlsPage, FPegCol, FRowIndex, GetPeg(ANode.Parent));
  1089. AddCellExceptZero(AXlsPage, FProCol, FRowIndex, GetPro(ANode));
  1090. AddCellExceptZero(AXlsPage, FSubProCol, FRowIndex, GetSubPro(ANode));
  1091. AddCellExceptZero(AXlsPage, FSubSubProCol, FRowIndex, GetSubSubPro(ANode));
  1092. AddCellExceptZero(AXlsPage, FPositonCol, FRowIndex, GetPositon(ANode));
  1093. AddCellExceptZero(AXlsPage, FDrawingCol, FRowIndex, GetDrawingCode(ANode));
  1094. Inc(FRowIndex);
  1095. end;
  1096. end;
  1097. procedure TFlatGclExcelExportor.ExportFile(const AFileName: string);
  1098. begin
  1099. ExportTo(FXlsOutPut.AddPage);
  1100. FXlsOutPut.SaveToFile(AFileName);
  1101. end;
  1102. procedure TFlatGclExcelExportor.ExportNode(AXlsPage: TXlsCustomPage;
  1103. ANode: TZjIDTreeNode);
  1104. begin
  1105. if not Assigned(ANode) then Exit;
  1106. ExportData(AXlsPage, ANode);
  1107. if ANode.HasChildren then
  1108. ExportNode(AXlsPage, ANode.FirstChild);
  1109. ExportNode(AXlsPage, ANode.NextSibling);
  1110. end;
  1111. procedure TFlatGclExcelExportor.ExportTo(AXlsPage: TXlsCustomPage);
  1112. begin
  1113. InitialPageHead(AXlsPage);
  1114. ExportNode(AXlsPage, FBillsData.BillsTree.FirstNode.FirstChild);
  1115. end;
  1116. function TFlatGclExcelExportor.GetDrawingCode(
  1117. ANode: TZjIDTreeNode): string;
  1118. begin
  1119. Result := '';
  1120. if not Assigned(ANode) then Exit;
  1121. if FBillsData.cdsBills.FindKey([ANode.ID]) then
  1122. if FBillsData.cdsBillsDrawingCode.AsString <> '' then
  1123. Result := FBillsData.cdsBillsDrawingCode.AsString
  1124. else
  1125. Result := GetDrawingCode(ANode.Parent);
  1126. end;
  1127. function TFlatGclExcelExportor.GetPeg(ANode: TZjIDTreeNode): string;
  1128. begin
  1129. Result := '';
  1130. if not Assigned(ANode) then Exit;
  1131. if FBillsData.cdsBills.FindKey([ANode.ID]) then
  1132. if CheckPegStr(FBillsData.cdsBillsName.AsString) then
  1133. Result := FBillsData.cdsBillsName.AsString
  1134. else
  1135. Result := GetPeg(ANode.Parent);
  1136. end;
  1137. function TFlatGclExcelExportor.GetPositon(ANode: TZjIDTreeNode): string;
  1138. function GetFirstXmjParent(AChild: TZjIDTreeNode): TZjIDTreeNode;
  1139. begin
  1140. Result := AChild;
  1141. if not Assigned(ANode) then Exit;
  1142. if FBillsData.cdsBills.FindKey([AChild.ID]) then
  1143. if FBillsData.cdsBillsCode.AsString <> '' then
  1144. Result := AChild
  1145. else
  1146. Result := GetFirstXmjParent(AChild.Parent);
  1147. end;
  1148. var
  1149. AFirstXmjParent: TZjIDTreeNode;
  1150. begin
  1151. Result := '';
  1152. if not Assigned(ANode) then Exit;
  1153. // 若首先找到的项目节(项目节编号有数值)的名称中有桩号,则留空
  1154. AFirstXmjParent := GetFirstXmjParent(ANode);
  1155. if FBillsData.cdsBills.FindKey([AFirstXmjParent.ID])
  1156. and CheckPegStr(FBillsData.cdsBillsName.AsString) then Exit;
  1157. // 若首先找到的项目节为第二层或第三层(或者判断小于第四层),则留空
  1158. if AFirstXmjParent.Level < 4 then Exit;
  1159. // 否则显示项目节名称
  1160. if FBillsData.cdsBills.FindKey([AFirstXmjParent.ID]) then
  1161. Result := FBillsData.cdsBillsName.AsString;
  1162. end;
  1163. function TFlatGclExcelExportor.GetPro(ANode: TZjIDTreeNode): string;
  1164. begin
  1165. Result := '';
  1166. if not Assigned(ANode) then Exit;
  1167. // 取树结构的第二层节点的名称(level从0开始)
  1168. if ANode.Level > 1 then
  1169. Result := GetPro(ANode.Parent)
  1170. else
  1171. if FBillsData.cdsBills.FindKey([ANode.ID])
  1172. and (FBillsData.cdsBillsCode.AsString <> '') then
  1173. Result := FBillsData.cdsBillsName.AsString;
  1174. end;
  1175. function TFlatGclExcelExportor.GetSubPro(ANode: TZjIDTreeNode): string;
  1176. begin
  1177. Result := '';
  1178. if not Assigned(ANode) then Exit;
  1179. // 取树结构的第三层节点的名称
  1180. if ANode.Level > 2 then
  1181. Result := GetSubPro(ANode.Parent)
  1182. else
  1183. if FBillsData.cdsBills.FindKey([ANode.ID])
  1184. and (FBillsData.cdsBillsCode.AsString <> '') then
  1185. Result := FBillsData.cdsBillsName.AsString;
  1186. end;
  1187. function TFlatGclExcelExportor.GetSubSubPro(ANode: TZjIDTreeNode): string;
  1188. begin
  1189. Result := '';
  1190. if not Assigned(ANode) then Exit;
  1191. // 取树结构的第四层节点的名称
  1192. if ANode.Level > 3 then
  1193. Result := GetSubSubPro(ANode.Parent)
  1194. else
  1195. if FBillsData.cdsBills.FindKey([ANode.ID])
  1196. and (FBillsData.cdsBillsCode.AsString <> '') then
  1197. Result := FBillsData.cdsBillsName.AsString;
  1198. end;
  1199. procedure TFlatGclExcelExportor.InitHeadCell(AXlsPage: TXlsCustomPage;
  1200. ACol, ARow: Integer; const AText: string);
  1201. var
  1202. xlsCell: TXlsCustomCell;
  1203. begin
  1204. xlsCell := AXlsPage.AddCell(ACol, ARow, AText);
  1205. xlsCell.HTextAlign := htaCenter;
  1206. xlsCell.Font.Name := '黑体';
  1207. xlsCell.Font.Size := 10;
  1208. xlsCell.Font.Style := [fsBold];
  1209. end;
  1210. procedure TFlatGclExcelExportor.InitialPageHead(AXlsPage: TXlsCustomPage);
  1211. begin
  1212. InitHeadCell(AXlsPage, FCodeCol, FRowIndex, '清单编号');
  1213. InitHeadCell(AXlsPage, FNameCol, FRowIndex, '名称');
  1214. InitHeadCell(AXlsPage, FUnitsCol, FRowIndex, '单位');
  1215. InitHeadCell(AXlsPage, FQuantityCol, FRowIndex, '工程量');
  1216. InitHeadCell(AXlsPage, FPriceCol, FRowIndex, '单价');
  1217. InitHeadCell(AXlsPage, FPegCol, FRowIndex, '桩号及位置');
  1218. InitHeadCell(AXlsPage, FProCol, FRowIndex, '单位工程');
  1219. InitHeadCell(AXlsPage, FSubProCol, FRowIndex, '分部工程');
  1220. InitHeadCell(AXlsPage, FSubSubProCol, FRowIndex, '分项工程');
  1221. InitHeadCell(AXlsPage, FPositonCol, FRowIndex, '部位');
  1222. InitHeadCell(AXlsPage, FDrawingCol, FRowIndex, '图号');
  1223. AXlsPage.Widths[FCodeCol] := 72;
  1224. AXlsPage.Widths[FNameCol] := 188;
  1225. AXlsPage.Widths[FUnitsCol] := 40;
  1226. AXlsPage.Widths[FQuantityCol] := 72;
  1227. AXlsPage.Widths[FPriceCol] := 62;
  1228. AXlsPage.Widths[FPegCol] := 160;
  1229. AXlsPage.Widths[FProCol] := 100;
  1230. AXlsPage.Widths[FSubProCol] := 100;
  1231. AXlsPage.Widths[FSubSubProCol] := 100;
  1232. AXlsPage.Widths[FPositonCol] := 100;
  1233. AXlsPage.Widths[FDrawingCol] := 100;
  1234. Inc(FRowIndex);
  1235. end;
  1236. procedure TFlatGclExcelExportor.SetCommonCellFont(
  1237. AXlsCell: TXlsCustomCell);
  1238. begin
  1239. if Assigned(AXlsCell) then
  1240. begin
  1241. AXlsCell.Font.Name := 'SmartSimsun';
  1242. AXlsCell.Font.Size := 9;
  1243. end;
  1244. end;
  1245. end.