TXLSReadWriteII导出Excle (有点复杂,可以自己简化一下,直接从项目中抓取的)procedure TformSubReport.DataToExcel(_Item: Integer; _Obj: TObject); //导出Exclevari, 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;beginif _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 thenExit;aFileName := SaveDialog1.FileName;if aFileName = '' thenExit;aXlsObj := TXLSReadWriteII.Create(nil);tryaXlsObj.Sheets[0].NameWideString('统计报表' + aStampTime); //工作表命名with aXlsObj.Formats.Add do //定义格式一beginaFontIndex := aXlsObj.Fonts.AddIndex;aXlsObj.Fonts[aFontIndex].Name := '黑体';aXlsObj.Fonts[aFontIndex].Size := 14;aXlsObj.Fonts[aFontIndex].Color := xcRed; //字体颜色无效。shitHorizAlignment := chaCenter;VertAlignment := cvaCenter;FillPatternForeColor := xcLilac;end;with aXlsObj.Formats.Add do //定义格式二beginHorizAlignment := chaCenter;VertAlignment := cvaCenter;end;case _item of1:beginaXlsObj.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;elsebeginaXlsObj.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 thenbeginfor i := 0 to FVendorHash.Count - 1 dobeginaVendorObj := FVendorHash[i];if aVendorObj = nil then Continue;aBookmark := aEnterpriseCount;aEnterpriseCount := 0;for j := 0 to aVendorObj.EnterpriseHash.Count - 1 dobeginaEnterpriseObj := aVendorObj.EnterpriseHash[j];if aEnterpriseObj = nil then Continue;if _Item = 1 thenbegininc(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;endelsebeginfor k := 0 to aEnterpriseObj.CarHash.Count - 1 dobeginaCarRec := aEnterpriseObj.CarHash[k];if _Item = 2 then //在线beginif not aCarRec.IsLine then Continue;aOnLine := '在线';endelse if _Item = 3 then //离线beginif aCarRec.IsLine then Continue;aOnLine := '离线';endelse if _Item = 4 then //所有beginif aCarRec.IsLine thenaOnLine := '在线'elseaOnLine := '离线';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 //小计beginInc(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;endelse if _Obj is TVendor thenbeginfor j := 0 to TVendor(_Obj).EnterpriseHash.Count - 1 dobeginaEnterpriseObj := TVendor(_Obj).EnterpriseHash[j];if aEnterpriseObj = nil then Continue;for k := 0 to aEnterpriseObj.CarHash.Count - 1 dobeginaCarRec := aEnterpriseObj.CarHash[k];if _Item = 2 then //在线beginif not aCarRec.IsLine then Continue;aOnLine := '在线';endelse if _Item = 3 then //离线beginif aCarRec.IsLine then Continue;aOnLine := '离线';endelse if _Item = 4 then //所有beginif aCarRec.IsLine thenaOnLine := '在线'elseaOnLine := '离线';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;endelse if _Obj is TEnterprise thenbeginaVendorObj := FVendorHash.ValueOf(IntToStr(TEnterprise(_Obj).DataRec.VendorID));if aVendorObj = nil thenExit;aVendorName := aVendorObj.DataRec.VendorName;for k := 0 to TEnterprise(_Obj).CarHash.Count - 1 dobeginaCarRec := TEnterprise(_Obj).CarHash[k];if _Item = 2 then //在线beginif not aCarRec.IsLine then Continue;aOnLine := '在线';endelse if _Item = 3 then //离线beginif aCarRec.IsLine then Continue;aOnLine := '离线';endelse if _Item = 4 then //所有beginif aCarRec.IsLine thenaOnLine := '在线'elseaOnLine := '离线';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 //汇总beginInc(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]));finallyaXlsObj.Free;end;end;