控件:ExcelApplication1、ExcelWorkbook1var
VarWorkSheet:Variant;
begin
ExcelApplication1.Connect;
ExcelWorkbook1.ConnectTo(ExcelApplication1.ActiveWorkbook);
VarWorkSheet:=ExcelApplication1.WorkSheets.Item[1];
//这里如何设置WorkSheet的字体格式(整个工作表)
//这里如何合并WorkSheet的某几个单元格,如'A1:G1'
//....赋值 VarWorkSheet.SaveAs(FileName);
ExcelWorkbook1.Saved[1]:=True;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Disconnect;
end;*****请就我的代码给与答案******
VarWorkSheet:Variant;
begin
ExcelApplication1.Connect;
ExcelWorkbook1.ConnectTo(ExcelApplication1.ActiveWorkbook);
VarWorkSheet:=ExcelApplication1.WorkSheets.Item[1];
//这里如何设置WorkSheet的字体格式(整个工作表)
//这里如何合并WorkSheet的某几个单元格,如'A1:G1'
//....赋值 VarWorkSheet.SaveAs(FileName);
ExcelWorkbook1.Saved[1]:=True;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Disconnect;
end;*****请就我的代码给与答案******
解决方案 »
- FastReport的数字预览显示,在界面又不显示。
- 请问如何让listview上的column中的文字居中对齐?默认为靠左对齐,另外如何改变它的默认字体?
- 程序打包的问题。
- 从资源文件中调用dll
- dbgrid控件如何能在左边显示行号?
- 完全动态生成报表问题??????
- 怎么获取本地连接的ip地址
- 分多了拿在手上没有用,散分,限前20人(每人5分)!
- ShellExecute实现调用并登陆另一个程序
- 用delphi编写的dll中函数返回类型不能为string,否则其他程序调用时就会出错?请问如何解决?
- 如何在保存excel文件时去掉是否保存的提示?
- 怎样用代码控制EXCEL ,并用代码合并和拆散单元格向里面添加图片
(一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp := CreateOleObject( 'Excel.Application' );1) 显示当前窗口:ExcelApp.Visible := True;2) 更改 Excel 标题栏:ExcelApp.Caption := '应用程序调用 Microsoft Excel';3) 添加新工作簿:ExcelApp.WorkBooks.Add;4) 打开已存在的工作簿:ExcelApp.WorkBooks.Open( 'C:.xls' );5) 设置第2个工作表为活动工作表:ExcelApp.WorkSheets[2].Activate;或ExcelApp.WorksSheets[ 'Sheet2' ].Activate;6) 给单元格赋值:ExcelApp.Cells[1,4].Value := '第一行第四列';7) 设置指定列的宽度(单位:字符个数),以第一列为例:ExcelApp.ActiveSheet.Columns[1].ColumnsWidth := 5;8) 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例:ExcelApp.ActiveSheet.Rows[2].RowHeight := 1/0.035; // 1厘米9) 在第8行之前插入分页符:ExcelApp.WorkSheets[1].Rows[8].PageBreak := 1;10) 在第8列之前删除分页符:ExcelApp.ActiveSheet.Columns[4].PageBreak := 0;11) 指定边框线宽度:ExcelApp.ActiveSheet.Range[ 'B3:D4' ].Borders[2].Weight := 3;1-左 2-右 3-顶 4-底 5-斜( ) 6-斜( / )12) 清除第一行第四列单元格公式:ExcelApp.ActiveSheet.Cells[1,4].ClearContents;13) 设置第一行字体属性:ExcelApp.ActiveSheet.Rows[1].Font.Name := '隶书';ExcelApp.ActiveSheet.Rows[1].Font.Color := clBlue;ExcelApp.ActiveSheet.Rows[1].Font.Bold := True;ExcelApp.ActiveSheet.Rows[1].Font.UnderLine := True;14) 进行页面设置:a.页眉:ExcelApp.ActiveSheet.PageSetup.CenterHeader := '报表演示';b.页脚:ExcelApp.ActiveSheet.PageSetup.CenterFooter := '第&P页';c.页眉到顶端边距2cm:ExcelApp.ActiveSheet.PageSetup.HeaderMargin := 2/0.035;d.页脚到底端边距3cm:ExcelApp.ActiveSheet.PageSetup.HeaderMargin := 3/0.035;e.顶边距2cm:ExcelApp.ActiveSheet.PageSetup.TopMargin := 2/0.035;f.底边距2cm:ExcelApp.ActiveSheet.PageSetup.BottomMargin := 2/0.035;g.左边距2cm:ExcelApp.ActiveSheet.PageSetup.LeftMargin := 2/0.035;h.右边距2cm:ExcelApp.ActiveSheet.PageSetup.RightMargin := 2/0.035;i.页面水平居中:ExcelApp.ActiveSheet.PageSetup.CenterHorizontally := 2/0.035;j.页面垂直居中:ExcelApp.ActiveSheet.PageSetup.CenterVertically := 2/0.035;k.打印单元格网线:ExcelApp.ActiveSheet.PageSetup.PrintGridLines := True;15) 拷贝操作:a.拷贝整个工作表:ExcelApp.ActiveSheet.Used.Range.Copy;b.拷贝指定区域:ExcelApp.ActiveSheet.Range[ 'A1:E2' ].Copy;c.从A1位置开始粘贴:ExcelApp.ActiveSheet.Range.[ 'A1' ].PasteSpecial;d.从文件尾部开始粘贴:ExcelApp.ActiveSheet.Range.PasteSpecial;16) 插入一行或一列:a. ExcelApp.ActiveSheet.Rows[2].Insert;b. ExcelApp.ActiveSheet.Columns[1].Insert;17) 删除一行或一列:a. ExcelApp.ActiveSheet.Rows[2].Delete;b. ExcelApp.ActiveSheet.Columns[1].Delete;18) 打印预览工作表:ExcelApp.ActiveSheet.PrintPreview;19) 打印输出工作表:ExcelApp.ActiveSheet.PrintOut;20) 工作表保存:if not ExcelApp.ActiveWorkBook.Saved thenExcelApp.ActiveSheet.PrintPreview;21) 工作表另存为:ExcelApp.SaveAs( 'C:.xls' );22) 放弃存盘:ExcelApp.ActiveWorkBook.Saved := True;23) 关闭工作簿:ExcelApp.WorkBooks.Close;24) 退出 Excel:ExcelApp.Quit;(二) 使用Delphi 控件方法在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet。1) 打开ExcelExcelApplication1.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:.xlsEmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,0)7) 设置第2个工作表为活动工作表:ExcelApplication1.WorkSheets[2].Activate; 或ExcelApplication1.WorksSheets[ 'Sheet2' ].Activate;
uses Windows, SysUtils, Messages, Dialogs, Classes, Forms, OleCtnrs, OleServer, Excel97, ComObj;Const ReNo=23; //一页显示的记录数
Const MAX=35; //最大的数组个数Var
ExlApp:OleVariant;
ExlBook:OleVariant; function GetRepRange(x,y:integer):String; //将(x,y)坐标形式改为Excel区域(A1:B1)形式
procedure CellMerge(x1,y1,x2,y2:integer); //合并指定单元格
procedure SetRepLine(x,y:Integer); //加边框线
procedure CellWrite(RepData: String; x,y:Integer); //单元格写数据
procedure CellFormat(x1,y1,x2,y2:integer); //指定单元格格式
procedure CellGS(x1,y1,x2,y2,f:integer); //灵活单元格格式 procedure RepCreat; //创建OLE对象(Excel Application与WorkBook)
procedure CreatRepSheet(SheetName:String;PageSize,PageLay:Integer); //新建工作簿、页面设置
procedure SetAddMess(H_Mess1,H_Mess2,H_Mess3,F_Mess1,F_Mess2,F_Mess3:String); //设置附加信息
procedure SetRepBody(x,ch:Integer;cw:Double;cf:String); //设置整体各列数据格式
procedure CreatTitle(TitleName:String;y:Integer); //设置标题
procedure CreatSubHead(SubTitle: Array of String); //设置常规子表头
procedure SubHeadFormat(y,r:Integer); //设置子表头格式
procedure DTSubHeadGS(x,y,r:Integer); //设置动态子表头格式
procedure WriteData(RepData: String; x,y,flag:Integer); //写入数据
procedure RepPageBreak(x,y,r:Integer); //分页、复制表头
procedure RepSaveAs(FileName:String); //保存为*.xls文件
procedure RepPrivew(FileName:String); //预览
procedure RepQuit; //退出Excel
procedure RepDestroy; //非正常退出Excelimplementationfunction GetRepRange(x,y:integer):string;
var fX,fY:string;
begin
if y<=0 then
fX:='A';
if y<=26 then
fX := chr(64+y);
if y>26 then
fX:=chr(64+(y div 26))+chr(64+(y mod 26)); fY:=IntToStr(x);
Result:=fX+fY;
end;procedure CellMerge(x1,y1,x2,y2:integer);
{合并指定单元格}
Var
RepSpace:String;
begin
RepSpace:=GetRepRange(x1,y1)+':'+GetRepRange(x2,y2);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.Merge;
end;{CellMerge}procedure CellFormat(x1,y1,x2,y2:integer);
{指定单元格格式}
Var
RepSpace:String;
begin
RepSpace:=GetRepRange(x1,y1)+':'+GetRepRange(x2,y2);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.NumberFormat :='G/通用格式';
ExlApp.Selection.Font.Bold:=True;
ExlApp.Selection.HorizontalAlignment:=3; //水平方向对齐方式:居中end;{CellFormat}procedure CellGS(x1,y1,x2,y2,f:integer);
{灵活单元格格式}
Var
RepSpace:String;
begin
RepSpace:=GetRepRange(x1,y1)+':'+GetRepRange(x2,y2);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.NumberFormat :='G/通用格式';
ExlApp.Selection.HorizontalAlignment:=f; //水平方向对齐方式:居中
end;{CellGS}procedure SetRepLine(x,y:Integer);
{加边框线}
Var
RepSpace:String;
begin
RepSpace:=GetRepRange(x,1)+':'+GetRepRange(x,y);
ExlApp.ActiveSheet.Range[RepSpace].Borders.LineStyle:=xlContinuous;
end;{SetRepLine}procedure CellWrite(RepData: String; x,y:Integer);
{单元格写数据}
begin
ExlApp.cells(x,y):=RepData;
end;{CellWrite}procedure RepCreat;
{创建Excel对象}
begin
try
ExlApp:=CreateOLEObject('Excel.Application');
ExlBook:=CreateOLEObject('Excel.Sheet');
ExlApp.Visible :=False;// True;
ExlApp.DisplayAlerts := False;
except
MessageDlg('您的机器里未安装Microsoft Excel!', mtError, [mbOk], 0);
Exit;
end;{try}
end;{RepCreat}
procedure CreatRepSheet(SheetName:String;PageSize,PageLay:Integer);
{新建Excel工作簿、进行页面设置}
begin
{新建Excel工作簿}
if ExlApp.WorkBooks.Count<1 then
begin
ExlBook:=ExlApp.Workbooks.Add; //ExlBook:=ExlApp.WorkBooks[1].WorkSheets[1];
ExlApp.ActiveSheet.Name:=SheetName;
end;{if} {进行页面设置}以上是怎么不用空间而调用EXCEL的
很方便的!
//设置页面
if PageSize=1 then
ExlApp.ActiveSheet.PageSetup.PaperSize:=xlPaperA3; //纸张大小 :A3
if PageSize=2 then
ExlApp.ActiveSheet.PageSetup.PaperSize:=xlPaperA4; //纸张大小 :A4
if PageSize=3 then
ExlApp.ActiveSheet.PageSetup.PaperSize:=xlPaperB5; //纸张大小 :B5
if PageLay=1 then
ExlApp.ActiveSheet.PageSetup.Orientation:=xlportrait; //页面放置方向:纵向
if PageLay=2 then
ExlApp.ActiveSheet.PageSetup.Orientation:=xlLandscape; //页面放置方向:横向 //设置页宽自动适应
ExlApp.ActiveSheet.PageSetup.Zoom := False;
ExlApp.ActiveSheet.PageSetup.FitToPagesWide := 1;
ExlApp.ActiveSheet.PageSetup.FitToPagesTall := False; //设置页眉、页脚(即:页标题、页号)
ExlApp.ActiveSheet.PageSetup.RightFooter:='打印时间: '+'&D &T';
ExlApp.ActiveSheet.PageSetup.CenterFooter:='第&''&P&''页,共&''&N&''页';
ExlApp.ActiveSheet.PageSetup.TopMargin:=1.5/0.035;
ExlApp.ActiveSheet.PageSetup.BottomMargin:=1.5/0.035;
ExlApp.ActiveSheet.PageSetup.LeftMargin:=1/0.035;
ExlApp.ActiveSheet.PageSetup.RightMargin:=1/0.035;
ExlApp.ActiveSheet.PageSetup.HeaderMargin:=0.5/0.035;
ExlApp.ActiveSheet.PageSetup.FooterMargin:=0.5/0.035; //设置页面对齐方式
ExlApp.ActiveSheet.PageSetup.CenterHorizontally:=True; //页面水平居中
// ExlApp.ActiveSheet.PageSetup.CenterVertically :=True; //页面垂直居中 //设置整体字体格式
ExlApp.Cells.Font.Name:='宋体'; //字体
ExlApp.Cells.Font.Size:=12; //字号
ExlApp.Cells.RowHeight:=16; //行高
ExlApp.Cells.VerticalAlignment:=2; //垂直方向对齐方式:居中
end;{CreatRepSheet}procedure SetAddMess(H_Mess1,H_Mess2,H_Mess3,F_Mess1,F_Mess2,F_Mess3:String);
//用户自定义页眉、页脚(即:页标题、页号)
begin
ExlApp.ActiveSheet.PageSetup.LeftHeader:=H_Mess1;
ExlApp.ActiveSheet.PageSetup.CenterHeader:=H_Mess2;
ExlApp.ActiveSheet.PageSetup.RightHeader:=H_Mess3;
end;{SetAddMess}procedure SetRepBody(x,ch:Integer;cw:Double;cf:String);
//设置整体各列数据格式
begin
ExlApp.ActiveSheet.Columns[x].ColumnWidth:=cw; //列宽
ExlApp.ActiveSheet.Columns[x].NumberFormat:=Cf; //单元格数据格式
ExlApp.ActiveSheet.Columns[x].HorizontalAlignment:=ch; //水平方向对齐方式
end;{SetRepBody}procedure CreatTitle(TitleName:String;y:Integer);
{设置标题}
Var
RepSpace:String;
begin
CellMerge(1,1,1,y);
ExlApp.cells(1,1):=TitleName;
RepSpace:='A1'+':'+GetRepRange(1,y);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.NumberFormat :='G/通用格式';
ExlApp.Selection.Font.Size:=22;
ExlApp.Selection.Font.Name:='黑体';
ExlApp.Selection.Font.Bold:=True;
ExlApp.Selection.HorizontalAlignment:=3; //水平方向对齐方式:居中
ExlApp.Rows[1].RowHeight:=28;
end;{RepHead}procedure CreatSubHead(SubTitle: Array of String);
{设置常规子表头}
Var
i,j:Integer;
begin
j:=0;
for i:=Low(SubTitle) to High(SubTitle) do
begin
Inc(j);
ExlApp.cells(2,j):=SubTitle[i];
end;
end;{CreatRepHead}procedure SubHeadFormat(y,r:Integer);
{设置子表头格式}
Var
RepSpace:String;
n:Integer;
begin
RepSpace:='A2'+':'+GetRepRange(1+r,y);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.NumberFormat :='G/通用格式';
ExlApp.Selection.HorizontalAlignment:=3; //表头水平对齐方式:居中
ExlApp.Selection.Font.Bold:=True;
for n:=1 to r do
begin
ExlApp.Rows[1+n].RowHeight:=18;
SetRepLine(1+n,y);
end;{for}
end;{SubHeadFormat}procedure DTSubHeadGS(x,y,r:Integer);
{设置动态子表头格式}
Var
RepSpace:String;
n:Integer;
begin
RepSpace:=GetRepRange(x,1)+':'+GetRepRange(x+r-1,y);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.NumberFormat :='G/通用格式';
ExlApp.Selection.HorizontalAlignment:=3; //表头水平对齐方式:居中
ExlApp.Selection.Font.Bold:=True;
for n:=0 to r-1 do
begin
ExlApp.Rows[x+n].RowHeight:=18;
SetRepLine(x+n,y);
end;{for}
end;{DTSubHeadGS}procedure WriteData(RepData: String; x,y,flag:Integer); //写入数据
{写数据}
begin
if flag=1 then
ExlApp.cells(x,y):=StrToDate(RepData)
else
ExlApp.cells(x,y):=RepData;
end;{WriteDate}procedure RepPageBreak(x,y,r:Integer); //X:分页处行数,Y:列数,R:子表头总共的行数
//分页、复制表头
Var
RepSpace:String;
n:Integer;
begin
ExlApp.ActiveSheet.Rows[x].PageBreak := 1;
RepSpace:='A1'+':'+GetRepRange(r+1,y);
ExlApp.ActiveSheet.Range[RepSpace].Copy;
RepSpace:='A'+IntToStr(x);
ExlApp.ActiveSheet.Range[RepSpace].PasteSpecial;
ExlApp.Rows[x].RowHeight:=28;
for n:=2 to r do
ExlApp.Rows[x+n].RowHeight:=18;
end;{RepPageBreak}procedure RepSaveAs(FileName:String);
{保存为*.xls文件}
begin
try
ExlBook.saveas(FileName);
except
MessageDlg('不能访问文件,请关闭Microsoft Excel后再运行本程序!', mtError, [mbOk], 0);
end;
end;{RepSaveAs}procedure RepPrivew(FileName:String);
{预览}
begin
RepCreat;
ExlApp.Visible :=True;
try
ExlApp.workBooks.Open(FileName);
ExlApp.Workbooks[1].WorkSheets[1].PrintPreview;
finally
ExlApp.Quit;
ExlApp:=Unassigned;
//ExlApp:='';
end;{try}
end;{RepPrivew}procedure RepQuit;
{退出Excel}
begin
ExlBook.Close;
ExlApp.Quit; //退出Excel Application
ExlApp:=Unassigned; //释放VARIANT变量
end;{RepQuit}procedure RepDestroy;
{非正常退出Excel}
begin
if Not VarIsEmpty(ExlApp) then
RepQuit;
end;{RepDestroy}end.
续上!!!!