Delphi TXLSReadWriteII導出Excel


TXLSReadWriteII導出Excle (有點復雜,可以自己簡化一下,直接從項目中抓取的)

 

procedure TformSubReport.DataToExcel(_Item: Integer; _Obj: TObject);                         //導出Excle

var

  i, j, k: Integer;

  aVendorObj: TVendor;

  aEnterpriseObj: TEnterprise;

  aXlsObj: TXLSReadWriteII;

  aFileName: AnsiString;

  aCarRec: PCarRec;

  aOnLine: AnsiString;

  aRecordCount: Integer;                                                       //Excel數據條數

  aEnterpriseCount: Integer;                                                   //統計企業有多少行 + 合計一行

  aBookmark: Integer;                                                          //記錄運營商的起始位置

  aMergedCell: TMergedCell;

  aFontIndex: Integer;

  aStampTime: AnsiString;

  aVendorName: AnsiString;

begin

  if _Item = 1 then

    _Obj := nil;

 

  aRecordCount := 0;

  aEnterpriseCount := 0;

 

  SaveDialog1.InitialDir := ExtractFilePath(ParamStr(0));

  SaveDialog1.DefaultExt := 'xls';

  SaveDialog1.Filter := 'Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*';

  aStampTime := FormatDateTime('yyyymmddhhnnss', Now);

  SaveDialog1.FileName := aStampTime;

 

  if not SaveDialog1.Execute then

    Exit;

 

  aFileName := SaveDialog1.FileName;

  if aFileName = '' then

    Exit;

 

  aXlsObj := TXLSReadWriteII.Create(nil);

  try

    aXlsObj.Sheets[0].NameWideString('統計報表' + aStampTime);                 //工作表命名

 

    with aXlsObj.Formats.Add do                                                //定義格式一

    begin

      aFontIndex := aXlsObj.Fonts.AddIndex;

      aXlsObj.Fonts[aFontIndex].Name := '黑體';

      aXlsObj.Fonts[aFontIndex].Size := 14;

      aXlsObj.Fonts[aFontIndex].Color := xcRed;                                //字體顏色無效。shit

 

      HorizAlignment := chaCenter;

      VertAlignment := cvaCenter;

      FillPatternForeColor := xcLilac;

    end;

 

    with aXlsObj.Formats.Add do                                                //定義格式二

    begin

      HorizAlignment := chaCenter;

      VertAlignment := cvaCenter;

    end;

 

    case _item of

      1:begin

          aXlsObj.Sheets[0].WriteWideString(0, 0, 1, '運營商名稱');

          aXlsObj.Sheets[0].WriteWideString(1, 0, 1, '企業名稱');

          aXlsObj.Sheets[0].WriteWideString(2, 0, 1, '軌跡數總數');

          aXlsObj.Sheets[0].WriteWideString(3, 0, 1, '超速報警總數');

          aXlsObj.Sheets[0].WriteWideString(4, 0, 1, '車輛總數');

          aXlsObj.Sheets[0].WriteWideString(5, 0, 1, '在線車輛總數');

          aXlsObj.Sheets[0].WriteWideString(6, 0, 1, '在線率(%)');

        end;

    else

      begin

        aXlsObj.Sheets[0].WriteWideString(0, 0, 1, '序號');

        aXlsObj.Sheets[0].WriteWideString(1, 0, 1, '運營商名稱');

        aXlsObj.Sheets[0].WriteWideString(2, 0, 1, '企業名稱');

        aXlsObj.Sheets[0].WriteWideString(3, 0, 1, '定位時間');

        aXlsObj.Sheets[0].WriteWideString(4, 0, 1, '車牌號碼');

        aXlsObj.Sheets[0].WriteWideString(5, 0, 1, '軌跡數');

        aXlsObj.Sheets[0].WriteWideString(6, 0, 1, '超速報警數');

        aXlsObj.Sheets[0].WriteWideString(7, 0, 1, '疲勞駕駛報警數');

        aXlsObj.Sheets[0].WriteWideString(8, 0, 1, '是否在線');

      end;

    end;

 

    if _Obj = nil then

    begin

      for i := 0 to FVendorHash.Count - 1 do

      begin

        aVendorObj := FVendorHash[i];

        if aVendorObj = nil then Continue;

 

        aBookmark := aEnterpriseCount;

        aEnterpriseCount := 0;

 

        for j := 0 to aVendorObj.EnterpriseHash.Count - 1 do

        begin

          aEnterpriseObj := aVendorObj.EnterpriseHash[j];

          if aEnterpriseObj = nil then Continue;

 

          if _Item = 1 then

          begin

            inc(aRecordCount);

            inc(aEnterpriseCount);

 

            aXlsObj.Sheets[0].WriteWideString(0, aRecordCount, 2, aVendorObj.DataRec.VendorName);

            aXlsObj.Sheets[0].AsWideString[1, aRecordCount] := aEnterpriseObj.DataRec.EnterpriseName;

            aXlsObj.Sheets[0].AsFloat[2, aRecordCount] := aEnterpriseObj.TotalInfo.TotalGpsCount;

            aXlsObj.Sheets[0].AsFloat[3, aRecordCount] := aEnterpriseObj.TotalInfo.TotalOverAlarm;

            aXlsObj.Sheets[0].AsFloat[4, aRecordCount] := aEnterpriseObj.TotalInfo.TotalCarCount;

            aXlsObj.Sheets[0].AsFloat[5, aRecordCount] := aEnterpriseObj.TotalInfo.TotalOnLine;

            aXlsObj.Sheets[0].AsFloat[6, aRecordCount] := aEnterpriseObj.TotalInfo.OnlineRate;

          end

          else

          begin

            for k := 0 to aEnterpriseObj.CarHash.Count - 1 do

            begin

              aCarRec := aEnterpriseObj.CarHash[k];

 

              if _Item = 2 then //在線

              begin

                if not aCarRec.IsLine then Continue;

 

                aOnLine := '在線';

              end

              else if _Item = 3 then //離線

              begin

                if aCarRec.IsLine then Continue;

 

                aOnLine := '離線';

              end

              else if _Item = 4 then //所有

              begin

                if aCarRec.IsLine then

                  aOnLine := '在線'

                else

                  aOnLine := '離線';

              end;

 

              inc(aRecordCount);

              inc(aEnterpriseCount);

              aXlsObj.Sheets[0].AsInteger[0, aRecordCount] := aRecordCount;

              aXlsObj.Sheets[0].AsWideString[1, aRecordCount] := aVendorObj.DataRec.VendorName;

              aXlsObj.Sheets[0].AsWideString[2, aRecordCount] := aEnterpriseObj.DataRec.EnterpriseName;

              aXlsObj.Sheets[0].AsWideString[3, aRecordCount] := DateTimeToStr(aCarRec.ReportDate);

              aXlsObj.Sheets[0].AsWideString[4, aRecordCount] := aCarRec.RegistrationNO;

              aXlsObj.Sheets[0].AsFloat[5, aRecordCount] := aCarRec.GpsCount;

              aXlsObj.Sheets[0].AsFloat[6, aRecordCount] := aCarRec.OverSpeedAlarmCount;

              aXlsObj.Sheets[0].AsFloat[7, aRecordCount] := aCarRec.DriverFatigueAlarmCount;

              aXlsObj.Sheets[0].AsWideString[8, aRecordCount] := aOnLine;

            end;

          end;

        end;

 

        if _Item = 1 then                                   //小計

        begin

          Inc(aRecordCount);

          Inc(aEnterpriseCount);

 

          aXlsObj.Sheets[0].WriteWideString(0, aRecordCount, 2, aVendorObj.DataRec.VendorName);

          aXlsObj.Sheets[0].WriteWideString(1, aRecordCount, 2, '小計');

          aXlsObj.Sheets[0].AsFloat[2, aRecordCount] := aVendorObj.TotalInfo.TotalGpsCount;

          aXlsObj.Sheets[0].AsFloat[3, aRecordCount] := aVendorObj.TotalInfo.TotalOverAlarm;

          aXlsObj.Sheets[0].AsFloat[4, aRecordCount] := aVendorObj.TotalInfo.TotalCarCount;

          aXlsObj.Sheets[0].AsFloat[5, aRecordCount] := aVendorObj.TotalInfo.TotalOnLine;

          aXlsObj.Sheets[0].AsFloat[6, aRecordCount] := aVendorObj.TotalInfo.OnlineRate;

 

          aMergedCell := aXlsObj.Sheets[0].MergedCells.Add;

          aMergedCell.Col1 := 0;

          aMergedCell.Row1 := aBookmark + 1;

          aMergedCell.Col2 := 0;

          aMergedCell.Row2 := aBookmark + aEnterpriseCount;                    //這里的值是對應Excel中最后一行的地址

        end;

      end;

    end

    else if _Obj is TVendor then

    begin

      for j := 0 to TVendor(_Obj).EnterpriseHash.Count - 1 do

      begin

        aEnterpriseObj := TVendor(_Obj).EnterpriseHash[j];

        if aEnterpriseObj = nil then Continue;

 

        for k := 0 to aEnterpriseObj.CarHash.Count - 1 do

        begin

          aCarRec := aEnterpriseObj.CarHash[k];

 

          if _Item = 2 then //在線

          begin

            if not aCarRec.IsLine then Continue;

 

            aOnLine := '在線';

          end

          else if _Item = 3 then //離線

          begin

            if aCarRec.IsLine then Continue;

 

            aOnLine := '離線';

          end

          else if _Item = 4 then //所有

          begin

            if aCarRec.IsLine then

              aOnLine := '在線'

            else

              aOnLine := '離線';

          end;

 

          inc(aRecordCount);

          aXlsObj.Sheets[0].AsInteger[0, aRecordCount] := aRecordCount;

          aXlsObj.Sheets[0].AsWideString[1, aRecordCount] := TVendor(_Obj).DataRec.VendorName;

          aXlsObj.Sheets[0].AsWideString[2, aRecordCount] := aEnterpriseObj.DataRec.EnterpriseName;

          aXlsObj.Sheets[0].AsWideString[3, aRecordCount] := DateTimeToStr(aCarRec.ReportDate);

          aXlsObj.Sheets[0].AsWideString[4, aRecordCount] := aCarRec.RegistrationNO;

          aXlsObj.Sheets[0].AsFloat[5, aRecordCount] := aCarRec.GpsCount;

          aXlsObj.Sheets[0].AsFloat[6, aRecordCount] := aCarRec.OverSpeedAlarmCount;

          aXlsObj.Sheets[0].AsFloat[7, aRecordCount] := aCarRec.DriverFatigueAlarmCount;

          aXlsObj.Sheets[0].AsWideString[8, aRecordCount] := aOnLine;

        end;

      end;

    end

    else if _Obj is TEnterprise then

    begin

      aVendorObj := FVendorHash.ValueOf(IntToStr(TEnterprise(_Obj).DataRec.VendorID));

      if aVendorObj = nil then

        Exit;

      aVendorName := aVendorObj.DataRec.VendorName;

 

      for k := 0 to TEnterprise(_Obj).CarHash.Count - 1 do

      begin

        aCarRec := TEnterprise(_Obj).CarHash[k];

 

        if _Item = 2 then                         //在線

        begin

          if not aCarRec.IsLine then Continue;

 

          aOnLine := '在線';

        end

        else if _Item = 3 then                    //離線

        begin

          if aCarRec.IsLine then Continue;

 

          aOnLine := '離線';

        end

        else if _Item = 4 then                    //所有

        begin

          if aCarRec.IsLine then

            aOnLine := '在線'

          else

            aOnLine := '離線';

        end;

 

        inc(aRecordCount);

        aXlsObj.Sheets[0].AsInteger[0, aRecordCount] := aRecordCount;

        aXlsObj.Sheets[0].AsWideString[1, aRecordCount] := aVendorName;

        aXlsObj.Sheets[0].AsWideString[2, aRecordCount] := TEnterprise(_Obj).DataRec.EnterpriseName;

        aXlsObj.Sheets[0].AsWideString[3, aRecordCount] := DateTimeToStr(aCarRec.ReportDate);

        aXlsObj.Sheets[0].AsWideString[4, aRecordCount] := aCarRec.RegistrationNO;

        aXlsObj.Sheets[0].AsFloat[5, aRecordCount] := aCarRec.GpsCount;

        aXlsObj.Sheets[0].AsFloat[6, aRecordCount] := aCarRec.OverSpeedAlarmCount;

        aXlsObj.Sheets[0].AsFloat[7, aRecordCount] := aCarRec.DriverFatigueAlarmCount;

        aXlsObj.Sheets[0].AsWideString[8, aRecordCount] := aOnLine;

      end;

    end;

 

    if (_Item = 1) and (_Obj = nil) then                                       //匯總

    begin

      Inc(aRecordCount);

      aXlsObj.Sheets[0].WriteWideString(0, aRecordCount, 2, '匯總');

      aXlsObj.Sheets[0].AsInteger[1, aRecordCount] := FTotalEnterpriseCount;

      aXlsObj.Sheets[0].AsFloat[2, aRecordCount] := FTotalInfo.TotalGpsCount;

      aXlsObj.Sheets[0].AsFloat[3, aRecordCount] := FTotalInfo.TotalOverAlarm;

      aXlsObj.Sheets[0].AsFloat[4, aRecordCount] := FTotalInfo.TotalCarCount;

      aXlsObj.Sheets[0].AsFloat[5, aRecordCount] := FTotalInfo.TotalOnLine;

      aXlsObj.Sheets[0].AsFloat[6, aRecordCount] := FTotalInfo.OnlineRate;

    end;

 

    aXlsObj.Filename := aFileName;

    aXlsObj.Write;

    Information(Format('導出文件'+#13#10+'%s'+#13#10 +'成功!', [aFileName]));

  finally

    aXlsObj.Free;

  end;

end;

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM