自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP教程之導(dǎo)出Excel數(shù)據(jù)的四種方法

開發(fā) 后端
本文介紹的是ASP導(dǎo)出Excel書籍的四種方法,希望對大家有幫助,一起來看。

我們有時(shí)候需要把數(shù)據(jù)導(dǎo)出來,作為參考等等。下面就為你介紹,ASP導(dǎo)出Excel書籍的四種方法。

一、使用OWC

什么是OWC?

OWC是office Web Compent的縮寫,即Microsoft的office Web組件,它為在Web中繪制圖形提供了靈活的同時(shí)也是最基本的機(jī)制。在一個(gè)intranet環(huán)境中,如果可以假設(shè)客戶機(jī)上存在特定的瀏覽器和一些功能強(qiáng)大的軟件(如IE5和office 2000),那么就有能力利用office Web組件提供一個(gè)交互式圖形開發(fā)環(huán)境。這種模式下,客戶端工作站將在整個(gè)任務(wù)中分擔(dān)很大的比重。

以下為引用的內(nèi)容:

 

  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. '設(shè)置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 "在保存過程中有錯(cuò)誤!"   
  94. End If   
  95. Set objExcel = Nothing   
  96. objRS.Close   
  97. Set objRS = Nothing   
  98. %>  

二、用Excel的Application組件在客戶端導(dǎo)出到Excel或word

以下為引用的內(nèi)容:

注意:兩個(gè)函數(shù)中的“data“是網(wǎng)頁中要導(dǎo)出的table的 id

 

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

 

導(dǎo)出到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.   導(dǎo)出到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("綜合查詢結(jié)果集") //顯示表格標(biāo)題  
  50.  
  51. objwordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("")   
  52. Set rngPara = objwordDoc.Application.ActiveDocument.Paragraphs(1).Range   
  53. With rngPara   
  54. .Bold = True //將標(biāo)題設(shè)為粗體   
  55. .ParagraphFormat.Alignment = 1 //將標(biāo)題居中   
  56. .Font.Name = "隸書" //設(shè)定標(biāo)題字體   
  57. .Font.Size = 18 //設(shè)定標(biāo)題字體大小   
  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文件

以下為引用的內(nèi)容:

把讀出的數(shù)據(jù)用<table>格式,在網(wǎng)頁中顯示出來,同時(shí),加上下一句即可把Excel表在客客戶端顯示。

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

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

四、導(dǎo)出以半角逗號隔開的csv

用fso方法生成文本文件的方法,生成一個(gè)擴(kuò)展名為csv文件。此文件,一行即為數(shù)據(jù)表的一行。生成數(shù)據(jù)表字段用半角逗號隔開。(有關(guān)fso生成文本文件的方法,在此就不做介紹了)

CSV文件介紹 (逗號分隔文件)

選擇該項(xiàng)系統(tǒng)將創(chuàng)建一個(gè)可供下載的CSV 文件; CSV是最通用的一種文件格式,它可以非常容易地被導(dǎo)入各種PC表格及數(shù)據(jù)庫中。

請注意即使選擇表格作為輸出格式,仍然可以將結(jié)果下載CSV文件。在表格輸出屏幕的底部,顯示有 "CSV 文件"選項(xiàng),點(diǎn)擊它即可下載該文件。

希望本文介紹的三種方法,能夠幫助到你。

【編輯推薦】

  1. ASP.NET如何進(jìn)行性能優(yōu)化問題
  2. 詳細(xì)介紹ASP.NET的實(shí)用技巧
  3. ASP基礎(chǔ)之Global.asa文件技巧用法
  4. 利用ASP和SQL Server構(gòu)建網(wǎng)頁防火墻
  5. ASP新手之常用錯(cuò)誤處理解析
責(zé)任編輯:于鐵 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2021-03-10 10:13:39

爬蟲Python代碼

2010-11-04 13:58:58

DB2存儲過程

2018-02-01 08:25:10

DB2存儲方法

2020-07-24 09:56:12

React開發(fā)數(shù)據(jù)

2014-03-17 09:22:43

Linux命令

2022-09-02 14:29:01

JavaScrip數(shù)組屬性

2024-10-16 18:09:54

2010-08-31 15:51:51

DB2清除數(shù)據(jù)

2023-03-17 10:51:26

2009-11-23 15:57:51

PHP偽靜態(tài)

2022-07-15 14:43:21

數(shù)據(jù)安全Linux

2011-06-22 15:21:08

XML

2020-08-10 00:30:55

備份密碼iPhone移動(dòng)安全

2009-02-25 09:52:14

類型轉(zhuǎn)換.NET 強(qiáng)制轉(zhuǎn)型

2009-03-31 13:12:30

解析XMLJava

2018-03-05 22:25:21

數(shù)據(jù)中心降低成本停機(jī)

2022-11-04 13:35:29

IT遠(yuǎn)程工作混合工作

2022-12-07 10:28:22

2009-09-17 16:55:58

C#組件設(shè)計(jì)

2010-03-18 17:57:37

Java XMLSoc
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號