• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphi导出excel

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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;

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphixe下快捷使用FastMM的内存泄露检测功能发布时间:2022-07-18
下一篇:
Delphi在Github的开源框架发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap