1.创建Excel对象 eole=CREATEOBJECT(′Excel.application′)
2.添加新工作簿
eole.Workbooks.add
3.设置第3个工作表为激活工作表
eole.Worksheets(″sheet3″).Activate
4.打开指定工作簿
eole.Workbooks.Open(″c:/temp/ll.xls″)
5.显示Excel窗口
eole.visible=.t.
6.更改Excel标题栏
eole.Caption=″VFP应用程序调用Microsoft Excel″
7.给单元格赋值
eole.cells(1,4).value=XM(XM为数据库字段名)
8.设置指定列的宽度(单位:字符个数)
eole.ActiveSheet.Columns(1).Column EndRowCol).Select
MyOLE.ActiveWorkBook.Sheets(worksheet).Range(BeginRowCol ":" EndRowCol).Merge
43 sheet改名
MyOLE.ActiveWorkBook.Sheets(olename).select
MyOLE.ActiveWorkBook.Sheets(olename).name=newname
MyOLE=Create OLEObject
COnnectErr=MyOLE.ConnectToNewObject ("excel.Application")
MyOLE.visible=false
//打开指定的XLS文件激活workbooks
MyOLE.application.workbooks.Open (FilePath)
//对XLS文件进行了改动以后,在关闭该文件时是否需要向用户提出警告。
MyOLE.Application.DisplayAlerts = False
if isnull(MyOLE) then return -1 ;
if ConnectErr <0 then
choose case ConnectErr
case -1
messagebox('错误提示','无效的调用')
case -2
messagebox('错误提示','类名没发现')
case -3
messagebox('错误提示','对象不能创建')
case -4
messagebox('错误提示','文件不能连接')
case -5
messagebox('错误提示','不能连接现在的对象')
case -6
messagebox('错误提示','文件无效')
case -7
messagebox("错误提示","文件不存在或已经打开")
case -8
messagebox("错误提示","服务器不能装载选择的文件")
case -9
messagebox("错误提示","其他错误")
end choose
return -1
end if
///////////////////////////////////////////////////////////////////////////////oleobject xlApp //声明Excel应用程序对象 xlApp = Create OLEObject OLEObject xlsub1,xlsub2,xlsub3,obook1,obook2,xlsub4,obook3 ret = xlApp.ConnectToNewObject( "Excel.Sheet" ) //打开自动化对象 应为 "excel.application" if ret <0 then MessageBox("Connect to Excel Failed !",string(ret)) return //操作第一个excel文件 string ls_file3="C:/mytest.xls" obook3=xlApp.Application.Workbooks.Open(ls_file3) //打开文件 xlsub4 = xlapp.Application.ActiveWorkbook.Worksheets[1] //激活第一页 //对单元格赋值 xlsub4.cells[c,3].Value = string(mydata.getitemdecimal(r,'rs')) xlsub4.cells[c,5].Value = string(mydata.getitemdecimal(r,'yp')) xlsub4.cells[c,6] = string(mydata.getitemdecimal(r,'jc')) xlsub4.cells[c,7] = string(mydata.getitemdecimal(r,'hy')) xlsub4.cells[c,8] = string(mydata.getitemdecimal(r,'zl')) //合并一个excel文件
string ls_ran ls_ran = "B6:M"+string(ld_1+1) obook1 = xlApp.Application.Workbooks.Open(ls_file2) xlsub1 = xlapp.Application.ActiveWorkbook.Worksheets[1] xlsub1.Range(ls_ran).Copy() //把所选内容复制到内存中
xlApp.Application.Visible = false xlsub2 = obook3.Worksheets.add() xlsub2.name = "分类汇总" xlsub2.Range(ls_ran).PasteSpecial() //把上一个文件内容粘过来 //加入分类汇总 obook3.Worksheets("分类汇总").Activate()
integer a[6] a={8,9,10,11,12,13} xlsub2.Range(ls_ran).Select() xlApp.Application.Selection.Subtotal(3,-4157,a, True, False, True) xlsub2.Rows(1).Insert() xlsub2.cells[1,1] = left(gs_shuqi,4)+"年"+right(gs_shuqi,2)+"月份" xlsub2.Cells[1,1].Font.Name="宋体" xlsub2.Cells[1,1].Font.Size=22 xlsub2.Range("A1:M1").Select() xlApp.Application.Selection.Merge() xlApp.Application.Selection.HorizOntalAlignment= 2
oBook1.save() oBook3.Save() oBook3.Saveas(is_docname) oBook1.close() oBook3.close() xlApp.Application.quit() xlApp.DisConnectObject()
|