asp操作excel全集
(2012-04-17 02:55:20)
标签:
aspexcelit |
分类: 其他开发语言 |
一、ASP对Excel的基本操作
1、建立Excel对象
set objExcelApp = CreateObject("Excel.Application")
objExcelApp.DisplayAlerts = false 不显示警告
objExcelApp.Application.Visible = false 不显示界面
2、新建Excel文件
objExcelApp.WorkBooks.add
set objExcelBook = objExcelApp.ActiveWorkBook
set objExcelSheets = objExcelBook.Worksheets
set objExcelSheet = objExcelBook.Sheets(1)
3、读取已有Excel文件
strAddr = Server.MapPath(".")
objExcelApp.WorkBooks.Open(strAddr &
"TempletTable.xls")
set objExcelBook = objExcelApp.ActiveWorkBook
set objExcelSheets = objExcelBook.Worksheets
set objExcelSheet = objExcelBook.Sheets(1)
4、另存Excel文件
objExcelBook.SaveAs strAddr & "TempTable.xls"
5、保存Excel文件
objExcelBook.Save (笔者测试时保存成功,页面报错。)
6、退出Excel操作
objExcelApp.Quit 一定要退出
set objExcelApp = Nothing
二、ASP操作Excel生成数据表
1、在一个范围内插入数据
objExcelSheet.Range(\"B3:k3\").Value = Array("67", "87", "5", "9",
"7", "45", "45", "54", "54", "10")
2、在一个单元格内插入数据
objExcelSheet.Cells(3,1).Value="Internet Explorer"
3、设置指定列的宽度(单位:字符个数)
objExcelSheet.ActiveSheet.Columns(1).ColumnWidth=5
4、设置指定行的高度(单位:磅)
objExcelSheet.ActiveSheet.Rows(1).RowHeight=1/0.035
(设定行高为1厘米,1磅=0.035厘米)
5、指定边框线宽度(Borders参数如下)
objExcelSheet.ActiveSheet.Range(″b3:d3″).Borders(2).Weight=3
6、设置四个边框线条的类型
objExcelSheet.ActiveSheet.Range(″b3:d3″).Borders(2).LineStyle=1
(其中Borders参数:1-左、2-右、3-顶、4-底、5-斜、6-斜/;LineStyle值:1与7-细实、2-细虚、4-点虚、9-双细实线)
三、Excel页面操作及字体
1、设置页眉
objExcelSheet.ActiveSheet.PageSetup.CenterHeader=″报表1″
2、设置页脚
objExcelSheet.ActiveSheet.PageSetup.CenterFooter=″第&P页″
3、设置页眉到顶端边距为2厘米
objExcelSheet.ActiveSheet.PageSetup.HeaderMargin=2/0.035
4、设置页脚到底边距为3厘米
objExcelSheet.ActiveSheet.PageSetup.FooterMargin=3/0.035
5、设置顶边距为2厘米
objExcelSheet.ActiveSheet.PageSetup.TopMargin=2/0.035
6、设置底边距为4厘米
objExcelSheet.ActiveSheet.PageSetup.BottomMargin=4/0.035
7、设置左边距为2厘米
objExcelSheet.ActiveSheet.PageSetup.LeftMargin=2/0.035
8、设置右边距为2厘米
objExcelSheet.ActiveSheet.PageSetup.RightMargin=2/0.035
9、设置字体
objExcelSheet.ActiveSheet.Cells(2,1).Font.Name=″黑体″
10、设置字体大小
objExcelSheet.ActiveSheet.Cells(1,1).Font.Size=25
11、设置字体为斜体
objExcelSheet.ActiveSheet.Cells(1,1).Font.Italic=.t.
12、设置整列字体为粗体
objExcelSheet.ActiveSheet.Columns(1).Font.Bold=.t.
四、ASP操作Excel生成Chart图
1、创建Chart图
objExcelApp.Charts.Add
2、设定Chart图种类
objExcelApp.ActiveChart.ChartType = 97
注:二维折线图,4;二维饼图,5;二维柱形图,51
3、设定Chart图标题
objExcelApp.ActiveChart.HasTitle = True
objExcelApp.ActiveChart.ChartTitle.Text = "A test Chart"
4、通过表格数据设定图形
objExcelApp.ActiveChart.SetSourceData
objExcelSheet.Range("A1:k5"),1
5、直接设定图形数据(推荐)
objExcelApp.ActiveChart.SeriesCollection.NewSeries
objExcelApp.ActiveChart.SeriesCollection(1).Name = "=""333"""
objExcelApp.ActiveChart.SeriesCollection(1).Values =
"={1,4,5,6,2}"
6、绑定Chart图
objExcelApp.ActiveChart.Location 1
7、显示数据表
objExcelApp.ActiveChart.HasDataTable = True
8、显示图例
objExcelApp.ActiveChart.DataTable.ShowLegendKey = True
出错时Excel出现的死进程出现是一件很头疼的事情。在每个文件前加上“On Error Resume
Next”将有助于改善这种情况,因为它会不管文件是否产生错误都坚持执行到“Application.Quit”,保证每次程序执行完不留下死进程。