Delphi 导出Excel功能

一个简单的Delphi导出Excel的例子,只为与大神们共同学习。
以一个出货通知单导出到Excle作为例:

procedure TfrmGosuncnClientInvProdShip.ExportReceiptInfo(Sender: TObject);
var h,k,top_height:integer;
Excelid,FPicture,sheet: OleVariant;
s,s1,s2,AuditNo,Sell_No,tmpstr,receiptNo,mtrl_desc,mtrl_desc_new,mtrl_name: string;
Q,Q1:TPgClientSimpleDataEntity;
asheet1,achart, range:variant;//范围
split:Char;
AuditNos:TArray;
begin
try
Excelid := CreateOLEObject(‘Excel.Application’); //创建Excel
except
Application.MessageBox(‘Excel没有安装!’, ‘提示信息’,MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);
Exit;
end;
AuditNo:=etAUDIT_ID.Text;
if AuditNo=‘’ then
raise TPgError.Create(‘请选择出货通知单!’);
try
top_height:=0;
Q := TPgLib.CreateDataEntity;
Q.Close;
Q.CommandText := ‘select * from TB_MM_AUDIT_HD where AUDIT_ID =:AUDIT_ID’;
Q.Params.ParamByName(‘AUDIT_ID’).AsString := AuditNo;
Q.Open;
if not Q.IsEmpty then
begin
Sell_No:=Q.FieldByName(‘AUDIT_EX1’).AsString;
end;
Excelid.Visible := True;
Excelid.WorkBooks.Add;

sheet := Excelid.WorkSheets[1];
sheet.Select;
sheet.Columns[1].ColumnWidth:=30;
sheet.Columns[2].ColumnWidth:=14;//设置列宽度
sheet.Columns[3].ColumnWidth:=14;
sheet.Columns[4].ColumnWidth:=18;
sheet.Columns[5].ColumnWidth:=5;
sheet.Columns[6].ColumnWidth:=5;
sheet.Columns[7].ColumnWidth:=15;
sheet.Columns[8].ColumnWidth:=20;

//插入图象e79fa5e98193e59b9ee7ad9431333363393665并设置位置与大小
tmpstr:='\\127.0.0.1\picture\XXX.png';
if not TFile.Exists(tmpstr) then
begin
  Excelid.Quit;
  raise Exception.Create('\\127.0.0.1路径下不存在名字为【XXX.png】图片,请添加该图片到此目录下'); 
end;
FPicture:=sheet.Pictures.Insert(tmpstr);
FPicture.Left:=205;
FPicture.Top:=0;
FPicture.width:=125;

//HorizontalAlignment(水平) 左对齐(-4131),居中(-4108),右对齐(-4152)
//VerticalAlignment(垂直) 上对齐(-4160),居中(-4108),下对齐(-4107)

sheet.Range['B1:H1'].Merge(True); //第2单元格-第7单元格合并成1个
sheet.Cells[1,2].Value :='         设置Excel标题' ;
sheet.Cells[1,10].Value :='XXXX' ;
sheet.Cells[1,2].HorizontalAlignment:=-4108;    //居中显示
sheet.Range['B1:H1'].Font.Name := '黑体';
sheet.Range['B1:H1'].Font.Size := 17;
sheet.range['B1:H1'].font.bold:=true;
sheet.Rows[1].RowHeight :=28;
top_height:= top_height+28;

//sheet.Rows[2].RowHeight :=18;
sheet.range['B2:H2'].Merge(True);
sheet.Cells[2,2].Value := 'GS-P-W-009-001A0';
sheet.Range['B2:H2'].Font.Name := '黑体';
sheet.Rows[2].RowHeight :=18;
top_height:= top_height+18;

sheet.Cells[2,10].Value := 'XXX';
sheet.Cells[3,2].Value := '订单编号:';
sheet.Cells[3,2].HorizontalAlignment:=-4108;
sheet.range['B3:B3'].font.bold:=true;
sheet.range['C3:F3'].Merge(True);
sheet.Cells[3,3].Value := Sell_No;//Q.FieldByName('AUIDT_EX1').AsString;
sheet.Cells[3,7].Value := '发货批次号:';
sheet.Cells[3,7].HorizontalAlignment:=-4108;
sheet.range['G3:G3'].font.bold:=true;
sheet.Cells[3,8].Value :='';
sheet.Range['B3:H3'].Borders.LineStyle := 1;
//sheet.Range['B3:H3'].Borders[3].LineWeight := 2;    //指定边框线宽度:1-左 2-右 3-顶 4-底 5-斜( \ ) 6-斜( / )
sheet.Range['B3:H3'].Font.size := 12;
sheet.Rows[3].RowHeight:=30;
top_height:= top_height+30;

sheet.Cells[4,2].Value := '用户单位:';
sheet.Cells[4,2].HorizontalAlignment:=-4108;
sheet.range['B4:B4'].font.bold:=true;
sheet.range['C4:F4'].Merge(True);
sheet.Cells[4,3].Value := '';
sheet.Cells[4,7].Value := '发货日期:';
sheet.Cells[4,7].HorizontalAlignment:=-4108;
sheet.range['G4:G4'].font.bold:=true;
sheet.Cells[4,8].Value := DateTimeToStr(date());
sheet.Cells[4,8].HorizontalAlignment:= -4131;
sheet.Range['B4:H4'].Borders.LineStyle := 1;
sheet.Range['B4:H4'].Font.size := 12;
sheet.Rows[4].RowHeight:=30;
top_height:= top_height+30;

sheet.Cells[5,2].Value := '项目名称:';
sheet.Cells[5,2].HorizontalAlignment:=-4108;
sheet.range['B5:B5'].font.bold:=true;

sheet.range['C5:F5'].Merge(True);
sheet.Cells[5,3].Value := '';
sheet.Range['C5:C5'].WrapText :=True;   //换行
sheet.Cells[5,7].Value := '项目编码:';
sheet.Cells[5,7].HorizontalAlignment:=-4108;
sheet.range['G5:G5'].font.bold:=true;
sheet.Cells[5,8].Value := '';
sheet.Range['B5:H5'].Borders.LineStyle := 1;
sheet.Range['B5:H5'].Font.size := 12;
sheet.Rows[5].RowHeight:=30;
top_height:= top_height+30;

GXX_ExcelRange(sheet,range,'收货地址','B6:B7');
GXX_ExcelRange(sheet,range,'','C6:F7');
//sheet.Cells[6,2].Value := '收货地址:';
sheet.Cells[6,2].HorizontalAlignment:=-4108;
sheet.range['B6:B6'].font.bold:=true;
//sheet.range['B6:B7'].Merge(True);

//sheet.range['C6:C7'].Merge;
//sheet.range['C6:F6'].Merge(True);  //合并单元格
//sheet.range['C7:F7'].Merge(True);

//sheet.range['B6:B7'].Merge(True);

//sheet.Cells[6,3].Value :='';
sheet.Cells[6,3].HorizontalAlignment:=-4131;
//sheet.Range['C6:C6'].WrapText:=True;
sheet.Cells[6,7].Value := '联系人:';
sheet.Cells[6,7].HorizontalAlignment:=-4108;
sheet.range['G6:G6'].font.bold:=true;
sheet.Cells[6,8].Value := '';
sheet.Cells[7,7].Value := '联系电话:';
sheet.Cells[7,7].HorizontalAlignment:=-4108;
sheet.range['G7:G7'].font.bold:=true;
sheet.Cells[7,8].Value:='';
sheet.Cells[7,8].HorizontalAlignment:=-4131;
sheet.Cells[7,8].Value := '';
sheet.Range['B6:H6'].Borders.LineStyle := 1;
sheet.Range['B7:H7'].Borders.LineStyle := 1;
sheet.Range['B6:H6'].Font.Size := 12;
sheet.Range['B7:H7'].Font.Size := 12;

sheet.Cells[8,2].Value := '设备名称';
sheet.range['C8:D8'].Merge(True);
sheet.Cells[8,3].Value := '设备内容';
sheet.Cells[8,5].Value := '单位';
sheet.Cells[8,6].Value := '数量';
sheet.Cells[8,7].Value := '箱号';
sheet.Cells[8,8].Value := '每套装箱';
sheet.Cells[8,2].HorizontalAlignment:=-4108;
sheet.Cells[8,3].HorizontalAlignment:=-4108;
sheet.Cells[8,5].HorizontalAlignment:=-4108;
sheet.Cells[8,6].HorizontalAlignment:=-4108;
sheet.Cells[8,7].HorizontalAlignment:=-4108;
sheet.Cells[8,8].HorizontalAlignment:=-4108;
sheet.Range['B8:H8'].Font.Name := '宋体';
sheet.Range['B8:H8'].Font.Size := 12;
sheet.range['B8:H8'].font.bold:=true;
sheet.Range['B8:H8'].Borders.LineStyle := 1;
sheet.Rows[6].RowHeight:=30;
top_height:= top_height+30;
sheet.Rows[7].RowHeight:=30;
top_height:= top_height+30;
sheet.Rows[8].RowHeight:=20;
top_height:= top_height+20;


  sheet.Cells[h,1].Value := '';
  sheet.Cells[h,2].Value := '';  //物料名称
  sheet.Cells[h,3].Value :='';//
  sheet.Cells[h,5].Value := '';
  sheet.Cells[h,6].Value :='';         //拿未完成数量
  sheet.Cells[h,1].HorizontalAlignment:=-4108;
  sheet.Cells[h,2].HorizontalAlignment:=-4108;
  sheet.Cells[h,3].HorizontalAlignment:=-4108;
  sheet.Cells[h,5].HorizontalAlignment:=-4108;
  sheet.Cells[h,6].HorizontalAlignment:=-4108;
  sheet.Cells[h,7].HorizontalAlignment:=-4108;
  sheet.Cells[h,8].HorizontalAlignment:=-4108;
  s1:='C'+IntToStr(h)+':D'+IntToStr(h);
  sheet.range[s1].Merge(True);
  s:='B'+IntToStr(h)+':H'+IntToStr(h);
  sheet.Range[s].Font.Name := '宋体';
  sheet.Range[s].Font.size := 12;
  sheet.Range[s].Borders.LineStyle := 1;
  sheet.Range[s].WrapText :=True;     //允许自动换行
  sheet.Rows[h].RowHeight:=50;
  top_height:= top_height+50;
 
sheet.Cells[h,2].HorizontalAlignment:=-4108;
s:='B'+IntToStr(h)+':B'+IntToStr(h+2);

sheet.range[s].font.bold:=true;

sheet.Cells[h,3].Value := '总重量';
sheet.Cells[h,3].HorizontalAlignment:=-4108;
sheet.Cells[h,4].Value := '';//Q1.FieldByName('AUDIT_QTY').AsString;
sheet.Cells[h,4].HorizontalAlignment:=-4108;
s:='E'+IntToStr(h)+':F'+IntToStr(h);
sheet.range[s].Merge(True);
sheet.Cells[h,5].Value := 'KG';
sheet.Cells[h,5].HorizontalAlignment:=-4108;
//sheet.Cells[h,7].Value := '提货人签名';
sheet.Cells[h,7].HorizontalAlignment:=-4108;
s:='G'+IntToStr(h)+':G'+IntToStr(h+2);

sheet.range[s].font.bold:=true;
sheet.range[s].WrapText:=True;
s:='H'+IntToStr(h)+':H'+IntToStr(h+2);


sheet.Cells[h,8].Value := '';
sheet.Cells[h,8].HorizontalAlignment:=-4108;
s2:='B'+IntToStr(h)+':H'+IntToStr(h);
sheet.Range[s2].Borders.LineStyle := 1;
sheet.Range[s2].Font.size := 12;
sheet.Rows[h].RowHeight:=20;
top_height:= top_height+20;

h:=h+1;
sheet.Cells[h,3].Value := '总体积';
sheet.Cells[h,3].HorizontalAlignment:=-4108;
sheet.Cells[h,4].Value := '';
sheet.Cells[h,4].HorizontalAlignment:=-4108;
s:='E'+IntToStr(h)+':F'+IntToStr(h);
sheet.range[s].Merge(True);
sheet.Cells[h,5].Value := '立方米';
sheet.Cells[h,5].HorizontalAlignment:=-4108;
s2:='B'+IntToStr(h)+':H'+IntToStr(h);
sheet.Range[s2].Borders.LineStyle := 1;
sheet.Range[s2].Font.size := 12;
sheet.Rows[h].RowHeight:=20;
top_height:= top_height+20;

h:=h+1;
sheet.Cells[h,3].Value := '总箱数';
sheet.Cells[h,3].HorizontalAlignment:=-4108;
sheet.Cells[h,4].Value := '';
sheet.Cells[h,4].HorizontalAlignment:=-4108;
s:='E'+IntToStr(h)+':F'+IntToStr(h);
sheet.range[s].Merge(True);
sheet.Cells[h,5].Value := '箱';
sheet.Cells[h,5].HorizontalAlignment:=-4108;
s2:='B'+IntToStr(h)+':H'+IntToStr(h);
sheet.Range[s2].Borders.LineStyle := 1;
sheet.Range[s2].Font.size := 12;
sheet.Rows[h].RowHeight:=20;
top_height:= top_height+20;

h:=h+1;
sheet.Cells[h,2].Value := '检验';
sheet.Cells[h,2].HorizontalAlignment:=-4108;
s:='B'+IntToStr(h)+':B'+IntToStr(h);
sheet.range[s].font.bold:=true;
s:='C'+IntToStr(h)+':D'+IntToStr(h);
sheet.range[s].Merge(True);
sheet.Cells[h,3].Value := '';
sheet.Cells[h,3].HorizontalAlignment:=-4108;
s:='E'+IntToStr(h)+':F'+IntToStr(h);
sheet.range[s].Merge(True);
sheet.Cells[h,5].Value := '发货单位(盖章)';
sheet.Cells[h,5].HorizontalAlignment:=-4108;
sheet.range[s].font.bold:=true;
sheet.range[s].WrapText:=True;
s:='G'+IntToStr(h)+':H'+IntToStr(h);
sheet.range[s].Merge(True);
sheet.Cells[h,7].Value := '';
sheet.Cells[h,7].HorizontalAlignment:=-4108;
s2:='B'+IntToStr(h)+':H'+IntToStr(h);
sheet.Range[s2].Borders.LineStyle := 1;
sheet.Range[s2].Font.size := 12;
sheet.Rows[h].RowHeight:=33;
top_height:= top_height+33;

h:=h+1;
sheet.Cells[h,2].Value := '是否易损件';
sheet.Cells[h,2].HorizontalAlignment:=-4108;
s:='B'+IntToStr(h)+':B'+IntToStr(h);
sheet.range[s].font.bold:=true;
s:='C'+IntToStr(h)+':H'+IntToStr(h);
sheet.range[s].Merge(True);
sheet.Cells[h,3].Value := '';
s:='B'+IntToStr(h)+':H'+IntToStr(h);
sheet.Range[s].Borders.LineStyle := 1;
sheet.Range[s].Font.size := 12;
sheet.Rows[h].RowHeight:=20;
top_height:= top_height+20;

h:=h+1;
ExcelContent('温馨提示',h,Excelid);
sheet.Cells[h,2].HorizontalAlignment:=-4108;    //居中显示
s:='B'+IntToStr(h)+':H'+IntToStr(h);
sheet.Range[s].Font.size := 20;
sheet.range[s].font.bold:=true;
sheet.Rows[h].RowHeight:=25;
top_height:= top_height+25;
sheet.Range[s].Borders.LineStyle := 1;
sheet.Range[s].Borders[3].LineStyle := 7;
sheet.Range[s].Borders[4].LineStyle := 7;



{$REGION '生成二维码'}
_split_:='_';
if AuditNo.Contains('_JH_') then
begin
  AuditNos:=AuditNo.Split(_split_);
  receiptNo:=AuditNos[2];
  Update(receiptNo,AuditNo);
  tmpstr:='\\127.0.0.1\picture\QRCodeImg\'+AuditNo+'\'+receiptNo+'.png';
  FPicture:=sheet.Pictures.Insert(tmpstr);
  FPicture.Left:=695;
  FPicture.Top := top_height;       //获取二维码距离顶部的高度  
  FPicture.width:=60;
end;
{$ENDREGION}

h:=h+1;
//sheet.Cells[h,2].Value := '收货确认';
sheet.Cells[h,2].HorizontalAlignment:=-4108;
s:='B'+IntToStr(h)+':B'+IntToStr(h+1);

sheet.range[s].Font.bold:=True;
sheet.Cells[h,3].Value := '收货总箱数:';
sheet.Cells[h,3].HorizontalAlignment:=-4108;
sheet.Cells[h,4].Value := '';
s:='E'+IntToStr(h)+':G'+IntToStr(h+1);

sheet.range[s].Merge(True);
sheet.Cells[h,5].Value := '收货人签名:';
sheet.Cells[h,5].HorizontalAlignment:=-4108;
sheet.range[s].Font.bold:=True;


sheet.Cells[h,8].Value := '';
s2:='B'+IntToStr(h)+':H'+IntToStr(h);
sheet.Range[s2].Borders.LineStyle := 1;
sheet.Range[s2].Font.size := 12;
sheet.Rows[h].RowHeight:=18;

h:=h+1;
sheet.Cells[h,3].Value := '收货日期';
sheet.Cells[h,3].HorizontalAlignment:=-4108;
sheet.Cells[h,4].Value := '';
s:='E'+IntToStr(h)+':G'+IntToStr(h);
sheet.range[s].Merge(True);
sheet.Cells[h,5].Value := '签收单位(盖章):';
sheet.Cells[h,5].HorizontalAlignment:=-4108;
sheet.range[s].Font.bold:=True;
sheet.Cells[h,8].Value := '';


s2:='B'+IntToStr(h)+':H'+IntToStr(h);
sheet.Range[s2].Borders.LineStyle := 1;                      //1实线 2虚线 7双实线
sheet.Range[s2].Font.size := 12;
sheet.Rows[h].RowHeight:=18;

h:=h+1;
s:='B'+IntToStr(h)+':H'+IntToStr(h);
sheet.range[s].Merge(True);
sheet.Cells[h,2].Value := '      请 货 运 公 司 提 前 一 天 预 约 送 货 时 间!  谢 谢!      ';
sheet.Cells[h,2].HorizontalAlignment:=-4108;    //居中显示
sheet.Range[s].Font.Size := 14;
sheet.range[s].font.bold:=true;
sheet.Rows[h].RowHeight:=18;



Excelid.Quit;

except on e:Exception do
begin
//Application.MessageBox(‘导入数据出错!请检查文件的格式是否正确!’, ‘提示信息’, MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);
//Application.MessageBox(PChar(E.Message), ‘error’, 16);//抛出异常信息
raise Exception.Create(E.Message);
end;
end;
MessageBox(GetActiveWindow(), ‘EXCEL数据导出成功!’, ‘提示信息’,
MB_OK +MB_ICONWARNING);
end;

中间有很多是我项目中的需要用到的地方,大家在使用的时候记得替换掉,放入自己项目中的内容即可!

示例中的二维码是先生成,在导入到Exclel中,如有直接在Exclel中生成二维码的,可以留言交流,谢谢!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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慢的问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值