各位高手...我要把数据导放到Excel报表中呢? 比如我要把一个数放在已经做好的EXCEL表格中,比如把100放在D5这个单元格...然后把这张EXCEL报表打印出来...要怎么实现呢???能否说详细一些...在此谢了..解决了立码送分.. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 var xls:OleVariant;begin try xls:=CreateOleObject('Excel.Application'); //调用 Excel 需要在单元加上ComObj; except // Msgbox(sMsgNotInstallExcel,sTitleError,1); exit; end;if not (FileExists(extractfil+'\pring\报价单.xls')) then xls.WorkBooks.add //增加一个表 else xls.WorkBooks.open(extractfil+'\pring\报价单.xls');//打开一个设计好的表 xls.cells(4,2):='100';//写入数据 xls.ActiveSheet.PrintPreview;//打印一览end; 修改2樓的.uses comobj;var xls:Variant;begin try xls:=CreateOleObject('Excel.Application'); //调用 Excel 需要在单元加上ComObj; except // Msgbox(sMsgNotInstallExcel,sTitleError,1); xls:=unAssigned; exit; end; if not (FileExists(extractfil+'\pring\报价单.xls')) then xls.WorkBooks.add //增加一个表 else xls.WorkBooks.open(extractfil+'\pring\报价单.xls');//打开一个设计好的表 xls.cells(4,2):='100';//写入数据 xls.Visible:=True; xls.ActiveSheet.PrintPreview;//打印一览end; 谢谢二楼的回复,如果我打印后.要不保存退出应该怎么做呢??如果用xls.quit 会提示是否保存. 建议用DBGRIDEH,导出为EXCEL太方便了 如果你只想打印的话 建议你使用FastReport 来做报表 我一般做两个模式 一个把数据保存成一个xls文件 为了可以发email的如果是打印的 的话 我就用 FastReport 来做报表 自由界面和报表的完美解决方案!http://www.anylib.com 谢谢二楼的回复,如果我打印后.要不保存退出应该怎么做呢??如果用xls.quit 会提示是否保存.//不出現提示xls.DisplayAlerts := False; 我觉得像控制Excel, Word用VB简单得多. konhon(优华)就OK 各位高手,按你们的方法,为何我用'xls.' 时,不会像控件那样自动出现属性和方法呢?你们又是如何知道“xls.DisplayAlerts ”之类的方法的呢? (二) 使用Delphi 控件方法在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet。 1) 打开Excel ExcelApplication1.Connect;2) 显示当前窗口:ExcelApplication1.Visible[0]:=True;3) 更改 Excel 标题栏:ExcelApplication1.Caption := '应用程序调用 Microsoft Excel';4) 添加新工作簿:ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(EmptyParam,0)); 5) 添加新工作表:var Temp_Worksheet: _WorkSheet;beginTemp_Worksheet:=ExcelWorkbook1.WorkSheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _WorkSheet;ExcelWorkSheet1.ConnectTo(Temp_WorkSheet);End; 6) 打开已存在的工作簿:ExcelApplication1.Workbooks.Open (c:\a.xlsEmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam,EmptyParam,0)7) 设置第2个工作表为活动工作表:ExcelApplication1.WorkSheets[2].Activate; 或ExcelApplication1.WorksSheets[ 'Sheet2' ].Activate;8) 给单元格赋值:ExcelApplication1.Cells[1,4].Value := '第一行第四列';9) 设置指定列的宽度(单位:字符个数),以第一列为例:ExcelApplication1.ActiveSheet.Columns[1].ColumnsWidth := 5;10) 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例:ExcelApplication1.ActiveSheet.Rows[2].RowHeight := 1/0.035; // 1厘米11) 在第8行之前插入分页符:ExcelApplication1.WorkSheets[1].Rows[8].PageBreak := 1;12) 在第8列之前删除分页符:ExcelApplication1.ActiveSheet.Columns[4].PageBreak := 0;13) 指定边框线宽度:ExcelApplication1.ActiveSheet.Range[ 'B3:D4' ].Borders[2].Weight := 3;1-左 2-右 3-顶 4-底 5-斜( \ ) 6-斜( / )14) 清除第一行第四列单元格公式:ExcelApplication1.ActiveSheet.Cells[1,4].ClearContents;15) 设置第一行字体属性:ExcelApplication1.ActiveSheet.Rows[1].Font.Name := '隶书';ExcelApplication1.ActiveSheet.Rows[1].Font.Color := clBlue;ExcelApplication1.ActiveSheet.Rows[1].Font.Bold := True;ExcelApplication1.ActiveSheet.Rows[1].Font.UnderLine := True;16) 进行页面设置: a.页眉: ExcelApplication1.ActiveSheet.PageSetup.CenterHeader := '报表演示';b.页脚: ExcelApplication1.ActiveSheet.PageSetup.CenterFooter := '第&P页';c.页眉到顶端边距2cm: ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin := 2/0.035;d.页脚到底端边距3cm: ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin := 3/0.035;e.顶边距2cm: ExcelApplication1.ActiveSheet.PageSetup.TopMargin := 2/0.035;f.底边距2cm: ExcelApplication1.ActiveSheet.PageSetup.BottomMargin := 2/0.035;g.左边距2cm: ExcelApplication1.ActiveSheet.PageSetup.LeftMargin := 2/0.035;h.右边距2cm: ExcelApplication1.ActiveSheet.PageSetup.RightMargin := 2/0.035;i.页面水平居中: ExcelApplication1.ActiveSheet.PageSetup.CenterHorizontally := 2/0.035;j.页面垂直居中: ExcelApplication1.ActiveSheet.PageSetup.CenterVertically := 2/0.035;k.打印单元格网线: ExcelApplication1.ActiveSheet.PageSetup.PrintGridLines := True;17) 拷贝操作:a.拷贝整个工作表: ExcelApplication1.ActiveSheet.Used.Range.Copy;b.拷贝指定区域: ExcelApplication1.ActiveSheet.Range[ 'A1:E2' ].Copy;c.从A1位置开始粘贴: ExcelApplication1.ActiveSheet. unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, {if delphi6.0 add ...}Variants{d6};type TForm1 = class(TForm) Button1: TButton; CheckBox1: TCheckBox; Button2: TButton; OpenDialog1: TOpenDialog; Button3: TButton; Button4: TButton; Button5: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); private v:Variant; Sheet: Variant; { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}uses Comobj;//打开EXCEL新建文件procedure TForm1.Button1Click(Sender: TObject);begin try v:= CreateOleObject('Excel.Application'); v.Visible := CheckBox1.Checked;//是否显示 v.Workbooks.Add;//新建EXCEL文件 v.Workbooks[1].WorkSheets[1].Name := 'DELPHI演示';// Sheet:= v.Workbooks[1].WorkSheets['DELPHI演示'];//等效下面的语句 Sheet:= v.Workbooks[1].WorkSheets[1]; Sheet.Cells[1,1] :='DELPHI盒子'; Sheet.Cells[2,1] :='http://www.delphibox.com'; except Showmessage('初始化Excel失败,可能没装Excel,或者其他错误;请重起再试。'); v.DisplayAlerts := false;//是否提示存盘 v.Quit;//如果出错则退出 exit; end; Application.Restore; Application.BringToFront;end;//打开存在的EXCEL文档procedure TForm1.Button2Click(Sender: TObject);begin if OpenDialog1.Execute then begin try v:= CreateOleObject('Excel.Application'); v.Visible := CheckBox1.Checked; v.Workbooks.Open(OpenDialog1.FileName); // Sheet := v.Workbooks[1].WorkSheets[1]; except Showmessage('初始化Excel失败,可能没装Excel,或者其他错误;请重起再试。'); v.DisplayAlerts := false; v.Quit; exit; end; Application.Restore; Application.BringToFront; end;end;//关闭EXCEL并退出procedure TForm1.Button3Click(Sender: TObject);begin try If not varIsEmpty(v) then begin // 如果需要在关闭前确定是否存盘,加入: // v.DiaplayAlert:=true; //确定存盘 // v.DiaplayAlert:=false;//不存盘,直接退出 v.WorkBooks[1].Close(True, 'C:\untitled.xls'); //取文件名退出 v.quit; end; finally Close; end;end;//EXCEL打印页面设置procedure TForm1.Button4Click(Sender: TObject);begin if OpenDialog1.Execute then begin try v:= CreateOleObject('Excel.Application'); v.Visible := CheckBox1.Checked; v.Workbooks.Open(OpenDialog1.FileName); Sheet:=v.Workbooks[1].WorkSheets[1]; Sheet.PageSetup.PrintTitleRows :='$1:$3'; //页眉 Sheet.PageSetup.PrintTitleColumns := ''; Sheet.PageSetup.LeftFooter := ' 注:页脚'+' 总共&N页'+'--第&P页'; //页脚 Sheet.PageSetup.LeftMargin := 30; //设置边距 Sheet.PageSetup.RightMargin := 30; Sheet.PageSetup.TopMargin := 30; Sheet.PageSetup.BottomMargin := 50; // Sheet.PageSetup.PrintQuality := 400; //分辨率(根据打印机确定) Sheet.PageSetup.CenterHorizontally := True;//是否水平居中 Sheet.PageSetup.CenterVertically := True; //是否垂直居中 Sheet.PageSetup.Orientation := 2; //横向打印 Sheet.PageSetup.Draft := False; //非草稿模式 // Sheet.PageSetup.FirstPageNumber := xlAutomatic; Sheet.PageSetup.BlackAndWhite := True; //黑白稿 Sheet.PageSetup.Zoom := 100; //缩放 sheet.PrintPreView; //打印预览 except Showmessage('初始化Excel失败,可能没装Excel,或者其他错误;请重起再试。'); v.DisplayAlerts := false; v.Quit; exit; end; end;end;//EXCEL文档格式设置procedure TForm1.Button5Click(Sender: TObject);var Range: Variant;begin if OpenDialog1.Execute then begin try v:= CreateOleObject('Excel.Application'); v.Visible := CheckBox1.Checked; v.Workbooks.Open(OpenDialog1.FileName); Range := v.Workbooks[1].WorkSheets[1].Range['A2:G2'];//单元格从A2到M2 Range.Merge; //合并单元格 Range.Rows.RowHeight := 50; //设置行高 Range.Borders.LineStyle := 1; //加边框 Range.Columns[2].ColumnWidth := 12; // 设置列宽 Range.FormulaR1C1 := '合并区'; Range.HorizontalAlignment := 3;//xlCenter(水平对齐方式) Range.VerticalAlignment := 2;//xlCenter(垂直对齐方式) Range.Characters.Font.Name := '宋体'; //字体 Range.Characters.Font.FontStyle := '加粗'; Range.Characters.Font.Size := 15; Range.Characters.Font.OutlineFont := False; //是否有下划线 Range.Characters.Font.ColorIndex := 0;//xlAutomatic; //颜色 except Showmessage('初始化Excel失败,可能没装Excel,或者其他错误;请重起再试。'); v.DisplayAlerts := false; v.Quit; exit; end; end;end;end. delphi 如何把登陆界面的用户名在主界面显示出来? 急啊~~~ 程序在运行,但是不响应任何鼠标键盘消息,不知怎么回事? DBAdvGrid问题 请大家帮忙翻译一下c++builder的程序,很简单的 转了一圈,又回到原点 如何将图象设为墙纸? 请问一个控件,点击鼠标左键,出现上弹式菜单 100相送!!!! 请问如何中止线程中正在执行的查询? 数据库 部分表的备份与还原 大家帮忙看看这个SQL语句错在哪里? 用过fastreport的大侠请进来看下......急!!谢谢!!
begin try
xls:=CreateOleObject('Excel.Application'); //调用 Excel 需要在单元加上ComObj;
except
// Msgbox(sMsgNotInstallExcel,sTitleError,1);
exit;
end;if not (FileExists(extractfil+'\pring\报价单.xls')) then
xls.WorkBooks.add //增加一个表
else xls.WorkBooks.open(extractfil+'\pring\报价单.xls');//打开一个设计好的表
xls.cells(4,2):='100';//写入数据
xls.ActiveSheet.PrintPreview;//打印一览end;
uses comobj;var
xls:Variant;
begin try
xls:=CreateOleObject('Excel.Application'); //调用 Excel 需要在单元加上ComObj;
except
// Msgbox(sMsgNotInstallExcel,sTitleError,1);
xls:=unAssigned;
exit;
end; if not (FileExists(extractfil+'\pring\报价单.xls')) then
xls.WorkBooks.add //增加一个表
else
xls.WorkBooks.open(extractfil+'\pring\报价单.xls');//打开一个设计好的表
xls.cells(4,2):='100';//写入数据
xls.Visible:=True;
xls.ActiveSheet.PrintPreview;//打印一览end;
如果用xls.quit 会提示是否保存.
如果是打印的 的话 我就用 FastReport 来做报表
http://www.anylib.com
如果用xls.quit 会提示是否保存.//不出現提示
xls.DisplayAlerts := False;
konhon(优华)
就OK
你们又是如何知道“xls.DisplayAlerts ”之类的方法的呢?
在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet。
1) 打开Excel
ExcelApplication1.Connect;
2) 显示当前窗口:
ExcelApplication1.Visible[0]:=True;
3) 更改 Excel 标题栏:
ExcelApplication1.Caption := '应用程序调用 Microsoft Excel';
4) 添加新工作簿:
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(EmptyParam,0));
5) 添加新工作表:
var Temp_Worksheet: _WorkSheet;
begin
Temp_Worksheet:=ExcelWorkbook1.
WorkSheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _WorkSheet;
ExcelWorkSheet1.ConnectTo(Temp_WorkSheet);
End;
6) 打开已存在的工作簿:
ExcelApplication1.Workbooks.Open (c:\a.xls
EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,0)
7) 设置第2个工作表为活动工作表:
ExcelApplication1.WorkSheets[2].Activate; 或
ExcelApplication1.WorksSheets[ 'Sheet2' ].Activate;
8) 给单元格赋值:
ExcelApplication1.Cells[1,4].Value := '第一行第四列';
9) 设置指定列的宽度(单位:字符个数),以第一列为例:
ExcelApplication1.ActiveSheet.Columns[1].ColumnsWidth := 5;
10) 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例:
ExcelApplication1.ActiveSheet.Rows[2].RowHeight := 1/0.035; // 1厘米
11) 在第8行之前插入分页符:
ExcelApplication1.WorkSheets[1].Rows[8].PageBreak := 1;
12) 在第8列之前删除分页符:
ExcelApplication1.ActiveSheet.Columns[4].PageBreak := 0;
13) 指定边框线宽度:
ExcelApplication1.ActiveSheet.Range[ 'B3:D4' ].Borders[2].Weight := 3;
1-左 2-右 3-顶 4-底 5-斜( \ ) 6-斜( / )
14) 清除第一行第四列单元格公式:
ExcelApplication1.ActiveSheet.Cells[1,4].ClearContents;
15) 设置第一行字体属性:
ExcelApplication1.ActiveSheet.Rows[1].Font.Name := '隶书';
ExcelApplication1.ActiveSheet.Rows[1].Font.Color := clBlue;
ExcelApplication1.ActiveSheet.Rows[1].Font.Bold := True;
ExcelApplication1.ActiveSheet.Rows[1].Font.UnderLine := True;
16) 进行页面设置:
a.页眉:
ExcelApplication1.ActiveSheet.PageSetup.CenterHeader := '报表演示';
b.页脚:
ExcelApplication1.ActiveSheet.PageSetup.CenterFooter := '第&P页';
c.页眉到顶端边距2cm:
ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin := 2/0.035;
d.页脚到底端边距3cm:
ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin := 3/0.035;
e.顶边距2cm:
ExcelApplication1.ActiveSheet.PageSetup.TopMargin := 2/0.035;
f.底边距2cm:
ExcelApplication1.ActiveSheet.PageSetup.BottomMargin := 2/0.035;
g.左边距2cm:
ExcelApplication1.ActiveSheet.PageSetup.LeftMargin := 2/0.035;
h.右边距2cm:
ExcelApplication1.ActiveSheet.PageSetup.RightMargin := 2/0.035;
i.页面水平居中:
ExcelApplication1.ActiveSheet.PageSetup.CenterHorizontally := 2/0.035;
j.页面垂直居中:
ExcelApplication1.ActiveSheet.PageSetup.CenterVertically := 2/0.035;
k.打印单元格网线:
ExcelApplication1.ActiveSheet.PageSetup.PrintGridLines := True;
17) 拷贝操作:
a.拷贝整个工作表:
ExcelApplication1.ActiveSheet.Used.Range.Copy;
b.拷贝指定区域:
ExcelApplication1.ActiveSheet.Range[ 'A1:E2' ].Copy;
c.从A1位置开始粘贴:
ExcelApplication1.ActiveSheet.
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, {if delphi6.0 add ...}Variants{d6};type
TForm1 = class(TForm)
Button1: TButton;
CheckBox1: TCheckBox;
Button2: TButton;
OpenDialog1: TOpenDialog;
Button3: TButton;
Button4: TButton;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
v:Variant;
Sheet: Variant;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}uses Comobj;//打开EXCEL新建文件
procedure TForm1.Button1Click(Sender: TObject);
begin
try
v:= CreateOleObject('Excel.Application');
v.Visible := CheckBox1.Checked;//是否显示
v.Workbooks.Add;//新建EXCEL文件
v.Workbooks[1].WorkSheets[1].Name := 'DELPHI演示';
// Sheet:= v.Workbooks[1].WorkSheets['DELPHI演示'];//等效下面的语句
Sheet:= v.Workbooks[1].WorkSheets[1];
Sheet.Cells[1,1] :='DELPHI盒子';
Sheet.Cells[2,1] :='http://www.delphibox.com';
except
Showmessage('初始化Excel失败,可能没装Excel,或者其他错误;请重起再试。');
v.DisplayAlerts := false;//是否提示存盘
v.Quit;//如果出错则退出
exit;
end;
Application.Restore;
Application.BringToFront;
end;//打开存在的EXCEL文档
procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
try
v:= CreateOleObject('Excel.Application');
v.Visible := CheckBox1.Checked;
v.Workbooks.Open(OpenDialog1.FileName);
// Sheet := v.Workbooks[1].WorkSheets[1];
except
Showmessage('初始化Excel失败,可能没装Excel,或者其他错误;请重起再试。');
v.DisplayAlerts := false;
v.Quit;
exit;
end;
Application.Restore;
Application.BringToFront;
end;
end;//关闭EXCEL并退出
procedure TForm1.Button3Click(Sender: TObject);
begin
try
If not varIsEmpty(v) then
begin
// 如果需要在关闭前确定是否存盘,加入:
// v.DiaplayAlert:=true; //确定存盘
// v.DiaplayAlert:=false;//不存盘,直接退出
v.WorkBooks[1].Close(True, 'C:\untitled.xls'); //取文件名退出
v.quit;
end;
finally
Close;
end;
end;//EXCEL打印页面设置
procedure TForm1.Button4Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
try
v:= CreateOleObject('Excel.Application');
v.Visible := CheckBox1.Checked;
v.Workbooks.Open(OpenDialog1.FileName);
Sheet:=v.Workbooks[1].WorkSheets[1];
Sheet.PageSetup.PrintTitleRows :='$1:$3'; //页眉
Sheet.PageSetup.PrintTitleColumns := '';
Sheet.PageSetup.LeftFooter := ' 注:页脚'+' 总共&N页'+'--第&P页'; //页脚
Sheet.PageSetup.LeftMargin := 30; //设置边距
Sheet.PageSetup.RightMargin := 30;
Sheet.PageSetup.TopMargin := 30;
Sheet.PageSetup.BottomMargin := 50;
// Sheet.PageSetup.PrintQuality := 400; //分辨率(根据打印机确定)
Sheet.PageSetup.CenterHorizontally := True;//是否水平居中
Sheet.PageSetup.CenterVertically := True; //是否垂直居中
Sheet.PageSetup.Orientation := 2; //横向打印
Sheet.PageSetup.Draft := False; //非草稿模式
// Sheet.PageSetup.FirstPageNumber := xlAutomatic;
Sheet.PageSetup.BlackAndWhite := True; //黑白稿
Sheet.PageSetup.Zoom := 100; //缩放
sheet.PrintPreView; //打印预览
except
Showmessage('初始化Excel失败,可能没装Excel,或者其他错误;请重起再试。');
v.DisplayAlerts := false;
v.Quit;
exit;
end;
end;
end;//EXCEL文档格式设置
procedure TForm1.Button5Click(Sender: TObject);
var
Range: Variant;
begin
if OpenDialog1.Execute then
begin
try
v:= CreateOleObject('Excel.Application');
v.Visible := CheckBox1.Checked;
v.Workbooks.Open(OpenDialog1.FileName);
Range := v.Workbooks[1].WorkSheets[1].Range['A2:G2'];//单元格从A2到M2
Range.Merge; //合并单元格
Range.Rows.RowHeight := 50; //设置行高
Range.Borders.LineStyle := 1; //加边框
Range.Columns[2].ColumnWidth := 12; // 设置列宽
Range.FormulaR1C1 := '合并区';
Range.HorizontalAlignment := 3;//xlCenter(水平对齐方式)
Range.VerticalAlignment := 2;//xlCenter(垂直对齐方式)
Range.Characters.Font.Name := '宋体'; //字体
Range.Characters.Font.FontStyle := '加粗';
Range.Characters.Font.Size := 15;
Range.Characters.Font.OutlineFont := False; //是否有下划线
Range.Characters.Font.ColorIndex := 0;//xlAutomatic; //颜色
except
Showmessage('初始化Excel失败,可能没装Excel,或者其他错误;请重起再试。');
v.DisplayAlerts := false;
v.Quit;
exit;
end;
end;
end;end.