ExcelImport_GclBills.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. unit ExcelImport_GclBills;
  2. // 导入工程量清单至项目节
  3. interface
  4. uses
  5. Classes, DetailExcelImport, MCacheTree, sdDB;
  6. type
  7. TDEI_GclBills = class(TDetailExcelImport)
  8. private
  9. FParentID: Integer;
  10. FSelectSheets: TList;
  11. FCacheTree: TGclCacheTree;
  12. FCurRow: Integer;
  13. FB_CodeCol: Integer;
  14. FNameCol: Integer;
  15. FUnitsCol: Integer;
  16. FPriceCol: Integer;
  17. FQuantityCol: Integer;
  18. protected
  19. procedure BeginImport; override;
  20. procedure EndImport; override;
  21. procedure Import; override;
  22. procedure ImportSheet;
  23. procedure WriteNode(ADataSet: TsdDataSet; ANode: TGclCacheNode);
  24. procedure WriteNodes(ADataSet: TsdDataSet);
  25. public
  26. procedure ImportToXmj(const AFileName: string; AParentID: Integer);
  27. end;
  28. implementation
  29. uses
  30. Forms, mDataRecord, Controls, ProgressHintFrm, UtilMethods;
  31. { TDEI_GclBills }
  32. procedure TDEI_GclBills.BeginImport;
  33. begin
  34. Screen.Cursor := crHourGlass;
  35. ShowProgressHint('导入Excel数据', 100);
  36. FCacheTree := TGclCacheTree.Create;
  37. FCacheTree.NewNodeID := ProjectData.BillsData.GetMaxBillsID + 1;
  38. ProjectData.DisConnectTree;
  39. ProjectData.BillsData.DisableEvents;
  40. FSelectSheets := TList.Create;
  41. FB_CodeCol := 1;
  42. FNameCol := 2;
  43. FUnitsCol := 3;
  44. FQuantityCol := 4;
  45. FPriceCol := 5;
  46. end;
  47. procedure TDEI_GclBills.EndImport;
  48. var
  49. ParentRec: TsdDataRecord;
  50. begin
  51. FSelectSheets.Free;
  52. FCacheTree.Free;
  53. ProjectData.BillsData.EnableEvents;
  54. ProjectData.ReConnectTree;
  55. ParentRec := ProjectData.BillsData.sddBills.FindKey('idxID', FParentID);
  56. ProjectData.BillsCompileData.sdvBillsCompile.LocateInControl(ParentRec);
  57. ProjectData.BillsCompileData.CalculateAll;
  58. CloseProgressHint;
  59. Screen.Cursor := crDefault;
  60. end;
  61. procedure TDEI_GclBills.Import;
  62. var
  63. i: Integer;
  64. begin
  65. {if SelectSheets(FMSExcel, FSelectSheets) then
  66. begin
  67. for i := 0 to FSelectSheets.Count - 1 do
  68. begin
  69. UpdateProgressHint(Format('导入Excel数据--工作表[%s]', [FMSExcel.SheetNames.Strings[i]]));
  70. UpdateProgressPosition(0);
  71. ImportSheet(FSelectSheets.Items[i]);
  72. end;
  73. end;}
  74. ImportSheet;
  75. WriteNodes(ProjectData.BillsData.sddBills);
  76. end;
  77. procedure TDEI_GclBills.ImportSheet;
  78. var
  79. iPos: Integer;
  80. sB_Code, sName: string;
  81. Node: TGclCacheNode;
  82. begin
  83. FCurRow := 2;
  84. while (FCurRow <= Excel.XlsFile.MaxRow) do
  85. begin
  86. sB_Code := GetCellTrimStr(Excel.XlsFile, FCurRow, FB_CodeCol);
  87. sName := GetCellTrimStr(Excel.XlsFile, FCurRow, FNameCol);
  88. if (sB_Code <> '') or (sName <> '') then
  89. begin
  90. Node := FCacheTree.AddNodeByData(sB_Code, sName);
  91. Node.B_Code := sB_Code;
  92. Node.Name := sName;
  93. Node.Units := GetCellTrimStr(Excel.XlsFile, FCurRow, FUnitsCol);
  94. Node.Price := GetCellFloat(Excel.XlsFile, FCurRow, FPriceCol);
  95. Node.Quantity := GetCellFloat(Excel.XlsFile, FCurRow, FQuantityCol);
  96. end;
  97. Inc(FCurRow);
  98. iPos := FCurRow * 100 div Excel.XlsFile.MaxRow;
  99. UpdateProgressPosition(iPos);
  100. end;
  101. end;
  102. procedure TDEI_GclBills.ImportToXmj(const AFileName: string;
  103. AParentID: Integer);
  104. begin
  105. FParentID := AParentID;
  106. ImportFile(AFileName);
  107. end;
  108. procedure TDEI_GclBills.WriteNode(ADataSet: TsdDataSet;
  109. ANode: TGclCacheNode);
  110. var
  111. Rec: TBillsRecord;
  112. begin
  113. if ANode.B_Code <> '' then
  114. UpdateProgressHint('写入读取的Excel数据 ' + ANode.B_Code)
  115. else
  116. UpdateProgressHint('写入读取的Excel数据 ' + ANode.Name);
  117. Rec := TBillsRecord(ADataSet.Add);
  118. Rec.ID.AsInteger := ANode.ID;
  119. if ANode.ParentID = -1 then
  120. Rec.ParentID.AsInteger := FParentID
  121. else
  122. Rec.ParentID.AsInteger := ANode.ParentID;
  123. Rec.NextSiblingID.AsInteger := ANode.NextSiblingID;
  124. Rec.B_Code.AsString := ANode.B_Code;
  125. Rec.Name.AsString := ANode.Name;
  126. Rec.Units.AsString := ANode.Units;
  127. Rec.Price.AsFloat := PriceRoundTo(ANode.Price);
  128. Rec.OrgQuantity.AsFloat := QuantityRoundTo(ANode.Quantity);
  129. end;
  130. procedure TDEI_GclBills.WriteNodes(ADataSet: TsdDataSet);
  131. var
  132. i, iPos: Integer;
  133. begin
  134. UpdateProgressHint('写入读取的Excel数据');
  135. UpdateProgressPosition(0);
  136. for i := 0 to FCacheTree.CacheNodes.Count - 1 do
  137. begin
  138. WriteNode(ADataSet, TGclCacheNode(FCacheTree.CacheNodes[i]));
  139. iPos := i*100 div FCacheTree.CacheNodes.Count;
  140. UpdateProgressPosition(iPos);
  141. end;
  142. UpdateProgressPosition(100);
  143. end;
  144. end.