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;

  

Delphi TXLSReadWriteII导出Excel的更多相关文章

  1. Delphi TXLSReadWriteII 导出EXCEL

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  2. Delphi DBGridEh导出Excel

    unit Unit_DBGridEhToExcel; interface uses SysUtils, Variants, Classes, Graphics, Controls, Forms, Ex ...

  3. Delphi+DBGrid导出Excel

    uses ComObj; //DBGrid:指定的DBGrid;SaveFileName:要保存的文件名 function ExportDBGrid(DBGrid: TDBGrid; SaveFile ...

  4. delphi cxgrid导出excel去除货币符号

    版本 : devexpress 13.1.4 打开 包在ExpressExportLibary目录中.  修改FCells.SetCellDataCurrency为FCells.SetCellData ...

  5. Delphi 数据导出到Excel

    好多办公软件特别是财务软件,都需要配备把数据导出到Excel,下面就来介绍两种数据导出方法 1.ADODB导出查询结果(此方法需要安装Excel) 2.二维表数据导出(根据Excel文件结构生成二进制 ...

  6. CxGrid导出Excel时清除颜色的设置

    CxGrid导出Excel时清除颜色的设置 (2011-04-25 16:33:23) 转载▼ 标签: it 分类: Delphi http://www.radxe.com/?p=170 cxgrid ...

  7. C#使用Aspose.Cells导出Excel简单实现

    首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...

  8. 利用poi导出Excel

    import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.r ...

  9. [django]数据导出excel升级强化版(很强大!)

    不多说了,原理采用xlwt导出excel文件,所谓的强化版指的是实现在网页上选择一定条件导出对应的数据 之前我的博文出过这类文章,但只是实现导出数据,这次左思右想,再加上网上的搜索,终于找出方法实现条 ...

随机推荐

  1. 剑指Offer 16. 合并两个排序的链表 (链表)

    题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 题目地址 https://www.nowcoder.com/practice/d8b6b4358 ...

  2. 01_Mybaits逆向工程maven版

    1.创建generatorSqlmapCustom工程 2.修改pom文件 <?xml version="1.0" encoding="UTF-8"?&g ...

  3. IC卡T0协议中的过程字与状态字

    T0协议中,IC卡收到命里头以后向TTL回传过程字或状态字SW1.SW2: TTL和IC卡在二者之间的命令和数据交换的任何时刻都必须知道数据流的方向和IO线路由谁驱动. 摘录参考:<中国金融集成 ...

  4. 转载 单例(Singleton)模式)的误区

    在创建型模式中,单例(Singleton)模式和原型(Prototype)模式相对来说其用意更为简单明了.单例(Singleton)模式确保某类只有一个实例,且自行实例化并向整个系统提供这个实例:原型 ...

  5. 【ShareCode】不错的技术文章 -- 如何使用异或(XOR)运算找到数组中缺失的数?

    如何使用异或(XOR)运算找到数组中缺失的数? 今天给大家分享一篇关于使用XOR(异或)运算找到数组中缺失的数的问题. 在一次Javascript面试中,有这么一个问题: 假设有一个由0到99(包含9 ...

  6. 初学版本控制更新Version control

    概述: 在学习计算机软件工程中,修订控制是跟踪和控制源代码更改的任何类型的实践. 对于软件开发人员有时会使用修订控制软件来维护文档和配置文件以及源代码. 当团队设计,开发和部署软件时,通常会将同一软件 ...

  7. jquery中使元素显示和隐藏方法之间的区别

    在实际的项目开发中,要使一个元素隐藏的方法有很多,比如css的多种属性和jquery的多种方法,虽然他们的作用都是使元素不可见,但是各个方法实现的原理是不一样的.下面主要介绍jquery各个元素隐藏方 ...

  8. PythonStudy——阶段总结

    每个数据类型的最大特点是什么? (1)int整型:用于存放整形对象,是不可变类型.若将一个整数赋值给一个变量名,python可自动将其设置为int型. 例如:age = 30 这里的age对象的typ ...

  9. 自定义pip 包开发简单说明

    文档主要来自官方文档,主要是为了测试学习 创建pip 包项目 项目结构 ├── LICENSE ├── README.md ├── dalongrong_example_pkg │ └── __ini ...

  10. 让docker容器开机启动

    网上有些文章说,要让docker 的容器自动在开机启动,是写脚本,比如在 rc.local 中写.其实完全没必要这么麻烦,docker 有相关指令,docker run 指令中加入 --restart ...