*1.创建Excel对象 eole=CREATEOBJECT('Excel.application') * 2.添加新工作簿 eole.Workbooks.add 激活指定的工作簿 eole.WorkSheets("工作簿名").Activate * 3.设置第3个工作表为激活工作表 eole.Worksheets("sheet1").Activate * 4.打开指定工作簿 eole.Workbooks.add("E:/E_temp/ABC.xls") * 5.显示Excel窗口 eole.visible=.t. * 6.更改Excel标题栏 eole.Caption="VFP应用程序调用Microsoft Excel" * 7.给单元格赋值 * eole.cells(行,列).value=XM(XM为数据库字段名) eole.cells(1,4).value='ASDFASDFASDFASDFADSF' * 8.设置指定列的宽度(单位:字符个数) eole.ActiveSheet.Columns(1).ColumnWidth=5 * 9.设置指定行的高度(单位:磅) eole.ActiveSheet.Rows(1).RowHeight=1/0.035 && 设定行高为1厘米,1磅=0.035厘米 * (设定行高为1厘米,1磅=0.035厘米) * 10.在第18行之前插入分页符 eole.Worksheets("Sheet1").Rows(18).PageBreak=1 * 11.在第4列之前删除分页符 eole.ActiveSheet.Columns(4).PageBreak=0 * 12.指定边框线宽度(Borders参数如下) eole.ActiveSheet.Range("b3:d3").Borders(2).Weight=3 * 13.设置四个边框线条的类型 eole.ActiveSheet.Range("b3:d3").Borders(1).LineStyle=1 * (其中Borders参数:1-左、2-右、3-顶、4-底、5-斜、6-斜/;LineStyle 值:1与7-细实、2-细虚、4-点虚、9-双细实线) * 14.设置页眉 eole.ActiveSheet.PageSetup.CenterHeader="报表1" * 15.设置页脚 eole.ActiveSheet.PageSetup.CenterFooter="第&P页" * 16.设置页眉到顶端边距为2厘米 eole.ActiveSheet.PageSetup.HeaderMargin=2/0.035 * 17.设置页脚到底边距为3厘米 eole.ActiveSheet.PageSetup.FooterMargin=3/0.035 * 18.设置顶边距为2厘米 eole.ActiveSheet.PageSetup.TopMargin=2/0.035 * 19.设置底边距为4厘米 eole.ActiveSheet.PageSetup.BottomMargin=4/0.035 * 20.设置左边距为2厘米 eole.ActiveSheet.PageSetup.LeftMargin=2/0.035 * 21.设置右边距为2厘米 eole.ActiveSheet.PageSetup.RightMargin=2/0.035 * 22.设置页面水平居中 eole.ActiveSheet.PageSetup.CenterHorizontally=.t. * 23.设置页面垂直居中 eole.ActiveSheet.PageSetup.CenterVertically=.t. * 24.设置页面纸张大小(1-窄行8?5?11 39-宽行14?11) eole.ActiveSheet.PageSetup.PaperSize=1 可为下列 XlPaperSize 常量之一(某些打印机可能不支持所有的这些纸张大小); 常量 数值 意义; xlPaperLetter 1 Letter (8-1/2 in. x 11 in.) xlPaperA3 8 A3 (297 mm x 420 mm) xlPaperA4 9 A4 (210 mm x 297 mm) xlPaperA4Small 10 A4 Small (210 mm x 297 mm) xlPaperA5 11 A5 (148 mm x 210 mm) xlPaperB4 12 B4 (250 mm x 354 mm) xlPaperB5 13 B5 (182 mm x 257 mm) xlPaperFanfoldUS 39 U.S. Standard Fanfold (14-7/8 in. x 11 in.) xlPaperUser 用户自定义 * 25.打印单元格网线 eole.ActiveSheet.PageSetup.PrintGridlines=.t. * 26.拷贝整个工作表 eole.ActiveSheet.UsedRange.Copy * 27.拷贝指定区域 eole.ActiveSheet.Range("A1:E2").Copy * 28.粘贴 eole.Worksheets("sheet2").Activate eole.ActiveSheet.Range("F1").PasteSpecial * 29.在第2行之前插入一行 eole.ActiveSheet.Rows(2).Insert * 30.在第2列之前插入一列 eole.ActiveSheet.Columns(2).Insert * 31.设置字体 eole.ActiveSheet.Cells(2,1).Font.Name="黑体" * 32.设置字体大小 eole.ActiveSheet.Cells(1,1).Font.Size=25 * 33.设置字体为斜体 eole.ActiveSheet.Cells(1,1).Font.Italic=.t. * 34.设置整列字体为粗体 eole.ActiveSheet.Columns(1).Font.Bold=.t. 35.合并单元格,重要! _ole_excel.Worksheets("工作簿名").Range(范围).merge && 范围必须指定起止的单元格,如"A1:B4" eole.ActiveSheet.Range("A1:B4").merge &&合并当前工作表中指定范围 36.撤销合并单元格,上述操作的逆操作 _ole_excel.Worksheets("工作簿名").Range(范围).unmerge 37.在单元格中设置公式(重要,一般可以用来实现计算汇总、求平均等很多功能) _ole_excel.cells(行,列).value = "=公式" && 可以使用所有VBA内部函数,如sum()等。注意:不能使用VFP的函数啊! * 35.清除单元格公式 eole.ActiveSheet.Cells(1,4).ClearContents * 36.打印预览工作表 eole.ActiveSheet.PrintPreview * 37.打印输出工作表 eole.ActiveSheet.PrintOut * 38.工作表另为 eole.ActiveWorkbook.SaveAs("c:/temp/22.xls") * 39.放弃存盘 eole.ActiveWorkbook.saved=.t. * 40.关闭工作簿 WAIT WINDOW 'ASDFASDF' eole.Workbooks.close * 41.退出Excel eole.quit