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 & "\Templet\Table.xls")
set objExcelBook = objExcelApp.ActiveWorkBook
set objExcelSheets = objExcelBook.Worksheets
set objExcelSheet = objExcelBook.Sheets(1)
4、 另存Excel文件
objExcelBook.SaveAs strAddr & "\Temp\Table.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.Cells(3,1).Font.Color=vbred
4、 单元格四周画线条
objExcelSheet.Cells(3.1).Borders(1).LineStyle =1
objExcelSheet.Cells(3.1).Borders(2).LineStyle =1
objExcelSheet.Cells(3.1).Borders(3).LineStyle =1
objExcelSheet.Cells(3.1).Borders(4).LineStyle =1
区域中间画线条
objExcelSheet.Range(“A1:G7”).Borders(7).LineStyle =1
objExcelSheet.Range(“A1:G7”).Borders(8).LineStyle =1
8、 单元格设定背景色
objExcelSheet.Cells(3.1).Interior.colorindex=17
9、 合并单元格
objExcelSheet.Range(“A1:G7”).Merge
10、 设置左右对齐方式
2 left 3 middle 4 right
objExcelSheet.Range("A1").HorizontalAlignment = 2
11、 设置上下对齐方式
2 为居中
objExcelSheet.Range("A1")..VerticalAlignment = 2
四、 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
Set ExcelConn = Server.CreateObject("ADODB.Connection")
Set AccessConn = Server.CreateObject("ADODB.Connection")
ExcelConn.Open "Driver={Microsoft Excel Driver (*.xls)};DBQ=" & Server.MapPath("Excel文件名例如:50学生信息.xls")
AccessConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;User ID=;Password=;Data Source=" & Server.MapPath("Access文件名例如:学生信息.mdb")
'假设你的数据库是:Access
Set rsExcel = Server.CreateObject("Adodb.RecordSet")
Set rsAccess = Server.CreateObject("Adodb.RecordSet")
SqlExcel = "SELECT * FROM [" & "Excel内sheet的50学生信息表" & "$]"
SqlAccess = "SELECT * FROM [" & "Access内学生信息表" & "]"
rsExcel.Open SqlExcel ,ExcelConn ,1,1
rsAccess.Open SqlAccess,AccessConn,1,1
While Not rsExcel.eof '如果不是最后一行就继续
rsAccess.AddNew
rsAccess("学号") = rsExcel("学号")
rsAccess("姓名") = rsExcel("姓名")
rsAccess("性别") = rsExcel("性别")
rsAccess.Update
rsExcel.movenext
Wend
rsExcel.Close
ExcelConn.Close
AccessConn.Close
Set ExcelConn = Nothing
Set AccessConn = Nothing