ExcelImport_GclBills.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. unit ExcelImport_GclBills;
  2. // 导入工程量清单至项目节
  3. interface
  4. uses
  5. Classes, DetailExcelImport, MCacheTree, sdDB, OExport;
  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(ASheet: TExportWorkSheet);
  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, SysUtils;
  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 := 0;
  42. FNameCol := 1;
  43. FUnitsCol := 2;
  44. FQuantityCol := 3;
  45. FPriceCol := 4;
  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(OExport.OpenWorkSheet);
  75. WriteNodes(ProjectData.BillsData.sddBills);
  76. end;
  77. procedure TDEI_GclBills.ImportSheet(ASheet: TExportWorkSheet);
  78. var
  79. vRow: TExportRow;
  80. iPos, iRow: Integer;
  81. sB_Code, sName: string;
  82. Node: TGclCacheNode;
  83. begin
  84. for iRow := 1 to ASheet.Rows.Count do
  85. begin
  86. vRow := ASheet.Rows[iRow];
  87. sB_Code := GetCellTrimStr(vRow, FB_CodeCol);
  88. sName := GetCellTrimStr(vRow, FNameCol);
  89. if (sB_Code <> '') or (sName <> '') then
  90. begin
  91. Node := FCacheTree.AddNodeByData(sB_Code, sName);
  92. Node.B_Code := sB_Code;
  93. Node.Name := sName;
  94. Node.Units := GetCellTrimStr(vRow, FUnitsCol);
  95. Node.Price := GetCellFloat(vRow, FPriceCol);
  96. Node.Quantity := GetCellFloat(vRow, FQuantityCol);
  97. end;
  98. iPos := (iRow + 1) * 100 div ASheet.Rows.Count;
  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.