delphi 导出excel

Var
  FExcel:OleVariant; //excel应用程序
  FWorkBook :OleVariant; //工作表
  Temsheet:OleVariant; //工作薄
  FPicture:OleVariant;//图片
  tmpstr:String;
  range:variant;//范围
  i,j,TemInt:integer;
  TemFileName:String;
  real_american_price:Double;
begin
  if (kind = ‘小米清单‘) or (kind = ‘小商品清单‘) then
  Begin
    if cxxiaomilist.DataController.RecordCount = 0 then
    begin
      Application.MessageBox(PChar(‘没有导出数据!‘),‘提示‘,MB_OK + MB_ICONWARNING);
      abort;
    end;

    SaveDialog1.Filter:=‘.xlsx‘;
    if SaveDialog1.Execute then
    begin
      TemFileName:=SaveDialog1.FileName+‘.xlsx‘;

      Screen.Cursor:=CrHourGlass;
      TemInt:=0;
      try
        FExcel:= CreateoleObject(‘excel.Application‘);
        FWorkBook:=FExcel.WorkBooks.Add(-4167); //新的工作表
        Temsheet:=FWorkBook.Worksheets.Add;
        Temsheet.Name:=kind;
      except
        Application.MessageBox(‘无法创建Excel文件, 请确认是否安装了Excel软件‘,
          PChar(Application.Title), MB_OK + MB_ICONWarning);
        Exit;
      end;

      Temsheet.Select;
      Temsheet.Range[‘A1‘,‘A1‘].RowHeight := 60;

      for i := 1 to 111 do
      begin
        Temsheet.Columns[i].ColumnWidth:=15;
        Temsheet.cells[1,i] := ‘‘;
        if i = 1 then
          Temsheet.cells[1,i] := cxxiaomilist.Bands[111].Caption;
        if i = 22 then
          Temsheet.cells[1,i] := cxxiaomilist.Bands[112].Caption;
        Temsheet.cells[2,i] := cxxiaomilist.Bands[i-1].Caption;
        ////////////////////////////////////////////////////////////
        if i < 22 then
           Temsheet.cells[3,i] := cxxiaomilist.Columns[i-1].Caption;
      end;
      Temsheet.Columns[1].NumberFormat:=‘#####################‘;

      range:=Temsheet.Range[Temsheet.cells[3,22],Temsheet.cells[3,23]];//选定表格
      range.select;
      range.merge;
      range:=Temsheet.Range[Temsheet.cells[3,22],Temsheet.cells[3,24]];//选定表格
      range.select;
      range.merge;
      Temsheet.Cells[3,22].HorizontalAlignment:=-4108; //字居中
      Temsheet.Cells[3,22] := cxxiaomilist.Bands[112 + 1].Caption;

      range:=Temsheet.Range[Temsheet.cells[3,25],Temsheet.cells[3,26]];//选定表格
      range.select;
      range.merge;
      range:=Temsheet.Range[Temsheet.cells[3,25],Temsheet.cells[3,27]];//选定表格
      range.select;
      range.merge;
      Temsheet.Cells[3,25].HorizontalAlignment:=-4108; //字居中
      Temsheet.Cells[3,25] := cxxiaomilist.Bands[112 + 2].Caption;

      range:=Temsheet.Range[Temsheet.cells[3,109],Temsheet.cells[3,110]];//选定表格
      range.select;
      range.merge;
      range:=Temsheet.Range[Temsheet.cells[3,109],Temsheet.cells[3,111]];//选定表格
      range.select;
      range.merge;
      Temsheet.Cells[3,109].HorizontalAlignment:=-4108; //字居中
      Temsheet.Cells[3,109] := cxxiaomilist.Bands[112 + 30].Caption;

      for I := 0 to cxxiaomilist.DataController.RecordCount - 1 do
      begin
        for j := 1 to cxxiaomilist.ColumnCount do
        begin
          Temsheet.cells[4 + i,j] := VarToStr(cxxiaomilist.DataController.GetValue(i,j-1));
//          if j = 10 then
//          if TryStrToFloat(VarToStr(cxxiaomilist.DataController.GetValue(i,j)),real_american_price) then
//          if real_american_price > 150 then
//          begin
//            Temsheet.Cells[4 + i,j].Interior.Color := clRed;
//            Temsheet.Cells[4 + i,j + 4].Interior.Color := $0075C76B;
//          end;
        end;
      end;

  //    for i:=0 to DBGrid2.Columns.Count - 1 do
  //    begin
  //      Temsheet.Cells[TemInt,i+2]:=DBGrid2.Columns[i].Title.Caption;
  //      Temsheet.Cells[TemInt,i+2].HorizontalAlignment:=-4108; //字居中
  //      Temsheet.Cells[TemInt,i+2].Interior.Color:=clGray;     //单元格背景色
  //      range:=Temsheet.Range[Temsheet.cells[TemInt,i+2],Temsheet.cells[TemInt,i+2]];//选定表格
  //      range.borders.linestyle:=1;//华线
  //    end;
  //
  //    TemInt:=TemInt+1;
  //    //////////////////////////////////////////////
  //    j:=0;
  //    DBGrid2.DataSource.DataSet.First;
  //    while not DBGrid2.DataSource.DataSet.Eof do
  //    begin
  //      Temsheet.Cells[TemInt+j,1].Value:=j+1;
  //      Temsheet.Cells[TemInt+j,1].HorizontalAlignment:=-4108; //字居中
  //      range:=Temsheet.Range[Temsheet.cells[TemInt+j,1],Temsheet.cells[TemInt+j,1]];//选定表格
  //      range.borders.linestyle:=1;//华线
  //
  //      for i:=0 to DBGrid2.Columns.Count - 1 do
  //      begin
  //        Temsheet.Cells[TemInt+j,i+2].Value:=DBGrid2.Fields[i].AsString;
  //        range:=Temsheet.Range[Temsheet.cells[TemInt+j,i+2],Temsheet.cells[TemInt+j,i+2]];//选定表格
  //        range.borders.linestyle:=1;//华线
  //      end;
  //      DBGrid2.DataSource.DataSet.Next;
  //      j:=j+1;
  //    end;

      Application.ProcessMessages;

      Screen.Cursor:=CrDefault;
      FExcel.WorkBooks[1].saveas(TemFileName);//保存文件
      FExcel.workbooks[1].close; //关闭工作表
      Application.ProcessMessages;
      MessageBox(Handle,‘导出成功‘,‘提示‘,MB_OK);
      //FExcel.visible:=true;
      FExcel.quit; //关闭Excel
      FExcel := unassigned;
      //shellexecute(0,‘open‘,PChar(ExtractFileName(TemFileName)),nil,PChar(ExtractFilePath(TemFileName)),SW_Show);

    end;
  End;

原文地址:https://www.cnblogs.com/jijm123/p/10987881.html

时间: 2024-10-29 17:48:37

delphi 导出excel的相关文章

[转]Delphi导出Excel的设置操作

//设置页边距: ExlApp.ActiveSheet.PageSetup.TopMargin:=1.5/0.035; ExlApp.ActiveSheet.PageSetup.BottomMargin:=1.5/0.035; ExlApp.ActiveSheet.PageSetup.LeftMargin:=1/0.035; ExlApp.ActiveSheet.PageSetup.RightMargin:=1/0.035; ExlApp.ActiveSheet.PageSetup.Header

Delphi导出Excel的设置操作

procedure CreatRepSheet(SheetName:String;PageSize,PageLay:Integer); {新建Excel工作簿.进行页面设置} begin {新建Excel工作簿} if ExlApp.WorkBooks.Count < 1 then begin ExlBook := ExlApp.Workbooks.Add; //ExlBook:=ExlApp.WorkBooks[1].WorkSheets[1]; ExlApp.ActiveSheet.Name

Delphi 导出数据至Excel的7种方法【转】

转自:http://blog.csdn.net/zang141588761/article/details/52275948 一; delphi 快速导出excel uses ComObj,clipbrd; function ToExcel(sfilename:string; ADOQuery:TADOQuery):boolean; const xlNormal=-4143; var y : integer; tsList : TStringList; s,filename :string; a

delphi导出数据至Excel的七种方法及比较

一;delphi 快速导出excel uses ComObj,clipbrd; function ToExcel(sfilename:string; ADOQuery:TADOQuery):boolean;const      xlNormal=-4143;var    y     :  integer;    tsList :  TStringList;    s,filename  :string;    aSheet  :Variant;    excel :OleVariant;   

delphi 导出到excel的7种方法

本文来自 爱好者8888 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/kpc2000/article/details/17066823?utm_source=copy =================================================================================================== 第一种方法delphi 快速导出excel uses ComObj,clipbrd; funct

浅谈控件(组件)制作方法一(附带一delphi导出数据到Excel的组件实例)(原创)

来自:http://blog.csdn.net/zhdwjie/article/details/1490741 ------------------------------------------------------------------- 从99年学习delphi开始,我就被它的快速开发迷上了,那时候刚接触编程,对可视化开发特别来劲,原因嘛,不外乎是比C更快的实现啦,这几年来,从delphi的C/S到三层B/S,大大小小也写过一些软件,自认为这delphi也就这么些功能吧,自从最近偶得一

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

CxGrid导出Excel时清除颜色的设置 (2011-04-25 16:33:23) 转载▼ 标签: it 分类: Delphi http://www.radxe.com/?p=170 cxgrid导出到Excel是"所见即所得"模式的,应用程序中显示的颜色在导出时也会被设置到Excel,但很多时候导出的Excel是期望不包含色彩信息的,所以必须在导出时将cxgrid的颜色设置清除.cxgrid表格各个部分的颜色可以通过在view的styles中设置,未设置styles时cxgrid

分享Delphi处理EXCEL源码

分享Delphi处理EXCEL源码,源码如下: (****************************************************************************************) var FExcelApp:Variant;                                         // Excel App  FCellRange:Variant;                                       

Delphi操作Excel大全

转自  上帝的鱼--专栏  cdsn 个人收藏:Delphi  控制Excel(一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp := CreateOleObject( 'Excel.Application' );1) 显示当前窗口:ExcelApp.Visible := True;2) 更改 Excel 标题栏:ExcelApp.Caption := '应用程序调用 Microsoft Excel';3) 添加新