VFP控制EXCEL的方法终于找到了.docx
《VFP控制EXCEL的方法终于找到了.docx》由会员分享,可在线阅读,更多相关《VFP控制EXCEL的方法终于找到了.docx(23页珍藏版)》请在冰豆网上搜索。
VFP控制EXCEL的方法终于找到了
VFP控制EXCEL的常用方法
oExl=CREATEOBJECT('Excel.application')
oExl.Visible=.T.
oExl.DefaultSaveFormat=39
oExl.SheetsInNewWorkbook=1
oExl.Workbooks.Open(cXLS)
oExl.WindowState=-4140&&窗口最小化
oExl.WindowState=-4143&&窗口正常化
oExl.WindowState=-4147&&窗口最大化
*------------------------------------------------------*
&&页面设置
oExl.ActiveSheet.PageSetup.PrintTitleRows="$1:
$3"&&打印标题
oExl.ActiveSheet.PageSetup.Orientation=2&&页面方向:
1纵向/2横向
*oExl.ActiveSheet.PageSetup.PaperSize=11&&纸张尺寸:
9-A4/11-A5
oExl.ActiveSheet.PageSetup.TopMargin=0.8/0.035&&顶边距
oExl.ActiveSheet.PageSetup.BottomMargin=0.8/0.035&&底边距
oExl.ActiveSheet.PageSetup.LeftMargin=0&&左边距
oExl.ActiveSheet.PageSetup.RightMargin=0&&右边距
oExl.ActiveSheet.PageSetup.CenterHorizontally=.T.&&页面居中
oExl.ActiveSheet.PageSetup.HeaderMargin=0.5/0.035
oExl.ActiveSheet.PageSetup.FooterMargin=0.5/0.035
oExl.ActiveSheet.PageSetup.LeftHeader="&9表格"”9是字号”
oExl.ActiveSheet.PageSetup.CenterHeader=""
oExl.ActiveSheet.PageSetup.RightHeader="&9"
oExl.ActiveSheet.PageSetup.LeftFooter=""
oExl.ActiveSheet.PageSetup.CenterFooter="&9第&P页,共&N页"
oExl.ActiveSheet.PageSetup.RightFooter="&9制表人:
'+代码+'制表时间:
"+TTOC(DATETIME())
*------------------------------------------------------*
&&整体格式设置
oExl.ActiveSheet.Rows.Font.Size=9
oExl.ActiveSheet.Rows.Font.Name='宋体'
oExl.ActiveSheet.Rows.RowHeight=0.5/0.035
oExl.ActiveSheet.Rows.NumberFormatLocal="0.00_;[红色]-0.00"
oExl.ActiveSheet.Rows.VerticalAlignment=2&&单元格内容垂直居中
*------------------------------------------------------*
&&获取最大行号和最大列号
LocalnMaxRow,nMaxCol
nMaxRow=oExl.ActiveSheet.UsedRange.Rows.Count
nMaxCol=oExl.ActiveSheet.UsedRange.Columns.Count
&&表头格式设置
oExl.ActiveSheet.Rows
(1).Font.Size=16
oExl.ActiveSheet.Rows
(1).Font.Bold=.T.
oExl.ActiveSheet.Rows
(1).RowHeight=1/0.035
oExl.ActiveSheet.Rows
(1).HorizontalAlignment=3
oExl.ActiveSheet.Rows
(2).HorizontalAlignment=3
oExl.ActiveSheet.Rows(3).HorizontalAlignment=3
oExl.ActiveSheet.Rows(3).Font.Bold=.T.
oExl.ActiveSheet.Range('A1:
'+CHR(96+nMaxCol)+'1').Merge
oExl.ActiveSheet.Range('A2:
'+CHR(96+nMaxCol)+'2').Merge
*------------------------------------------------------*
&&条件格式隐藏0值
oExl.ActiveSheet.UsedRange.FormatConditions.Delete
oExl.ActiveSheet.UsedRange.FormatConditions.Add(1,3,'0')
oExl.ActiveSheet.UsedRange.FormatConditions
(1).Font.ColorIndex=2&&文字白色
*------------------------------------------------------*
oExl.ActiveSheet.Range('A1:
B1').Interior.ColorIndex=6&&单元格底纹黄色
*------------------------------------------------------*
oExl.ActiveSheet.Columns.AutoFit&&自动列宽
oExl.ActiveSheet.Range("A3:
"+CHR(96+nMaxCol)+ALLTRIM(STR(nMaxRow))).Borders.Weight=2&&边框线
*------------------------------------------------------*
&&自动筛选
IF!
oExl.ActiveSheet.AutoFilterMode&&判断是否存在自动筛选
oExl.ActiveSheet.Rows(3).Autofilter&&如果不存在自动筛选,则添加自动筛选
ENDIF
*------------------------------------------------------*
&&冻结窗格
oExl.ActiveSheet.Range('D4').Select
oExl.ActiveWindow.FreezePanes=.T.
*------------------------------------------------------*
*------------------------------------------------------*
&&分类汇总
LocalArry
(1),nMaxRow,nMaxCol
Arry
(1)=5
nMaxRow=oExl.ActiveSheet.UsedRange.Rows.Count
nMaxCol=oExl.ActiveSheet.UsedRange.Columns.Count
oExl.ActiveSheet.Range('A3:
'+chr(96+nMaxCol)+alltrim(str(nMaxRow))).Subtotal(2,-4157,@Arry,.T.,.F.,.T.)&&按第2列分类汇总数组Arry保存的列
*------------------------------------------------------*
1、对象的创建与关闭
*******************************
oExl=CREATEOBJECT('Excel.application') &&创建Excel对象
oExl.SheetsInNewWorkbook=1&&新建工作簿默认包含工作表个数
oExl.Workbooks.Add &&新建工作簿,工作表个数由SheetsInNewWorkBooks属性指定
oExl.Workbooks.Open(cXLS,3,.T.) &&打开指定工作簿(更新链接/只读打开)
oExl.Workbooks.Open(cXLS,[UpdateLinks],[ReadOnly],[Format],[Password],[WriteResPassword],[IgnoreReadOnlyRecommended],[Origin],[Delimiter],[Editable],[Notify],[Converter],[AddToMru],[Local],[CorruptLoad])
oExl.Worksheets(cSheet).Activate &&激活工作表Sheet3
oExl.Worksheets(3).Activate&&激活(从左到右)第3个工作表
oExl.WorkSheets.Count&&工作簿中工作表数
oExl.DefaultSaveFormat=39&&默认格式Excel5.0
oExl.DisplayAlerts=.F.&&不显示警告信息
oExl.Visible=.T. &&显示Excel窗口
oExl.Visible=.F. &&不显示Excel窗口
oExl.Caption="Excel标题栏" &&更改Excel标题栏
oExl.WorkSheet("Sheet2").Range("A1").PasteSpecial &&粘贴
oExl.Quit &&退出Excel
oExl.DisplayRecentFiles=.T.&&是否显示最近打开文档
oExl.RecentFiles.Maximum=4&&历史最大纪录数
oExl.UserName="XXXX"&&用户名
oExl.StandardFont="宋体"&&标准字体
oExl.StandardFontSize="12"&&标准字体大小
oExl.DefaultFilePath="D:
\XXXXXX\"&&默认工作目录
oExl.EnableSound=False&&声音反馈
oExl.RollZoom=False&&智能鼠标缩放
oExl.TransitionMenuKey="/"&&MicrosoftOfficeExcel菜单键
oExl.ActiveWorkbook.Password="123"
oExl.ActiveWorkbook.WritePassword="456"
oExl.ActiveWorkbook.ReadOnlyRecommended=False
oExl.ActiveWorkbook.SetPasswordEncryptionOptionsPasswordEncryptionProvider:
="",_
PasswordEncryptionAlgorithm:
="OfficeStandard",PasswordEncryptionKeyLength:
=40_
PasswordEncryptionFileProperties:
=False
IFoExl.ActiveWorkbook.FileFormat==39&&格式为Excel5.0工作簿
ENDIF
oExl.ActiveWorkBook.SaveAs(FileName,FileFormat,PassWord,WriteResPassWord,ReadOnlyRecommended,CreateBackup)
&&另存为
&&参数说明
FileName字符型,指定文件名
FileFormat数值型,文件格式
-4143MicrosoftOfficeExcel
11DBF4
39MicrosoftExcel5.0/95
43MicrosoftExcel97-Excel2003&5.0/95
44网页Html文件
-4158文本文件(制表符分隔)
PassWord字符型,只读密码
WriteResPassWord字符型,写密码
ReadOnlyRecommended逻辑型,建议只读
CreateBackup逻辑型,自动备份
例如:
oExl.ActiveWorkBook.SaveAs("d:
\1.xls",39)
oExl.ActiveWorkbook.saved=.T. &&放弃存盘
oExl.ActiveWorkbook.Save &&存盘(自动存盘不提问)
oExl.Workbooks.close &&关闭工作簿
3、单元格的设置
***********************************
&&冻结窗格
oExl.ActiveSheet.Range('D4').Select
oExl.ActiveWindow.FreezePanes=.T.
&&条件格式设定
oExl.ActiveSheet.Range('A1:
H18').FormatConditions.Add(Type,Operator,Formula1,Formula2)
Type类型(1单元格数值,2公式)
Operator操作符(1介于,2不介于,3等于,4不等于,5大于,6小于,7大于等于,8小于等于)
Formula1表达式1
Formula2表达式2,操作符为1介于或2不介于才有效
&&条件格式例句:
数值为0者,文字颜色白色
oExl.ActiveSheet.UsedRange.FormatConditions.Delete
oExl.ActiveSheet.UsedRange.FormatConditions.Add(1,3,'0')
oExl.ActiveSheet.UsedRange.FormatConditions
(1).Font.ColorIndex=2&&文字白色
&&编辑
oExl.ActiveSheet.UsedRange.Copy &&拷贝整个工作表
oExl.ActiveSheet.Range("A1:
E2").Copy &&拷贝指定区域
oExl.ActiveSheet.Columns("D:
D").Delete &&删除列
oExl.ActiveSheet.Columns("A:
B").Delete &&删除列
oExl.ActiveSheet.Rows
(1).Insert &&在第1行之前插入一行
oExl.ActiveSheet.Columns
(2).Insert &&在第2列之前插入一列
oExl.ActiveSheet.Cells(1,2).Value="ABTMC" &&给单元格赋值
oExl.ActiveSheet.Cells(2,2).Value="&BBBB" &&给单元格赋值
oExl.ActiveSheet.Cells(1,4).ClearContents &&清除单元格公式
oExl.ActiveSheet.Rows(18).PageBreak=1 &&在第18行之前插入分页符
oExl.ActiveSheet.Columns(4).PageBreak=0 &&在第4列之前删除分页符
oExl.ActiveSheet.Rows(3).AutoFilter&&自动筛选
oExl.ActiveSheet.Cells(1,1).Delete(-4159)&&删除单元格
-4159右边单元格左移
-4162下边单元格上移
oExl.Selection.EntireRow.Delete&&所在行整行删除
oExl.Selection.EntireColumn.Delete&&所在列整列删除
oExl.ActiveSheet.Cells(1,1).Insert(-4142)&&插入单元格
-4142活动单元格下移
-4161活动单元格右移
oExl.ActiveSheet.Selection.EntireRow.Insert&&插入行
oExl.ActiveSheet.Selection.EntireColumn.Insert&&插入列
&&选择性粘贴
oExl.ActiveSheet.Selection.PasteSpecialPaste:
=xlPasteValues,Operation:
=xlNone,SkipBlanks:
=False,Transpose:
=False
Paste粘贴数据:
8列宽,11公式和数值格式,12数值与数值格式,-4104全部,-4123公式,-4163数值,-4122格式,
Operation运算符:
-4142无,2加,3减,4乘,5除
SkipBlanks跳过空白单元:
逻辑型
Transpose转置:
逻辑型
--------------------------------------------------
&&排序
成功例句:
oExl.ActiveSheet.Range('A1:
'+CHR(96+nMaxCol)+ALLTRIM(STR(nMaxRow))).Sort(oExl.ActiveSheet.Range(CHR(96+ASCAN(Arry,'原币金额'))+'2'),2,,,,,,1)
&&按"原币金额"所在列降序排列,有标题
oExl.ActiveSheet.Rows('1:
28').Sort
Key1:
=Range("A2"),Order1:
=xlDescending,
Key2:
=Range("B2"),Order2:
=xlAscending,
Key3:
=Range("C2"),Order3:
=xlDescending,
Header:
=xlYes,
OrderCustom:
=6,
MatchCase:
=True,
Orientation:
=xlTopToBottom,
SortMethod:
=xlStroke,
DataOption1:
=xlSortNormal,
DataOption2:
=xlSortNormal,
DataOption3:
=xlSortNormal
参数说明:
2xlDescending降序
1xlAscenging升序
标题行:
0xlGuess
1xlYes,
2xlNo
1xlTopToBottom
排序方法:
1xlPinYin,
2xlStroke
0xlSortNormal
Key1排序字段:
取值类型是范围oExl.ActiveSheet.Range('B2')
Order1升序降序:
xlAscending=1升序,xlDescending=2降序
Header有无标题:
xlGuess=0自动判断,xlYes=1有,xlNo=2无
orderCustom排序顺序:
1普通,6日一二三
MatchCase是否区分大小写:
T区分,F不区分
Orientation1从头到底,2从左到右
SortMethod排序方法:
xlPinYin=1拼音,xlStroke=2字母
DataOption1xlSortNormal=0正常
DataOption2
DateOption3
&&分类汇总
LOCALArray
(1)
Array
(1)=5
oExl.ActiveSheet.Range('A1:
G28').Subtotal(2,-4157,@Array,.T.,.F.,.T.)
参数列表:
GroupBy:
=2,Function:
=xlSum,TotalList:
=Array(3),Replace:
=True,PageBreaks:
=False,SummaryBelowData:
=True
参数说明:
GroupBy分组字段序号
Function汇总方式:
-4157求和,-4112计数
TotalList汇总数据项:
保存汇总列序号的数组引用
Replace替换当前分类汇总:
逻辑型,默认T
PageBreaks每组数据分页:
逻辑型,默认F
SummaryBelowData汇总结果显示在数据下方:
逻辑型,默认T
&&汇总结果表间切换
oExl.ActiveSheet.Outline.ShowLevelsRowLevels:
=2
oExl.ActiveSheet.Outline.ShowLevelsRowLevels:
=1
oExl.ActiveSheet.Outline.ShowLevelsRowLevels:
=3
&&数值格式设置
oExl.ActiveSheet.Range("A").NumberFormatLocal="0.00" &&指定区域内的数字显示格式
&&对齐设置
oExl.ActiveSheet.Range('A1:
B1').HorizontalAlignment=3 &&水平方向 2左对齐,3居中,4右对齐
oExl.ActiveSheet.UsedRange.VerticalAlignment=2 &&垂直方向 1靠上,2居中,3靠下
oExl.ActiveSheet.Range('A1:
B1').WrapText=.T.&&自动换行
oExl.ActiveSheet.Range('A1:
B1').Orientation=-4166&&文字方向
-4166垂直向下
oExl.ActiveSheet.Range('A1:
B1').AddIndent=False&&缩进
oExl.ActiveSheet.Range('A1:
B1').IndentLevel=2&&缩进2字符
oExl.ActiveSheet.Range('A1:
B1').ShrinkToFit=.T.&&缩小以填充
oExl.ActiveSheet