最近一直在帮部门做各种数据工作,要求也是五花八门,尤其是某些人要求导出excel还是格式,每次都现找现用,导致用过的也忘了。
所以今天整理了一下,把搜来的试来的常用到的,罗列一下,省得以后到处找,以js为例。
打印:ExcelSheet.PrintOut 或xlBook.PrintOut
1.页面设置
//页边距 左2厘米
ExcelSheet.ActiveSheet.PageSetup.LeftMargin= 2/0.035;
ExcelSheet.ActiveSheet.PageSetup.RightMargin = 3/0.035;
ExcelSheet.ActiveSheet.PageSetup.TopMargin = 4/0.035;
ExcelSheet.ActiveSheet.PageSetup.BottomMargin = 5/0.035;
//页边距页眉1厘米,页脚2厘米
ExcelSheet.ActiveSheet.PageSetup.HeaderMargin = 1/0.035;
ExcelSheet.ActiveSheet.PageSetup.FooterMargin = 2/0.035;
ExcelSheet.ActiveSheet.PageSetup.CenterHeader = "页眉中部内容";
ExcelSheet.ActiveSheet.PageSetup.LeftHeader = "页眉左部内容";
ExcelSheet.ActiveSheet.PageSetup.RightHeader = "页眉右部内容";
ExcelSheet.ActiveSheet.PageSetup.CenterFooter = "页脚中部内容";
ExcelSheet.ActiveSheet.PageSetup.LeftFooter = "页脚左部内容";
ExcelSheet.ActiveSheet.PageSetup.RightFooter = "页脚右部内容";
2对单元格操作,带*部分对于行,列,区域都有相应属性
//设置单元格内容
ExcelSheet.ActiveSheet.Cells(row,col).Value = "内容";
//设置单元格边框(=1 虚线;=2实线;=-4238 粗实线(一般粗);=4实线(很粗))
ExcelSheet.ActiveSheet.Cells(row,col).Borders.Weight = 1;
//设置单元格底色*(1-黑色,2-白色,3-红色,4-绿色,5-蓝色,6-×××,7-粉红色,8-天蓝色,9-酱土色,如下图)
ExcelSheet.ActiveSheet.Cells(row,col).Interior.ColorIndex = 1;
//设置单元格背景样式*(1-无,2-细网格,3-粗网格,4-斑点,5-横线,6-竖线..可以多做尝试)
ExcelSheet.ActiveSheet.Cells(row,col).Interior.Pattern = 1;
ExcelSheet.ActiveSheet.Cells(row,col).Font.ColorIndex = 1;
//设置字体颜色*(与上相同)
ExcelSheet.ActiveSheet.Cells(row,col).Font.Size = 10;
//设置为10号字*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Name = "黑体";
//设置为黑体*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Italic = true;
//设置为斜体*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Bold = true;
//设置为粗体*
ExcelSheet.ActiveSheet.Cells(row,col).ClearContents;
//清除内容和样式*
ExcelSheet.ActiveSheet.Cells(row,col).WrapText=true;
//设置为自动换行*
ExcelSheet.ActiveSheet.Cells(row,col).Font.UnderLine=true;
//设置下划线*
ExcelSheet.UsedRange.WrapText = False
ExcelSheet.Range("A1").Rows.AutoFit
ExcelSheet.ActiveSheet.Cells(row,col).HorizontalAlignment = 3;
//水平对齐方式枚举* (1-常规,2-靠左,3-居中,4-靠右,5-填充 6-两端对齐,7-跨列居中,8-分散对齐)
ExcelSheet.ActiveSheet.Cells(row,col).VerticalAlignment = 2;
//垂直对齐方式枚举*(1-靠上,2-居中,3-靠下,4-两端对齐,5-分散对齐)
//设置
行,列有相应操作:
ExcelSheet.ActiveSheet.Rows(row).
ExcelSheet.ActiveSheet.Columns(col).
ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).
//如Rows("1:5" )即1到5行
ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).
//如Columns("1:5" )即1到5列
区域有相应操作:
XLObj.Range(startcell+":"+endcell).Select;
//如Range("A2:H8" )即A列第2格至H列第8格的整个区域
XLObj.Selection.
合并单元格
XLObj.Range(startcell+":"+endcell).MergeCells = true;
//如Range("A2:H8" )即将A列第2格至H列第8格的整个区域合并为一个单元格
XLObj.Range("A2",XLObj.Cells(8,8)).MergeCells = true;
设置行高与列宽
ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).ColumnWidth = 22;
//设置从firstcol到stopcol列的宽度为22
ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).RowHeight = 22;
//设置从firstrow到stoprow行的宽度为22
3、其他特殊效果
文字竖排显示,效果如下:sht.Cells(3, 1).Orientation = -4166
文
字
竖
排
//设置某一列显示字符而不是数字
xlsheet.Columns(2).NumberFormatLocal="@";
1) 文本:vnd.ms-excel.numberformat:@
2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd
3) 数字:vnd.ms-excel.numberformat:#,##0.00
4) 货币:vnd.ms-excel.numberformat:¥#,##0.00
5) 百分比:vnd.ms-excel.numberformat: #0.00%
插入分页符(10行前):
xlsheet.Rows(10).PageBreak = 1
删除分页符:
xlsheet.Rows(10).PageBreak = 0
4 拷贝相关操作
1)拷贝整个工作表:
xlbook.ActiveSheet.Used.Range.Copy
2)拷贝指定区域:
xlbook.ActiveSheet.Range( "A1:E2" ).Copy
3)从A1位置开始粘贴:
xlbook.ActiveSheet.Range.( "A1" ).PasteSpecial
4)从文件尾部开始粘贴:
xlbook.ActiveSheet.Range.PasteSpecial
插入一行或一列:
xlBook.ActiveSheet.Rows(2).Insert
xlBook.ActiveSheet.Columns(1).Insert
删除一行或一列:
xlBook.ActiveSheet.Rows(2).Delete
xlBook.ActiveSheet.Columns(1).Delete
打印预览工作表:
xlBook.ActiveSheet.PrintPreview
打印输出工作表:
xlBook.ActiveSheet.PrintOut
工作表保存:
if not xlBook.ActiveWorkBook.Saved then
xlBook.ActiveSheet.PrintPreview
工作表另存为:
xlBook.SaveAs( "C:\temp\test1.xlsx" )
放弃存盘:
xlBook.ActiveWorkBook.Saved =true
使用VBS 控制Excle二维图
1)选择当第一个工作薄第一个工作表
set Sheet=oExcel.Workbooks(1).Worksheets(1)
2)增加一个二维图
achart=oSheet.chartobjects.add(100,100,200,200)
3)选择二维图的形态
achart.chart.charttype=4
4)给二维图赋值
set series=achart.chart.seriescollection
range="sheet1!r2c3:r3c9"
series.add range,true
5)加上二维图的标题
achart.Chart.HasTitle=True
achart.Chart.ChartTitle.Characters.Text=" Excle二维图"
6)改变二维图的标题字体大小
achart.Chart.ChartTitle.Font.size=18
7)给二维图加下标说明
achart.Chart.Axes(xlCategory, xlPrimary).HasTitle = True
achart.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "下标说明"
8)给二维图加左标说明
achart.Chart.Axes(xlValue, xlPrimary).HasTitle = True
achart.Chart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "左标说明"
9)给二维图加右标说明
achart.Chart.Axes(xlValue, xlSecondary).HasTitle = True
achart.Chart.Axes(xlValue, xlSecondary).AxisTitle.Characters.Text = "右标说明"
10)改变二维图的显示区大小
achart.Chart.PlotArea.Left = 5
achart.Chart.PlotArea.Width = 223
achart.Chart.PlotArea.Height = 108
转载于:https://blog.51cto.com/lamela/1022224