asp對execl操作

寫excel

<%
response.write Server.MapPath("fso/")
Set ExcelApp =CreateObject("Excel.Application")
ExcelApp.Application.Visible = True
Set ExcelBook = ExcelApp.Workbooks.add
ExcelApp.Range(ExcelApp.Cells(1,   1),   ExcelApp.Cells(1,   4)).Merge'合併單元格

 

ExcelBook.WorkSheets(1).cells(1,1).value ="用戶表" 
ExcelBook.WorkSheets(1).cells(2,1).value = "用戶編號"
ExcelBook.WorkSheets(1).cells(2,2).value = "登陸名"
ExcelBook.WorkSheets(1).cells(2,3).value = "真實姓名"
ExcelBook.WorkSheets(1).cells(2,4).value = "密碼"
cnt =3
For i=3 To 6
ExcelBook.WorkSheets(1).cells(cnt,1).value = "1111"
ExcelBook.WorkSheets(1).cells(cnt,2).value = "22222"
ExcelBook.WorkSheets(1).cells(cnt,3).value = "33333"
ExcelBook.WorkSheets(1).cells(cnt,4).value = "ds"

cnt = cint(cnt) + 1
Next
ExcelApp.Range("B3:k3").Value=Array("67","87","5","9","7","45","45","54","54","10")'區段添加數據
Excelbook.SaveAs Server.MapPath("fso/1.xls")   '這個是數據導出完畢以後在D盤存成文件
ExcelApp.Application.Quit    '導出以後退出Excel
Set ExcelApp = Nothing    '註銷Excel對象
%>

讀取execle

<%
  dim     myconnection
  set     myconnection=server.createobject("adodb.connection")
  dim     myconnectionstr
  myconnectionstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("fso/2.xls")&";Extended Properties=Excel 8.0;"    
  myconnection.open     myconnectionstr  
  dim     myrecordset          
  set     myrecordset=server.createobject("adodb.recordset")  
  dim     mysql                
  mysql="select     *     from     [sheet1$]"
  myrecordset.open     mysql,myconnection,1,1 
  do     while     not     myrecordset.eof      
  response.write     myrecordset(0) &"  "& myrecordset(1)&"  "&myrecordset(1)    &"  "&myrecordset(3)&"  "&myrecordset(4)      &  "<br>"
  myrecordset.movenext          
  loop          
  set     myrecordset=nothing          
  set     myconnection=nothing          
      '默認excel的第一行不讀取。作爲標題頁
  %>  

發佈了11 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章