来源:www.cncfan.com | 2006-8-9 | (有3173人读过)
<!--#i nclude FILE="../conn.inc" --> <% Set rs= Server.CreateObject("ADODB.Recordset") sql="select wpbh,wplb,wpmc,ggxh,jldw from wpjbxx" rs.open sql,conn,1,1 'response.write sql 'response.end %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html> <head> <title>asp生成excel文件</title> </head> <body> <% CreateXlsFile() %> </body> </html> <script LANGUAGE=vbscript RUNAT=Server> Function GenFileName() dim fname fname = "File" systime=now() fname= fname & cstr(year(systime)) & cstr(month(systime)) & cstr(day(systime)) fname= fname & cstr(hour(systime)) & cstr(minute(systime)) & cstr(second(systime)) GenFileName = fname End Function
Function CreateXlsFile() Dim xlWorkSheet ' Excel Worksheet object Dim xlApplication //如提示CreateObject("Excel.application")没有权限 //则运行(dcomcnfg)或在管理工具中运行组件服务--计算机--DCOM配置--Micorsoft Excel应用程序 ///右键属性--把安全选项中的启动和激活权限附给everyone Set xlApplication = CreateObject("Excel.application") '调用excel对象 'xlApplication.Visible = false xlApplication.DisplayAlerts = false //不显示警告 xlApplication.Application.Visible = false //不显示界面 xlApplication.Workbooks.Add Set xlWorksheet = xlApplication.Worksheets(1) xlWorksheet.Cells(1,1).Value = "物品基本信息" iRow = 2 xlWorksheet.Cells(2,1).Value = "物品编码" xlWorksheet.Cells(2,2).Value = "物品类别" xlWorksheet.Cells(2,3).Value = "物品名称" xlWorksheet.Cells(2,4).Value = "规格型号" xlWorksheet.Cells(2,5).Value = "计量单位" iRow = 3 If Not rs.EOF Then Do Until rs.EOF For i = 0 To rs.fields.Count-1 xlWorksheet.Cells(iRow,i + 1).Value = rs.fields(i) //xlWorksheet.Cells(iRow,i + 1).HorizontalAlignment = xlCenter //xlWorksheet.Cells(iRow,i + 1).VerticalAlignment = xlCenter Next iRow = iRow + 1 rs.MoveNext Loop End If strFile = GenFileName() xlWorksheet.SaveAs Server.MapPath(".") & "\" & strFile & ".xls" xlApplication.Quit ' Close the Workbook Set xlWorksheet = Nothing Set xlApplication = Nothing Response.Write("Click <A HRef=" & strFile & ".xls>Here</A> to get XLS file") End Function </script> <script language="javascript"> function exportExcel(atblData) { if(typeof(EXPORT_OBJECT)!="object") { document.body.insertAdjacentHTML("afterBegin","<OBJECT style='display:none' classid=clsid:0002E510-0000-0000-C000-000000000046 id=EXPORT_OBJECT></Object>"); } with (EXPORT_OBJECT) { DataType = "HTMLData"; HTMLData =atblData.outerHTML; try{ ActiveSheet.Export("C:\\owcToExcel.xls", 0); alert('汇出完毕'); } catch (e){ alert('汇出Excel表失败,请确定已安装Excel2000(或更高版本),并且没打开同名xls文件'); } } } </script>
|