瀏覽代碼

Task #930 导入Excel数据时,应过滤单元格数据的前后空格

MaiXinRong 9 年之前
父節點
當前提交
4edfcf6379
共有 2 個文件被更改,包括 46 次插入45 次删除
  1. 3 3
      Units/DetailExcelImport.pas
  2. 43 42
      Units/ExcelImport.pas

+ 3 - 3
Units/DetailExcelImport.pas

@@ -212,8 +212,8 @@ begin
       vGclNode.Units := sUnits;
       vGclNode.Quantity := StrToFloatDef(GetCellValue(AXlsFile, FCurRow, FQuantityCol), 0);
       vGclNode.Price := fPrice;
-      vGclNode.DrawingCode := GetCellValue(AXlsFile, FCurRow, FDrawingCol);
-      vGclNode.MemoStr := GetCellValue(AXlsFile, FCurRow, FMemoCol);
+      vGclNode.DrawingCode := Trim(GetCellValue(AXlsFile, FCurRow, FDrawingCol));
+      vGclNode.MemoStr := Trim(GetCellValue(AXlsFile, FCurRow, FMemoCol));
     end
     else
       vGclNode.Quantity := vGclNode.Quantity + StrToFloatDef(GetCellValue(AXlsFile, FCurRow, FQuantityCol), 0);
@@ -248,7 +248,7 @@ begin
   begin
     for iCol := 1 to AXlsFile.MaxCol do
     begin
-      sColName := GetCellValue(AXlsFile, FCurRow, iCol);
+      sColName := Trim(GetCellValue(AXlsFile, FCurRow, iCol));
       if sColName = 'µÚ1²ã' then
         FXmjLevel1Col := iCol
       else if sColName = 'µÚ2²ã' then

+ 43 - 42
Units/ExcelImport.pas

@@ -19,6 +19,7 @@ type
     procedure Import; virtual; abstract;
 
     function GetCellValue(ASheet: TSpreadSheet; ACol, ARow: Integer): Variant;
+    function GetCellTrimText(ASheet: TSpreadSheet; ACol, ARow: Integer): string;
   public
     constructor Create(AProjectData: TProjectData);
     destructor Destroy; override;
@@ -186,6 +187,12 @@ begin
   inherited;
 end;
 
+function TExcelImport.GetCellTrimText(ASheet: TSpreadSheet; ACol,
+  ARow: Integer): string;
+begin
+  Result := Trim(VarToStrDef(GetCellValue(ASheet, ACol, ARow), ''));
+end;
+
 function TExcelImport.GetCellValue(ASheet: TSpreadSheet;
   ACol, ARow: Integer): Variant;
 begin
@@ -278,19 +285,16 @@ var
   Node: TBillsCacheNode;
   vValue: Variant;
 begin
-  with ASheet.Cells do
-  begin
-    sLevelCode := VarToStrDef(GetValue(FLevelCol, FCurRow), '');
-    if sLevelCode = '' then Exit;
+  sLevelCode := GetCellTrimText(ASheet, FLevelCol, FCurRow);
+  if sLevelCode = '' then Exit;
 
-    iFixedID := StrToInt(VarToStrDef(GetValue(FFixedIDCol, FCurRow), '-1'));
-    Node := FCacheTree.AddNodeByCode(sLevelCode, iFixedID);
-    Node.Code := VarToStrDef(GetValue(FCodeCol, FCurRow), '');
-    Node.B_Code := VarToStrDef(GetValue(FB_CodeCol, FCurRow), '');
-    Node.Name := VarToStrDef(GetValue(FNameCol, FCurRow), '');
-    Node.Units := VarToStrDef(GetValue(FUnitsCol, FCurRow), '');
-    Node.CanDelete := VarToIntDef(GetValue(FCanDeleteCol, FCurRow), 0) = 0;
-  end;
+  iFixedID := StrToIntDef(GetCellTrimText(ASheet, FFixedIDCol, FCurRow), -1);
+  Node := FCacheTree.AddNodeByCode(sLevelCode, iFixedID);
+  Node.Code := GetCellTrimText(ASheet, FCodeCol, FCurRow);
+  Node.B_Code := GetCellTrimText(ASheet, FB_CodeCol, FCurRow);
+  Node.Name := GetCellTrimText(ASheet, FNameCol, FCurRow);
+  Node.Units := GetCellTrimText(ASheet, FUnitsCol, FCurRow);
+  Node.CanDelete := VarToIntDef(GetCellValue(ASheet, FCanDeleteCol, FCurRow), 0) = 0;
 end;
 
 procedure TBillsExcelImport.LoadNodes(ASheet: TSpreadSheet);
@@ -476,10 +480,10 @@ var
   vValue: Variant;
   iFixedID: Integer;
 begin
-  sLevelCode := VarToStrDef(GetCellValue(ASheet, FLevelCol, FCurRow), '');
-  sCode := VarToStrDef(GetCellValue(ASheet, FCodeCol, FCurRow), '');
-  sB_Code := VarToStrDef(GetCellValue(ASheet, FB_CodeCol, FCurRow), '');
-  sName := VarToStrDef(GetCellValue(ASheet, FNameCol, FCurRow), '');
+  sLevelCode := GetCellTrimText(ASheet, FLevelCol, FCurRow);
+  sCode := GetCellTrimText(ASheet, FCodeCol, FCurRow);
+  sB_Code := GetCellTrimText(ASheet, FB_CodeCol, FCurRow);
+  sName := GetCellTrimText(ASheet, FNameCol, FCurRow);
 
   // 含层次编号时,层次编号为空不导入
   // 不含层次编号时,仅导入第一部分,且项目节编号、清单编号均未空时不导入
@@ -523,16 +527,16 @@ begin
       FFixedIDNodes.Add(Node);
   end;
 
-  Node.Code := VarToStrDef(GetCellValue(ASheet, FCodeCol, FCurRow), '');
-  Node.B_Code := VarToStrDef(GetCellValue(ASheet, FB_CodeCol, FCurRow), '');
-  Node.Name := VarToStrDef(GetCellValue(ASheet, FNameCol, FCurRow), '');
-  Node.Units := VarToStrDef(GetCellValue(ASheet, FUnitsCol, FCurRow), '');
+  Node.Code := sCode;
+  Node.B_Code := sB_Code;
+  Node.Name := sName;
+  Node.Units := GetCellTrimText(ASheet, FUnitsCol, FCurRow);
   Node.Price := StrToFloatDef(VarToStrDef(GetCellValue(ASheet, FPriceCol, FCurRow), ''), 0);
   Node.Quantity := StrToFloatDef(VarToStrDef(GetCellValue(ASheet, FQuantityCol, FCurRow), ''), 0);
   Node.DgnQuantity1 := StrToFloatDef(VarToStrDef(GetCellValue(ASheet, FDgnQuantity1Col, FCurRow), ''), 0);
   Node.DgnQuantity2 := StrToFloatDef(VarToStrDef(GetCellValue(ASheet, FDgnQuantity2Col, FCurRow), ''), 0);
-  Node.DrawingCode := VarToStrDef(GetCellValue(ASheet, FDrawingCol, FCurRow), '');
-  Node.MemoStr := VarToStrDef(GetCellValue(ASheet, FMemoCol, FCurRow), '');
+  Node.DrawingCode := GetCellTrimText(ASheet, FDrawingCol, FCurRow);
+  Node.MemoStr := GetCellTrimText(ASheet, FMemoCol, FCurRow);
 end;
 
 procedure TBillsEdtExcelImport.LoadNodes(ASheet: TSpreadSheet);
@@ -645,7 +649,7 @@ begin
   UpdateProgressPosition(0);
   while (FCurRow < ASheet.Cells.UsedRowCount) do
   begin
-    sB_Code := VarToStrDef(ASheet.Cells.GetValue(FB_CodeCol, FCurRow), '');
+    sB_Code := GetCellTrimText(ASheet, FB_CodeCol, FCurRow);
     if (sB_Code <> '') and CheckIsBillsCode(sB_Code) then
     begin
       fPrice := StrToFloatDef(VarToStrDef(ASheet.Cells.GetValue(FPriceCol, FCurRow), ''), 0);
@@ -670,7 +674,7 @@ begin
   begin
     for iCol := 0 to ASheet.Cells.UsedColCount do
     begin
-      sColName := VarToStrDef(ASheet.Cells.GetValue(iCol, FCurRow), '');
+      sColName := GetCellTrimText(ASheet, iCol, FCurRow);
       if SameText(sColName, '清单编号') or SameText(sColName, '子目号') then
         FB_CodeCol := iCol
       else if SameText(sColName, '名称') then
@@ -736,7 +740,7 @@ begin
   begin
     for iCol := 0 to ASheet.Cells.UsedColCount do
     begin
-      sColName := VarToStrDef(ASheet.Cells.GetValue(iCol, FCurRow), '');
+      sColName := GetCellTrimText(ASheet, iCol, FCurRow);
       if SameText(sColName, '清单编号') or SameText(sColName, '子目号') then
         FB_CodeCol := iCol
       else if SameText(sColName, '名称') then
@@ -780,23 +784,20 @@ var
 begin
   while (FCurRow < ASheet.Cells.UsedRowCount) do
   begin
-    sB_Code := VarToStrDef(ASheet.Cells.GetValue(FB_CodeCol, FCurRow), '');
+    sB_Code := GetCellTrimText(ASheet, FB_CodeCol, FCurRow);
     if (sB_Code <> '') and CheckIsBillsCode(sB_Code) then
     begin
       Rec := FProjectData.DealBillsData.sddDealBills.Add;
       Rec.ValueByName('ID').AsInteger := FBillsID;
       Rec.ValueByName('B_Code').AsString := sB_Code;
-      with ASheet.Cells do
-      begin
-        Rec.ValueByName('Name').AsString := VarToStrDef(GetValue(FNameCol, FCurRow), '');
-        Rec.ValueByName('Units').AsString := VarToStrDef(GetValue(FUnitsCol, FCurRow), '');
-        Rec.ValueByName('Price').AsFloat := PriceRoundTo(
-            StrToFloatDef(VarToStrDef(GetValue(FPriceCol, FCurRow), ''), 0));
-        Rec.ValueByName('Quantity').AsFloat := QuantityRoundTo(
-            StrToFloatDef(VarToStrDef(GetValue(FQuantityCol, FCurRow), ''), 0));
-        Rec.ValueByName('TotalPrice').AsFloat := TotalPriceRoundTo(
-            StrToFloatDef(VarToStrDef(GetValue(FTotalPriceCol, FCurRow), ''), 0));
-      end;
+      Rec.ValueByName('Name').AsString := GetCellTrimText(ASheet, FNameCol, FCurRow);
+      Rec.ValueByName('Units').AsString := GetCellTrimText(ASheet, FUnitsCol, FCurRow);
+      Rec.ValueByName('Price').AsFloat := PriceRoundTo(
+          StrToFloatDef(GetCellTrimText(ASheet, FPriceCol, FCurRow), 0));
+      Rec.ValueByName('Quantity').AsFloat := QuantityRoundTo(
+          StrToFloatDef(GetCellTrimText(ASheet, FQuantityCol, FCurRow), 0));
+      Rec.ValueByName('TotalPrice').AsFloat := TotalPriceRoundTo(
+          StrToFloatDef(GetCellTrimText(ASheet, FTotalPriceCol, FCurRow), 0));
       Inc(FBillsID);
     end;
     Inc(FCurRow);
@@ -878,14 +879,14 @@ var
 begin
   with ASheet.Cells do
   begin
-    sB_Code := VarToStrDef(GetValue(FB_CodeCol, FCurRow), '');
-    sName := VarToStrDef(GetValue(FNameCol, FCurRow), '');
+    sB_Code := Trim(VarToStrDef(GetValue(FB_CodeCol, FCurRow), ''));
+    sName := Trim(VarToStrDef(GetValue(FNameCol, FCurRow), ''));
 
     Node := FCacheTree.AddNodeByData(sB_Code, sName);
 
-    Node.B_Code := VarToStrDef(GetValue(FB_CodeCol, FCurRow), '');
-    Node.Name := VarToStrDef(GetValue(FNameCol, FCurRow), '');
-    Node.Units := VarToStrDef(GetValue(FUnitsCol, FCurRow), '');
+    Node.B_Code := sB_Code;
+    Node.Name := sName;
+    Node.Units := Trim(VarToStrDef(GetValue(FUnitsCol, FCurRow), ''));
     Node.Price := StrToFloatDef(VarToStrDef(GetValue(FPriceCol, FCurRow), ''), 0);
     Node.Quantity := StrToFloatDef(VarToStrDef(GetValue(FQuantityCol, FCurRow), ''), 0);
   end;