码迷,mamicode.com
首页 > 其他好文 > 详细

Delphi中控制Excel(转载)

时间:2014-07-16 23:17:35      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   数据   width   

用Delphi从数据库中取得资料,然后导出到Excel中做成报表是个不错的选择,因为Excel强大的报表功能那可是没话说
bubuko.com,布布扣前提
bubuko.com,布布扣Delphi中要
bubuko.com,布布扣uses comobj;
bubuko.com,布布扣var Excel:Variant;
bubuko.com,布布扣一、Excel操作
bubuko.com,布布扣1.创建Excel对象
bubuko.com,布布扣Excel := CreateOleObject( Excel.Application );
bubuko.com,布布扣2.显示Excel
bubuko.com,布布扣Excel.Visible:=true;
bubuko.com,布布扣3.更改标题
bubuko.com,布布扣Excel.Caption:=Excel的标题;
bubuko.com,布布扣4.退出Excel
bubuko.com,布布扣Excel.Quit;
bubuko.com,布布扣二、工作表操作
bubuko.com,布布扣1.添加新工作簿(刚创建的Excel对象是没有工作表的):
bubuko.com,布布扣Excel.WorkBooks.Add;
bubuko.com,布布扣2.打开已存在的工作簿:
bubuko.com,布布扣Excel.WorkBooks.Open( C:ExcelDemo.xls );
bubuko.com,布布扣3.设置第2个工作表为活动工作表:
bubuko.com,布布扣Excel.WorkSheets[2].Activate;
bubuko.com,布布扣
bubuko.com,布布扣Excel.WorksSheets[Sheet2].Activate;
bubuko.com,布布扣4.打印预览工作表:
bubuko.com,布布扣Excel.ActiveSheet.PrintPreview;
bubuko.com,布布扣5.打印输出工作表:
bubuko.com,布布扣Excel.ActiveSheet.PrintOut;
bubuko.com,布布扣6.工作表另存为:
bubuko.com,布布扣Excel.SaveAs( C:ExcelDemo1.xls );
bubuko.com,布布扣7.判断是否已经保存:
bubuko.com,布布扣if not Excel.ActiveWorkBook.Saved then
bubuko.com,布布扣Excel.ActiveSheet.PrintPreview;
bubuko.com,布布扣8.放弃存盘:
bubuko.com,布布扣Excel.ActiveWorkBook.Saved := True;
bubuko.com,布布扣9.关闭工作簿:
bubuko.com,布布扣Excel.WorkBooks.Close;
bubuko.com,布布扣10.删除“表1”工作表:
bubuko.com,布布扣Excel.Sheets(表1).delete;
bubuko.com,布布扣11.改“sheet1”工作表名为“123”
bubuko.com,布布扣Excel.Sheets(Sheet1).Name := 123;
bubuko.com,布布扣三、行/列设置
bubuko.com,布布扣1.设置指定列的宽度(单位:字符个数),以第一列为例:
bubuko.com,布布扣Excel.ActiveSheet.Columns[1].ColumnsWidth := 5;
bubuko.com,布布扣2.设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例:
bubuko.com,布布扣Excel.ActiveSheet.Rows[2].RowHeight := 1/0.035; // 1厘米
bubuko.com,布布扣3.插入一行或一列:
bubuko.com,布布扣a. Excel.ActiveSheet.Rows[2].Insert;
bubuko.com,布布扣b. Excel.ActiveSheet.Columns[1].Insert;
bubuko.com,布布扣4.删除一行或一列:
bubuko.com,布布扣a. Excel.ActiveSheet.Rows[2].Delete;
bubuko.com,布布扣b. Excel.ActiveSheet.Columns[1].Delete;
bubuko.com,布布扣5.自适应宽度
bubuko.com,布布扣Excel.ActiveSheet.Columns[1].EntireColumn.AutoFit; //第一列
bubuko.com,布布扣Excel.Cells.EntireColumn.AutoFit; //整个表所有列
bubuko.com,布布扣Excel.ActiveSheet.Rows[1:1].EntireRow.AutoFit; //第一行
bubuko.com,布布扣Excel.ActiveSheet.Columns[A:A].EntireColumn.AutoFit; //第一列
bubuko.com,布布扣6.居中显示J列
bubuko.com,布布扣Excel.Columns[J:J].HorizontalAlignment:=3;
bubuko.com,布布扣7.设置E列单元格式为3位小数数值
bubuko.com,布布扣Excel.Columns[E:E].NumberFormatLocal:=#,##0.000_
bubuko.com,布布扣8.设置E列单元格式为文字
bubuko.com,布布扣Excel.Columns[E:E].NumberFormatLocal:=@
bubuko.com,布布扣
bubuko.com,布布扣四、单元格设置
bubuko.com,布布扣1.设置单元格线框
bubuko.com,布布扣Excel.ActiveSheet.Range[B10:C13].Borders[N].LineStyle := xlNone
bubuko.com,布布扣Excel.ActiveSheet.Range[B10:C13].Borders[N].Weight := xlThin
bubuko.com,布布扣
bubuko.com,布布扣边框的类型 Borders[N]
bubuko.com,布布扣xlEdgeLeft 左=1
bubuko.com,布布扣xlEdgeRight 右=2
bubuko.com,布布扣xlEdgeTop 顶=3
bubuko.com,布布扣xlEdgeBottom 底=4
bubuko.com,布布扣xlDiagonalUp 左上右下=5
bubuko.com,布布扣xlDiagonalDown 左下右上=6
bubuko.com,布布扣xlEdgeLeft 外部左边框=7
bubuko.com,布布扣xlEdgeTop 外部上边框=8
bubuko.com,布布扣xlEdgeBottom 外部下边框=9
bubuko.com,布布扣xlEdgeRight 外部右边框=10
bubuko.com,布布扣xlInsideVertical 内部竖线=11
bubuko.com,布布扣xlInsideHorizontal 内部横线=12
bubuko.com,布布扣线条类型LineStyle,宽度Weight
bubuko.com,布布扣第7种=1,1
bubuko.com,布布扣第6种=2,2
bubuko.com,布布扣第3种=3,2
bubuko.com,布布扣第6种=4,2
bubuko.com,布布扣第4种=5,2
bubuko.com,布布扣第9种=6,3
bubuko.com,布布扣第12种=1,3
bubuko.com,布布扣第13种=1,4
bubuko.com,布布扣第2种=2,1
bubuko.com,布布扣单条线的LineStyle := xlContinuous
bubuko.com,布布扣双条线的LineStyle := xlDouble
bubuko.com,布布扣虚线 xlHairline 1
bubuko.com,布布扣实线 xlThin
bubuko.com,布布扣中实线 xlMedium
bubuko.com,布布扣粗实线 xlThick
bubuko.com,布布扣XLLineStyle等值参考Excel里面的VisualBasic对象浏览器帮助 (如图)
bubuko.com,布布扣2.给单元格赋值:
bubuko.com,布布扣Excel.Cells[1,4].Value := 第一行第四列;
bubuko.com,布布扣3.设置第一行字体属性(隶书,蓝色,加粗,下划线):
bubuko.com,布布扣Excel.ActiveSheet.Rows[1].Font.Name := 隶书;
bubuko.com,布布扣Excel.ActiveSheet.Rows[1].Font.Color := clBlue;
bubuko.com,布布扣Excel.ActiveSheet.Rows[1].Font.Bold := True;
bubuko.com,布布扣Excel.ActiveSheet.Rows[1].Font.UnderLine := True;
bubuko.com,布布扣4.设置整个表字体为9
bubuko.com,布布扣Excel.Cells.Font.Size:=9;
bubuko.com,布布扣5.在第8行之前插入/删除分页符:
bubuko.com,布布扣Excel.WorkSheets[1].Rows[8].PageBreak := 1; (0为删除)
bubuko.com,布布扣6.清除第一行第四列单元格公式:
bubuko.com,布布扣Excel.ActiveSheet.Cells[1,4].ClearContents;
bubuko.com,布布扣五、页面设置
bubuko.com,布布扣1.页脚和页眉
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.LeftHeader := 页眉左
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.CenterHeader := 页眉中
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.RightHeader := 页眉右
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.LeftFooter := 页脚左
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.CenterFooter := 页脚中
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.RightFooter := 页脚右
bubuko.com,布布扣2.页脚中间显示页数:
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.CenterFooter := 第&P页/共&N页;
bubuko.com,布布扣3.页眉到顶端边距2cm:
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.HeaderMargin := 2/0.035;
bubuko.com,布布扣4.页脚到底端边距3cm:
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.FooterMargin := 3/0.035;
bubuko.com,布布扣5.顶边距2cm:
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.TopMargin := 2/0.035;
bubuko.com,布布扣6.底边距2cm:
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.BottomMargin := 2/0.035;
bubuko.com,布布扣7.左边距2cm:
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.LeftMargin := 2/0.035;
bubuko.com,布布扣8.右边距2cm:
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.RightMargin := 2/0.035;
bubuko.com,布布扣9.页面水平居中:
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.CenterHorizontally := 2/0.035;
bubuko.com,布布扣10.页面垂直居中:
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.CenterVertically := 2/0.035;
bubuko.com,布布扣11.打印单元格网线:
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.PrintGridLines := True;
bubuko.com,布布扣12.横向打印
bubuko.com,布布扣Excel.ActiveSheet.PageSetup.Orientation:=2

Delphi中控制Excel(转载),布布扣,bubuko.com

Delphi中控制Excel(转载)

标签:style   http   color   os   数据   width   

原文地址:http://www.cnblogs.com/coolsundy/p/3811401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!