delphi7导出到Excel中怎么弄啊??有合并,样式设置
我想把一个TClientDataSet的数据导出到Excel中,如何,不用TDBGridEh的导出...
我想把一个TClientDataSet 的数据导出到Excel中,如何,不用TDBGridEh的导出
展开
1个回答
展开全部
我给你看一个我的例子,以前我做的,希望对你有用
procedure TfmvProduceZL.btnDaoChuClick(Sender: TObject);
var ExcelApp: Variant; // uses ComObj
i,nAll_NUM : integer;
s1 : String;
begin
inherited;
ExcelApp := CreateOleObject( 'Excel.Application' );
ExcelApp.Visible := True;
ExcelApp.Caption := '应用程序调用 Microsoft Excel';
ExcelApp.WorkBooks.Add;
ExcelApp.WorkBooks.Open( 'D:\jdmy\Demo.xls' );//这个是一个模板,可以提前把样式做好
///////写数据前先清空每个行数据。
//ExcelApp.ActiveSheet.PageSetup.PrintGridLines := True;
ExcelApp.ActiveSheet.PageSetup.HeaderMargin := 1/0.035; //顶端边距1cm:
ExcelApp.ActiveSheet.PageSetup.FooterMargin := 1/0.035; // 页脚到底端边距1cm:
ExcelApp.ActiveSheet.PageSetup.LeftMargin := 0.5/0.035; //左右边距 (单位:磅)(1磅=0.035厘米)
ExcelApp.ActiveSheet.PageSetup.RightMargin := 0/0.035;
//ExcelApp.ActiveSheet.PageSetup.CenterFooter := '第&P页/共&N页';
ExcelApp.range['A1:T1'].Merge; //合并 A1到T1
ExcelApp.Cells[1,1].Value := 'XXXX生产任务单('+Trim(cboAdd.Text)+')';//写表头
ExcelApp.range['A2:F2'].Merge; //合并
ExcelApp.Cells[2,1].Value := '下单日期:'+FormatDateTime('yyyy年mm月dd日',date);
ExcelApp.range['J2:J2'].Merge;
//ExcelApp.Cells[2,10].Value := '共****樘';
ExcelApp.range['Q2:S2'].Merge; //合并
ExcelApp.Cells[2,17].Value := '累计*****樘';
ExcelApp.range['A3:A4'].Merge;
ExcelApp.Cells[3,1].Value := '客户';
ExcelApp.range['B3:B4'].Merge;
ExcelApp.Cells[3,2].Value := '订单号';
ExcelApp.range['C3:C4'].Merge;
ExcelApp.Cells[3,3].Value := '编号';
ExcelApp.range['D3:E3'].Merge;
ExcelApp.Cells[3,4].Value := '规格';
ExcelApp.Cells[4,4].Value := '宽';
ExcelApp.Cells[4,5].Value := '高';
ExcelApp.range['F3:F4'].Merge;
ExcelApp.Cells[3,6].Value := '框型';
ExcelApp.range['G3:G4'].Merge;
ExcelApp.Cells[3,7].Value := '框厚';
ExcelApp.range['H3:H4'].Merge;
ExcelApp.Cells[3,8].Value := '花型';
ExcelApp.range['I3:I4'].Merge;
ExcelApp.Cells[3,9].Value := '板厚';
ExcelApp.range['J3:J4'].Merge;
ExcelApp.Cells[3,10].Value := '开向';
ExcelApp.range['K3:K4'].Merge;
ExcelApp.Cells[3,11].Value := '左';
ExcelApp.range['L3:L4'].Merge;
ExcelApp.Cells[3,12].Value := '右';
ExcelApp.range['M3:M4'].Merge;
ExcelApp.Cells[3,13].Value := '颜色';
ExcelApp.range['N3:R3'].Merge;
ExcelApp.Cells[3,14].Value := '下料尺寸';
ExcelApp.range['N4:O4'].Merge;
ExcelApp.Cells[4,14].Value := '主扇宽';
ExcelApp.Cells[4,16].Value := '主扇高';
ExcelApp.Cells[4,17].Value := '子扇宽';
ExcelApp.Cells[4,18].Value := '子扇高';
ExcelApp.range['S3:S4'].Merge;
ExcelApp.Cells[3,19].Value := '总高';
ExcelApp.range['T3:T4'].Merge;
ExcelApp.Cells[3,20].Value := '产地';
i :=3;
While i<=4 do
Begin
s1 :='A'+IntToStr(i)+':'+'T'+IntToStr(i);
ExcelApp.ActiveSheet.Range[ s1 ].Borders[1].Weight := 2; //边框线
ExcelApp.ActiveSheet.Range[ s1 ].Borders[2].Weight := 2;
ExcelApp.ActiveSheet.Range[ s1 ].Borders[3].Weight := 2;
ExcelApp.ActiveSheet.Range[ s1 ].Borders[4].Weight := 2;
i :=i+1;
End;
ExcelApp.ActiveSheet.Range[ 'A2:T2' ].Borders[4].Weight := 3; //下边框线
ExcelApp.ActiveSheet.Range[ 'A4:T4' ].Borders[4].Weight := 3; //下边框线
///循环写数据,注意每个模版能写多少行。
cdsM.First;
i :=5;
nAll_NUM :=0;
While not cdsM.Eof do
begin
nAll_NUM :=nAll_NUM +cdsM.FieldByName('AMOUNT').AsInteger;
ExcelApp.Cells[i,1].Value := cdsM.FieldByName('CUST_NAME').AsString;
if length(cdsM.FieldByName('CUST_NAME').AsString) >3 then
ExcelApp.Cells[i,1].Font.Size:=9;//设置某单元格的字体大小
ExcelApp.Cells[i,2].Value := cdsM.FieldByName('ORDER_NO').AsString;
if length(cdsM.FieldByName('BUSI_NO').AsString) >3 then
ExcelApp.Cells[i,2].Font.Size:=9;//设置某单元格的字体大小
ExcelApp.Cells[i,3].Value := cdsM.FieldByName('BUSI_NO').AsString;//编号
ExcelApp.Cells[i,4].Value := cdsM.FieldByName('DOOR_KUAN').AsString;
ExcelApp.Cells[i,5].Value := cdsM.FieldByName('SC_GAO').AsString;
ExcelApp.Cells[i,6].Value := cdsM.FieldByName('KUANG_X').AsString;
ExcelApp.Cells[i,7].Value := cdsM.FieldByName('KUANG_H').AsString;
ExcelApp.Cells[i,8].Value := cdsM.FieldByName('HUA_X_T').AsString;
ExcelApp.Cells[i,8].Font.Size:=9;//设置某单元格的字体大小
ExcelApp.Cells[i,9].Value := cdsM.FieldByName('MBK_HD').AsString;
ExcelApp.Cells[i,10].Value := cdsM.FieldByName('KAI_X_T').AsString;
//if (cdsM.FieldByName('KAI_X').AsString = '内左') or (cdsM.FieldByName('KAI_X').AsString = '外左') then
ExcelApp.Cells[i,11].Value := cdsM.FieldByName('AMOUNT_Z').AsString; //左
//if (cdsM.FieldByName('KAI_X').AsString = '内右') or (cdsM.FieldByName('KAI_X').AsString = '外右') then
ExcelApp.Cells[i,12].Value := cdsM.FieldByName('AMOUNT_Y').AsString; //右
ExcelApp.Cells[i,13].Value := cdsM.FieldByName('YAN_X').AsString;
if length(cdsM.FieldByName('YAN_X').AsString) >3 then
ExcelApp.Cells[i,13].Font.Size:=9;//设置某单元格的字体大小
ExcelApp.Cells[i,14].Value := '面';
//判断是否为四开门
if cdsM.FieldByName('ITEM_NAME').AsString ='四开门' then
Begin
s1 :='O'+IntToStr(i)+':'+'R'+IntToStr(i);
ExcelApp.range[s1].Merge;
ExcelApp.Cells[i,15].Value := cdsM.FieldByName('MIAN_ZHUS_K').AsString+' '+cdsM.FieldByName('MIAN_ZHUS_G').AsString+' '
+cdsM.FieldByName('MIAN_ZIS_K').AsString+' '+cdsM.FieldByName('MIAN_ZIS_G').AsString;
ExcelApp.Cells[i,15].Font.Size:=9;//设置某单元格的字体大小
End
Else
Begin
ExcelApp.Cells[i,15].Value := cdsM.FieldByName('MIAN_ZHUS_K').AsString;// 面主扇宽
ExcelApp.Cells[i,16].Value := cdsM.FieldByName('MIAN_ZHUS_G').AsString;// 面主扇高
ExcelApp.Cells[i,17].Value := cdsM.FieldByName('MIAN_ZIS_K').AsString;// 面子扇宽
ExcelApp.Cells[i,18].Value := cdsM.FieldByName('MIAN_ZIS_G').AsString;// 面子扇高
End;
s1 :='S'+IntToStr(i)+':'+'S'+IntToStr(i+1);
ExcelApp.range[s1].Merge;
ExcelApp.Cells[i,19].Value := cdsM.FieldByName('SUM_GAO').AsString;
s1 :='T'+IntToStr(i)+':'+'T'+IntToStr(i+1);
ExcelApp.range[s1].Merge;
if cdsM.FieldByName('MENG_L').AsString ='是' then
ExcelApp.Cells[i,20].Value := '√';//cdsM.FieldByName('MENG_L').AsString; //产地
s1 :='A'+IntToStr(i)+':'+'T'+IntToStr(i);
ExcelApp.ActiveSheet.Range[ s1 ].Borders[1].Weight := 2; //左边框线 1-左 2-右 3-顶 4-底 5-斜( \ ) 6-斜( / )
ExcelApp.ActiveSheet.Range[ s1 ].Borders[2].Weight := 2;
ExcelApp.ActiveSheet.Range[ s1 ].Borders[3].Weight := 2;
ExcelApp.ActiveSheet.Range[ s1 ].Borders[4].Weight := 2;
i :=i+1;
if i mod 48 =0 Then //共44/2=22个单子
ExcelApp.WorkSheets[1].Rows[i+1].PageBreak := 1;//分页符插入在当前行前 一个订单为两行,所以+1
ExcelApp.Cells[i,1].Value := '备注';
s1 :='B'+IntToStr(i)+':'+'M'+IntToStr(i);
ExcelApp.range[s1].Merge;
ExcelApp.Cells[i,2].Value := cdsM.FieldByName('ZHMEMO').AsString;
if length(cdsM.FieldByName('ZHMEMO').AsString)>26 Then
ExcelApp.Cells[i,2].Font.Size:=9;//设置某单元格的字体大小
ExcelApp.Cells[i,14].Value := '背';
//判断是否为四开门
if cdsM.FieldByName('ITEM_NAME').AsString ='四开门' then
Begin
s1 :='O'+IntToStr(i)+':'+'R'+IntToStr(i);
ExcelApp.range[s1].Merge;
ExcelApp.Cells[i,15].Value := cdsM.FieldByName('BEI_ZHUS_K').AsString+' '+cdsM.FieldByName('BEI_ZHUS_G').AsString+' '
+cdsM.FieldByName('BEI_ZIS_K').AsString+' '+cdsM.FieldByName('BEI_ZIS_G').AsString;
ExcelApp.Cells[i,15].Font.Size:=9;//设置某单元格的字体大小
End
Else
Begin
ExcelApp.Cells[i,15].Value := cdsM.FieldByName('BEI_ZHUS_K').AsString;// 背主扇宽
ExcelApp.Cells[i,16].Value := cdsM.FieldByName('BEI_ZHUS_G').AsString;// 背主扇高
ExcelApp.Cells[i,17].Value := cdsM.FieldByName('BEI_ZIS_K').AsString;// 背子扇宽
ExcelApp.Cells[i,18].Value := cdsM.FieldByName('BEI_ZIS_G').AsString;// 背子扇高
End;
s1 :='A'+IntToStr(i)+':'+'T'+IntToStr(i);
ExcelApp.ActiveSheet.Range[ s1 ].Borders[1].Weight := 2;
ExcelApp.ActiveSheet.Range[ s1 ].Borders[2].Weight := 2;
ExcelApp.ActiveSheet.Range[ s1 ].Borders[3].Weight := 2;
ExcelApp.ActiveSheet.Range[ s1 ].Borders[4].Weight := 3;
i :=i+1;
cdsM.Next;
End;
ExcelApp.Cells[2,10].Value := '共'+IntToStr(nAll_NUM)+'樘'; //统计完后,再写入总数
end;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询