ASP教程之导出Excel数据的四种方法

开发 后端
本文介绍的是ASP导出Excel书籍的四种方法,希望对大家有帮助,一起来看。

我们有时候需要把数据导出来,作为参考等等。下面就为你介绍,ASP导出Excel书籍的四种方法。

一、使用OWC

什么是OWC?

OWC是office Web Compent的缩写,即Microsoft的office Web组件,它为在Web中绘制图形提供了灵活的同时也是最基本的机制。在一个intranet环境中,如果可以假设客户机上存在特定的浏览器和一些功能强大的软件(如IE5和office 2000),那么就有能力利用office Web组件提供一个交互式图形开发环境。这种模式下,客户端工作站将在整个任务中分担很大的比重。

以下为引用的内容:

 

  1. <%Option Explicit   
  2. Class ExcelGen   
  3. Private obJSPreadsheet   
  4. Private iColOffset  
  5.  
  6. Private iRowOffset   
  7. Sub Class_Initialize()   
  8. Set obJSPreadsheet = Server.CreateObject("OWC.Spreadsheet")   
  9. iRowOffset = 2   
  10. iColOffset = 2   
  11. End Sub  
  12.  
  13. Sub Class_Terminate()   
  14. Set obJSPreadsheet = Nothing 'Clean up   
  15. End Sub  
  16.  
  17. Public Property Let ColumnOffset(iColOff)   
  18. If iColOff > 0 then   
  19. iColOffiColOffset = iColOff   
  20. Else   
  21. iColOffset = 2   
  22. End If   
  23. End Property  
  24.  
  25. Public Property Let RowOffset(iRowOff)   
  26. If iRowOff > 0 then   
  27. iRowOffiRowOffset = iRowOff   
  28. Else   
  29. iRowOffset = 2   
  30. End If   
  31. End Property Sub GenerateWorksheet(objRS)   
  32. 'Populates the Excel worksheet based on a Recordset's contents   
  33. 'Start by displaying the titles   
  34. If objRS.EOF then Exit Sub   
  35. Dim objField, iCol, iRow   
  36. iCol = iColOffset   
  37. iRow = iRowOffset   
  38. For Each objField in objRS.Fields   
  39. obJSPreadsheet.Cells(iRow, iCol).Value = objField.Name   
  40. obJSPreadsheet.Columns(iCol).AutoFitColumns   
  41. '设置Excel表里的字体   
  42. obJSPreadsheet.Cells(iRow, iCol).Font.Bold = True   
  43. obJSPreadsheet.Cells(iRow, iCol).Font.Italic = False   
  44. obJSPreadsheet.Cells(iRow, iCol).Font.Size = 10   
  45. obJSPreadsheet.Cells(iRow, iCol).Halignment = 2 '居中   
  46. iColiCol = iCol + 1   
  47. Next 'objField   
  48. 'Display all of the data   
  49. Do While Not objRS.EOF   
  50. iRowiRow = iRow + 1   
  51. iCol = iColOffset   
  52. For Each objField in objRS.Fields   
  53. If IsNull(objField.Value) then   
  54. obJSPreadsheet.Cells(iRow, iCol).Value = ""   
  55. Else   
  56. obJSPreadsheet.Cells(iRow, iCol).Value = objField.Value   
  57. obJSPreadsheet.Columns(iCol).AutoFitColumns   
  58. obJSPreadsheet.Cells(iRow, iCol).Font.Bold = False   
  59. obJSPreadsheet.Cells(iRow, iCol).Font.Italic = False   
  60. obJSPreadsheet.Cells(iRow, iCol).Font.Size = 10   
  61. End If   
  62. iColiCol = iCol + 1   
  63. Next 'objField   
  64. objRS.MoveNext   
  65. Loop   
  66. End Sub Function SaveWorksheet(strFileName)  
  67.  
  68. 'Save the worksheet to a specified filename   
  69. On Error Resume Next   
  70. Call obJSPreadsheet.ActiveSheet.Export(strFileName, 0)   
  71. SaveWorksheet = (Err.Number = 0)   
  72. End Function   
  73. End Class  
  74.  
  75. Dim objRS   
  76. Set objRS = Server.CreateObject("ADODB.Recordset")   
  77. objRS.Open "SELECT * FROM xxxx", "Provider=SQLOLEDB.1;Persist Security  
  78.  
  79. Info=True;User ID=xxxx;Password=xxxx;Initial Catalog=xxxx;Data source=xxxx;"   
  80. Dim SaveName   
  81. SaveName = Request.Cookies("savename")("name")   
  82. Dim objExcel   
  83. Dim ExcelPath   
  84. ExcelPath = "Excel\" & SaveName & ".xls"   
  85. Set objExcel = New ExcelGen   
  86. objExcel.RowOffset = 1   
  87. objExcel.ColumnOffset = 1   
  88. objExcel.GenerateWorksheet(objRS)   
  89. If objExcel.SaveWorksheet(Server.MapPath(ExcelPath)) then   
  90. 'Response.Write "<HTML><body bgcolor='gainsboro' text='#000000'>已保存为Excel文件.  
  91. <a href=../../'" & server.URLEncode(ExcelPath) & "'>下载</a>"   
  92. Else   
  93. Response.Write "在保存过程中有错误!"   
  94. End If   
  95. Set objExcel = Nothing   
  96. objRS.Close   
  97. Set objRS = Nothing   
  98. %>  

二、用Excel的Application组件在客户端导出到Excel或word

以下为引用的内容:

注意:两个函数中的“data“是网页中要导出的table的 id

 

  1. <input type="hidden" name="out_word" onclick="vbscript:buildDoc" value="导出到word" class="notPrint">   
  2. <input type="hidden" name="out_Excel" onclick="AutomateExcel();" value="导出到Excel" class="notPrint"> 

 

导出到Excel代码

 

  1. <SCRIPT LANGUAGE="javascript">   
  2. <!--   
  3. function AutomateExcel()   
  4. {   
  5. // Start Excel and get Application object.   
  6. var oXL = new ActiveXObject("Excel.Application");   
  7. // Get a new workbook.   
  8. var oWB = oXL.Workbooks.Add();   
  9. var oSheet = oWB.ActiveSheet;   
  10. var table = document.all.data;   
  11. var hang = table.rows.length;  
  12.  
  13. var lie = table.rows(0).cells.length;  
  14.  
  15. // Add table headers going cell by cell.   
  16. for (i=0;i<hang;i++)   
  17. {   
  18. for (j=0;j<lie;j++)   
  19. {   
  20. oSheet.Cells(i+1,j+1).value = table.rows(i).cells(j).innerText;   
  21. }  
  22.  
  23. }   
  24. oXL.Visible = true;   
  25. oXL.UserControl = true;   
  26. }   
  27. //-->   
  28. </SCRIPT>  
  29.  
  30.   导出到word代码  
  31.  
  32. <script language="vbscript">   
  33. Sub buildDoc   
  34. set table = document.all.data   
  35. row = table.rows.length   
  36. column = table.rows(1).cells.length  
  37.  
  38. Set objwordDoc = CreateObject("word.Document")  
  39.  
  40. objwordDoc.Application.Documents.Add theTemplate, False   
  41. objwordDoc.Application.Visible=True 
  42.  
  43. Dim theArray(20,10000)   
  44. for i=0 to row-1   
  45. for j=0 to column-1   
  46. theArray(j+1,i+1) = table.rows(i).cells(j).innerTEXT   
  47. next   
  48. next   
  49. objwordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("综合查询结果集") //显示表格标题  
  50.  
  51. objwordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("")   
  52. Set rngPara = objwordDoc.Application.ActiveDocument.Paragraphs(1).Range   
  53. With rngPara   
  54. .Bold = True //将标题设为粗体   
  55. .ParagraphFormat.Alignment = 1 //将标题居中   
  56. .Font.Name = "隶书" //设定标题字体   
  57. .Font.Size = 18 //设定标题字体大小   
  58. End With   
  59. Set rngCurrent = objwordDoc.Application.ActiveDocument.Paragraphs(3).Range   
  60. Set tabCurrent = ObjwordDoc.Application.ActiveDocument.Tables.Add(rngCurrent,row,column)  
  61.  
  62. for i = 1 to column  
  63.  
  64. objwordDoc.Application.ActiveDocument.Tables(1).Rows(1).Cells(i).Range.InsertAfter theArray(i,1)   
  65. objwordDoc.Application.ActiveDocument.Tables(1).Rows(1).Cells(i).Range.ParagraphFormat.alignment=1   
  66. next   
  67. For i =1 to column   
  68. For j = 2 to row   
  69. objwordDoc.Application.ActiveDocument.Tables(1).Rows(j).Cells(i).Range.InsertAfter theArray(i,j)   
  70. objwordDoc.Application.ActiveDocument.Tables(1).Rows(j).Cells(i).Range.ParagraphFormat.alignment=1   
  71. Next   
  72. Next 
  73.  
  74. End Sub   
  75. </SCRIPT>  

三、直接在IE中打开,再存为Excel文件

以下为引用的内容:

把读出的数据用<table>格式,在网页中显示出来,同时,加上下一句即可把Excel表在客客户端显示。

<%response.ContentType ="application/vnd.ms-Excel"%>

注意:显示的页面中,只把<table>输出,***不要输出其他表格以外的信息。

四、导出以半角逗号隔开的csv

用fso方法生成文本文件的方法,生成一个扩展名为csv文件。此文件,一行即为数据表的一行。生成数据表字段用半角逗号隔开。(有关fso生成文本文件的方法,在此就不做介绍了)

CSV文件介绍 (逗号分隔文件)

选择该项系统将创建一个可供下载的CSV 文件; CSV是最通用的一种文件格式,它可以非常容易地被导入各种PC表格及数据库中。

请注意即使选择表格作为输出格式,仍然可以将结果下载CSV文件。在表格输出屏幕的底部,显示有 "CSV 文件"选项,点击它即可下载该文件。

希望本文介绍的三种方法,能够帮助到你。

【编辑推荐】

  1. ASP.NET如何进行性能优化问题
  2. 详细介绍ASP.NET的实用技巧
  3. ASP基础之Global.asa文件技巧用法
  4. 利用ASP和SQL Server构建网页防火墙
  5. ASP新手之常用错误处理解析
责任编辑:于铁 来源: 互联网
相关推荐

2021-03-10 10:13:39

爬虫Python代码

2018-02-01 08:25:10

DB2存储方法

2010-11-04 13:58:58

DB2存储过程

2020-07-24 09:56:12

React开发数据

2014-03-17 09:22:43

Linux命令

2022-09-02 14:29:01

JavaScrip数组属性

2010-08-31 15:51:51

DB2清除数据

2023-03-17 10:51:26

2022-07-15 14:43:21

数据安全Linux

2011-06-22 15:21:08

XML

2009-03-31 13:12:30

解析XMLJava

2009-02-25 09:52:14

类型转换.NET 强制转型

2020-08-10 00:30:55

备份密码iPhone移动安全

2009-11-23 15:57:51

PHP伪静态

2018-03-05 22:25:21

数据中心降低成本停机

2014-02-28 10:50:24

Linux命令

2009-09-17 16:55:58

C#组件设计

2010-03-18 17:57:37

Java XMLSoc

2020-01-21 19:15:23

漏洞安全IT

2021-09-03 11:24:04

云计算云计算环境云应用
点赞
收藏

51CTO技术栈公众号