ASP匯出EXCEL檔案簡單實現代碼

來源:互聯網
上載者:User
 代碼如下 複製代碼

 


<!--#include file="conn.asp"-->

<%
Set xlApplication = Server.CreateObject("Excel.Application") '調用excel對象
xlApplication.Visible = False '無需開啟excel
xlApplication.SheetsInNewWorkbook=1 '指定excel中表的數量
xlApplication.Workbooks.Add '添加活頁簿
Set xlWorksheet = xlApplication.Worksheets(1) '產生第1個工作表的子物件
xlWorksheet.name="統計" '指定工作表名稱
'指定列的寬度以及對齊 1靠左對齊 2靠右對齊 3置中
xlApplication.ActiveSheet.Columns(1).ColumnWidth=5 
xlApplication.ActiveSheet.Columns(1).HorizontalAlignment=3    
xlApplication.ActiveSheet.Columns(2).ColumnWidth=10
xlApplication.ActiveSheet.Columns(2).HorizontalAlignment=3
xlApplication.ActiveSheet.Columns(3).ColumnWidth=20
xlApplication.ActiveSheet.Columns(3).HorizontalAlignment=3

'xlApplication.ActiveSheet.Rows(i).RowHeight = 30'行的高度
'指定列的高度以及特定列
xlWorksheet.Range(xlWorksheet.Cells(1,1), xlWorksheet.Cells(1,3)).MergeCells =True '合并列
xlWorksheet.Range("A1").value="2005年統計"
xlWorksheet.Range("A1").font.Size=14'字型大小
xlWorksheet.Range("A1").font.bold=true'粗體
xlWorksheet.Range("A1").HorizontalAlignment=3'水平對齊
xlWorksheet.Range("A1").VerticalAlignment=3'垂直對齊

xlWorksheet.Cells(2,1).Value = "編號"
xlWorksheet.Cells(2,2).Value = "姓名"
xlWorksheet.Cells(2,3).Value = "單位"


'xlWorksheet.Range("A1:C1").Borders.LineStyle=1  '設定行style

'--------------------------------------------------自己可做迴圈i=i+1(資料庫資料)

 

i=1
strSql = "select * from excel"
Set rs =conn.execute(strSql)
if not rs.eof then
do while not rs.eof
xlWorksheet.Cells(2+i,1).Value = rs(0)
xlWorksheet.Cells(2+i,2).Value = rs(1)
xlWorksheet.Cells(2+i,3).Value = rs(2)
i=i+1
rs.movenext
loop
end if

 

'--------------------------------------------------

 

Set fs = CreateObject("Scripting.FileSystemObject")
tfile=Server.MapPath("test.xls")
if fs.FileExists(tfile) then
Set f = fs.GetFile(tfile)
f.delete true
Set f = nothing
end if
Set fs = nothing
xlWorksheet.SaveAs tfile '儲存檔案
xlApplication.Quit '釋放對象
Set xlWorksheet = Nothing
Set xlApplication = Nothing
%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.