delphi 导出EXCEL XLSX

uses comobj;

procedure TFrm_AllRecord.EXCEL1Click(Sender: TObject);
var
  xlApp, xlBook, xlSheet, xlQuery: Variant;
  str1,str2,str3,fn,p_sconn:string;
  i,j,k,m,n,i2,j2,k2,m2,n2:Integer;
begin
    //Dbgrid2excel.DBGridToExcel(dbgrid1);

   if qry1.RecordCount >0 then
      begin


            dlgSave1.Filter:= 'Excel文件(*.xlsx)|*.xlsx';
            dlgSave1.DefaultExt :='.xlsx';
            if dlgSave1.Execute then
               begin
                    fn:=dlgSave1.FileName ;
                    xlApp := CreateOleObject('Excel.Application');
                    xlBook := xlApp.Workbooks.Add;
                    xlSheet := xlBook.Worksheets['sheet1'];
                    xlApp.Visible := false;

                    //nall:=Adoquery5.RecordCount; //保存记录的数量
                    //row1:=0; //初始化行,定位在第一 行
                    xlSheet.Cells.item[1, 1] := dbgrd1.Columns[0].Title.Caption ;
                    xlSheet.Cells.item[1, 2] := dbgrd1.Columns[1].Title.Caption ;
                    xlSheet.Cells.item[1, 3] := dbgrd1.Columns[2].Title.Caption ;
                    xlSheet.Cells.item[1, 4] := dbgrd1.Columns[3].Title.Caption;
                    xlSheet.Cells.item[1, 5] := dbgrd1.Columns[4].Title.Caption;
                    xlSheet.Cells.item[1, 6] := dbgrd1.Columns[5].Title.Caption;
                    xlSheet.Cells.item[1, 7] := dbgrd1.Columns[6].Title.Caption;
                    xlSheet.Cells.item[1, 8] := dbgrd1.Columns[7].Title.Caption;
                    xlSheet.Cells.item[1, 9] := dbgrd1.Columns[8].Title.Caption;
                    xlSheet.Cells.item[1, 10] := dbgrd1.Columns[9].Title.Caption;
                    xlSheet.Cells.item[1, 11] := dbgrd1.Columns[10].Title.Caption;
                    xlSheet.Cells.item[1, 12] := dbgrd1.Columns[11].Title.Caption;
                    xlSheet.Cells.item[1, 13] := dbgrd1.Columns[12].Title.Caption;
                    xlSheet.Cells.item[1, 14] := dbgrd1.Columns[13].Title.Caption;
                    xlSheet.Cells.item[1, 15] := dbgrd1.Columns[14].Title.Caption;
                    xlSheet.Cells.item[1, 16] := dbgrd1.Columns[15].Title.Caption;
                    xlSheet.Cells.item[1, 17] := dbgrd1.Columns[16].Title.Caption;
                    xlSheet.Cells.item[1, 18] := dbgrd1.Columns[17].Title.Caption;
                    xlSheet.Cells.item[1, 19] := dbgrd1.Columns[18].Title.Caption;
                    xlSheet.Cells.item[1, 20] := dbgrd1.Columns[19].Title.Caption;
                    xlSheet.Cells.item[1, 21] := dbgrd1.Columns[20].Title.Caption;
                    xlSheet.Cells.item[1, 22] := dbgrd1.Columns[21].Title.Caption;
                    xlSheet.Cells.item[1, 23] := dbgrd1.Columns[22].Title.Caption;
                    xlSheet.Cells.item[1, 24] := dbgrd1.Columns[23].Title.Caption;
                    xlSheet.Cells.item[1, 25] := dbgrd1.Columns[24].Title.Caption;
                    xlSheet.Cells.item[1, 26] := dbgrd1.Columns[25].Title.Caption;
                    xlSheet.Cells.item[1, 27] := dbgrd1.Columns[26].Title.Caption;

                  xlQuery := xlSheet.QueryTables.Add(qry1.Recordset ,xlSheet.Range['A2']);
                  xlQuery.FieldNames := false;
                  xlQuery.RowNumbers := False;
                  xlQuery.FillAdjacentFormulas := False;
                  xlQuery.PreserveFormatting := True;
                  xlQuery.RefreshOnFileOpen := False;
                  xlQuery.BackgroundQuery := True;
                 //xlQuery.RefreshStyle := xlOverwriteCells;
                  xlQuery.SavePassword := True;
                  xlQuery.SaveData := True;
                  xlQuery.AdjustColumnWidth := True;
                  xlQuery.RefreshPeriod := 0;
                  xlQuery.PreserveColumnInfo := True;
                  xlQuery.Refresh;
                  xlApp.DisplayAlerts := False;  //取消报警
                  //xlSheet.range['B2:B8'].Merge; //合并单元格
                  //xlSheet.range[xlSheet.Cells.item[2,4],xlSheet.Cells.item[4,4]].Merge;
                  //xlSheet.range[xlSheet.Cells.item[5,4],xlSheet.Cells.item[7,4]].Merge;

               //清除不必要的字符
                str1:=datetimetostr(now());
                str1:=stringreplace(str1,'-','',[rfReplaceAll,rfIgnoreCase]);
                str1:=stringreplace(str1,':','',[rfReplaceAll,rfIgnoreCase]);
                str1:=stringreplace(str1,' ','',[rfReplaceAll,rfIgnoreCase]);
                //str_memo:= stringreplace(str_memo,'-','',[rfReplaceAll,rfIgnoreCase]);
                //str2:=str3 + '\' + batch  + '_' + trim(dbcb2.Text ) + '_' + trim(edit1.Text ) + trim(edit3.Text ) + '_' + str1;

                //以下是导出完毕后的动作,

                xlBook.SavecopyAs(fn); //加后缀名,确保最后是EXCEL文件
                 xlBook.Close(false);
                 //xlApp.Disconnect; //断开与EXCEL的连接
                 Screen.Cursor:=crDefault; //指针随记录而动

                ShowMessage('导出完毕');
              end;
      end
   else
      begin
           ShowMessage('没有数据');
      end;

end;

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi导出Excel慢的原因有很多,如下所述: 1. 数据量大:如果导出的数据量非常庞大,那么导出时间就会较长。可以考虑对数据进行分页处理,每次只导出一页数据,以减少导出时间。 2. 导出文件格式:如果选择导出Excel格式为.xlsx,相对于.xls格式导出速度会较慢。因为.xlsx格式的文件需要进行压缩和加密,所以处理的时间会更长。 3. 使用组件库:某些组件库的导出功能可能较慢。使用对Excel导出性能进行了优化的第三方组件库可能会提升导出速度。 4. 使用较低版本的Delphi:较低版本的Delphi可能对Excel导出性能没有做过多的优化,可以尝试使用较新版本的Delphi进行导出。 5. 数据处理方式:在导出数据之前,可能对数据进行了一些处理操作,如排序、过滤、格式转换等。这些处理操作也会增加导出的时间消耗。 为了解决这个问题,可以采取以下措施: 1. 优化查询:在导出Excel之前,可以对查询语句进行优化,使用合适的索引、减少不必要的列、使用合适的查询条件等,以提高查询速度。 2. 使用多线程:将导出Excel的操作放在一个独立的线程中进行,这样可以避免阻塞主线程,提高用户体验。 3. 数据分页处理:将数据分页导出,每次只导出一部分数据,以减少导出的时间和内存消耗。 4. 使用合适的Excel组件库:选择性能较好的第三方组件库,可以显著提升导出速度。 5. 使用合适的导出格式:如果导出速度较慢,可以尝试使用.xls格式而非.xlsx格式。 总体来说,通过优化查询、使用多线程、数据分页处理、选择合适的Excel组件库和导出格式,可以改善Delphi导出Excel慢的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值