<!--#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 %> |