| 发表于:2007-06-11 13:24:142楼 得分:70 |
一) 使用动态创建的方法 首先创建 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:\excel\demo.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 then excelapp.activesheet.printpreview; 21) 工作表另存为: excelapp.saveas( 'c:\excel\demo1.xls ' ); 22) 放弃存盘: excelapp.activeworkbook.saved := true; 23) 关闭工作簿: excelapp.workbooks.close; 24) 退出 excel: excelapp.quit; | | |
|