Delphi中控制Excel

用Delphi从数据库中取得资料,然后导出到Excel中做成报表是个不错的选择,因为Excel强大的报表功能那可是没话说
前提
Delphi中要 
uses comobj;
var Excel:Variant;
一、Excel操作
1 .创建Excel对象
Excel :
=  CreateOleObject( Excel.Application );
2 .显示Excel
Excel.Visible:
= true ;
3 .更改标题
Excel.Caption:
= Excel的标题;
4 .退出Excel
Excel.Quit;
二、工作表操作
1 .添加新工作簿(刚创建的Excel对象是没有工作表的):
Excel.WorkBooks.Add;
2 .打开已存在的工作簿:
Excel.WorkBooks.Open( C:ExcelDemo.xls );
3 .设置第2个工作表为活动工作表:
Excel.WorkSheets[
2 ].Activate; 
或 
Excel.WorksSheets[Sheet2].Activate;
4 .打印预览工作表:
Excel.ActiveSheet.PrintPreview;
5 .打印输出工作表:
Excel.ActiveSheet.PrintOut;
6 .工作表另存为:
Excel.SaveAs( C:ExcelDemo1.xls );
7 .判断是否已经保存:
if  not Excel.ActiveWorkBook.Saved then
Excel.ActiveSheet.PrintPreview;
8 .放弃存盘:
Excel.ActiveWorkBook.Saved :
=  True;
9 .关闭工作簿:
Excel.WorkBooks.Close;
10 .删除“表1”工作表:
Excel.Sheets(表1).delete;
11 .改“sheet1”工作表名为“ 123
Excel.Sheets(Sheet1).Name :
=   123 ;
三、行
/ 列设置
1 .设置指定列的宽度(单位:字符个数),以第一列为例:
Excel.ActiveSheet.Columns[
1 ].ColumnsWidth : =   5 ;
2 .设置指定行的高度(单位:磅)(1磅= 0 .035厘米),以第二行为例:
Excel.ActiveSheet.Rows[
2 ].RowHeight : =   1 / 0.035 //  1厘米
3 .插入一行或一列:
a. Excel.ActiveSheet.Rows[
2 ].Insert;
b. Excel.ActiveSheet.Columns[
1 ].Insert;
4 .删除一行或一列:
a. Excel.ActiveSheet.Rows[
2 ].Delete;
b. Excel.ActiveSheet.Columns[
1 ].Delete;
5 .自适应宽度
Excel.ActiveSheet.Columns[
1 ].EntireColumn.AutoFit;  // 第一列
Excel.Cells.EntireColumn.AutoFit;  // 整个表所有列
Excel.ActiveSheet.Rows[ 1 : 1 ].EntireRow.AutoFit;  // 第一行
Excel.ActiveSheet.Columns[A:A].EntireColumn.AutoFit;  // 第一列
6 .居中显示J列
Excel.Columns[J:J].HorizontalAlignment:
= 3 ;
7 .设置E列单元格式为3位小数数值
Excel.Columns[E:E].NumberFormatLocal:
= #,## 0 .000_ 
8 .设置E列单元格式为文字
Excel.Columns[E:E].NumberFormatLocal:
= @

四、单元格设置
1 .设置单元格线框
Excel.ActiveSheet.Range[B10:C13].Borders[N].LineStyle :
=  xlNone
Excel.ActiveSheet.Range[B10:C13].Borders[N].Weight :
=  xlThin

边框的类型 Borders[N]
xlEdgeLeft 左
= 1
xlEdgeRight 右
= 2
xlEdgeTop 顶
= 3
xlEdgeBottom 底
= 4
xlDiagonalUp 左上右下
= 5
xlDiagonalDown 左下右上
= 6
xlEdgeLeft 外部左边框
= 7
xlEdgeTop 外部上边框
= 8
xlEdgeBottom 外部下边框
= 9
xlEdgeRight 外部右边框
= 10
xlInsideVertical 内部竖线
= 11
xlInsideHorizontal 内部横线
= 12
线条类型LineStyle,宽度Weight
第7种
= 1 , 1
第6种
= 2 , 2
第3种
= 3 , 2
第6种
= 4 , 2
第4种
= 5 , 2
第9种
= 6 , 3
第12种
= 1 , 3
第13种
= 1 , 4
第2种
= 2 , 1
单条线的LineStyle :
=  xlContinuous
双条线的LineStyle :
=  xlDouble
虚线 xlHairline 
1
实线 xlThin 
中实线 xlMedium 
粗实线 xlThick
XLLineStyle等值参考Excel里面的VisualBasic对象浏览器帮助 (如图)
2 .给单元格赋值:
Excel.Cells[
1 , 4 ].Value : =  第一行第四列;
3 .设置第一行字体属性(隶书,蓝色,加粗,下划线):
Excel.ActiveSheet.Rows[
1 ].Font.Name : =  隶书;
Excel.ActiveSheet.Rows[
1 ].Font.Color : =  clBlue;
Excel.ActiveSheet.Rows[
1 ].Font.Bold : =  True;
Excel.ActiveSheet.Rows[
1 ].Font.UnderLine : =  True;
4 .设置整个表字体为9
Excel.Cells.Font.Size:
= 9 ;
5 .在第8行之前插入 / 删除分页符:
Excel.WorkSheets[
1 ].Rows[ 8 ].PageBreak : =   1 ; (0为删除)
6 .清除第一行第四列单元格公式:
Excel.ActiveSheet.Cells[
1 , 4 ].ClearContents;
五、页面设置
1 .页脚和页眉
Excel.ActiveSheet.PageSetup.LeftHeader :
=  页眉左
Excel.ActiveSheet.PageSetup.CenterHeader :
=  页眉中
Excel.ActiveSheet.PageSetup.RightHeader :
=  页眉右
Excel.ActiveSheet.PageSetup.LeftFooter :
=  页脚左
Excel.ActiveSheet.PageSetup.CenterFooter :
=  页脚中
Excel.ActiveSheet.PageSetup.RightFooter :
=  页脚右
2 .页脚中间显示页数:
Excel.ActiveSheet.PageSetup.CenterFooter :
=  第 & P页 / & N页;
3 .页眉到顶端边距2cm:
Excel.ActiveSheet.PageSetup.HeaderMargin :
=   2 / 0.035 ;
4 .页脚到底端边距3cm:
Excel.ActiveSheet.PageSetup.FooterMargin :
=   3 / 0.035 ;
5 .顶边距2cm:
Excel.ActiveSheet.PageSetup.TopMargin :
=   2 / 0.035 ;
6 .底边距2cm:
Excel.ActiveSheet.PageSetup.BottomMargin :
=   2 / 0.035 ;
7 .左边距2cm:
Excel.ActiveSheet.PageSetup.LeftMargin :
=   2 / 0.035 ;
8 .右边距2cm:
Excel.ActiveSheet.PageSetup.RightMargin :
=   2 / 0.035 ;
9 .页面水平居中:
Excel.ActiveSheet.PageSetup.CenterHorizontally :
=   2 / 0.035 ;
10 .页面垂直居中:
Excel.ActiveSheet.PageSetup.CenterVertically :
=   2 / 0.035 ;
11 .打印单元格网线:
Excel.ActiveSheet.PageSetup.PrintGridLines :
=  True;
12 .横向打印
Excel.ActiveSheet.PageSetup.Orientation:
= 2  

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值