怎么用ASP導(dǎo)出Excel數(shù)據(jù)

本篇內(nèi)容介紹了“怎么用ASP導(dǎo)出Excel數(shù)據(jù)”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)建站于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站設(shè)計、成都網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元臨河做網(wǎng)站,已為上家服務(wù),為臨河各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220

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

一、使用OWC

什么是OWC?

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

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

<%Option Explicit   Class ExcelGen   Private obJSPreadsheet   Private iColOffset   Private iRowOffset   Sub Class_Initialize()   Set obJSPreadsheet = Server.CreateObject("OWC.Spreadsheet")   iRowOffset = 2   iColOffset = 2   End Sub   Sub Class_Terminate()   Set obJSPreadsheet = Nothing 'Clean up   End Sub   Public Property Let ColumnOffset(iColOff)   If iColOff > 0 then   iColOffiColOffset = iColOff   Else   iColOffset = 2   End If   End Property   Public Property Let RowOffset(iRowOff)   If iRowOff > 0 then   iRowOffiRowOffset = iRowOff   Else   iRowOffset = 2   End If   End Property Sub GenerateWorksheet(objRS)   'Populates the Excel worksheet based on a Recordset's contents   'Start by displaying the titles   If objRS.EOF then Exit Sub   Dim objField, iCol, iRow   iCol = iColOffset   iRow = iRowOffset   For Each objField in objRS.Fields   obJSPreadsheet.Cells(iRow, iCol).Value = objField.Name   obJSPreadsheet.Columns(iCol).AutoFitColumns   '設(shè)置Excel表里的字體   obJSPreadsheet.Cells(iRow, iCol).Font.Bold = True   obJSPreadsheet.Cells(iRow, iCol).Font.Italic = False   obJSPreadsheet.Cells(iRow, iCol).Font.Size = 10   obJSPreadsheet.Cells(iRow, iCol).Halignment = 2 '居中   iColiCol = iCol + 1   Next 'objField   'Display all of the data   Do While Not objRS.EOF   iRowiRow = iRow + 1   iCol = iColOffset   For Each objField in objRS.Fields   If IsNull(objField.Value) then   obJSPreadsheet.Cells(iRow, iCol).Value = ""   Else   obJSPreadsheet.Cells(iRow, iCol).Value = objField.Value   obJSPreadsheet.Columns(iCol).AutoFitColumns   obJSPreadsheet.Cells(iRow, iCol).Font.Bold = False   obJSPreadsheet.Cells(iRow, iCol).Font.Italic = False   obJSPreadsheet.Cells(iRow, iCol).Font.Size = 10   End If   iColiCol = iCol + 1   Next 'objField   objRS.MoveNext   Loop   End Sub Function SaveWorksheet(strFileName)   'Save the worksheet to a specified filename   On Error Resume Next   Call obJSPreadsheet.ActiveSheet.Export(strFileName, 0)   SaveWorksheet = (Err.Number = 0)   End Function   End Class   Dim objRS   Set objRS = Server.CreateObject("ADODB.Recordset")   objRS.Open "SELECT * FROM xxxx", "Provider=SQLOLEDB.1;Persist Security   Info=True;User ID=xxxx;Password=xxxx;Initial Catalog=xxxx;Data source=xxxx;"   Dim SaveName   SaveName = Request.Cookies("savename")("name")   Dim objExcel   Dim ExcelPath   ExcelPath = "Excel\" & SaveName & ".xls"   Set objExcel = New ExcelGen   objExcel.RowOffset = 1   objExcel.ColumnOffset = 1   objExcel.GenerateWorksheet(objRS)   If objExcel.SaveWorksheet(Server.MapPath(ExcelPath)) then   'Response.Write "<HTML><body bgcolor='gainsboro' text='#000000'>已保存為Excel文件.  <a href=../../'" & server.URLEncode(ExcelPath) & "'>下載</a>"   Else   Response.Write "在保存過程中有錯誤!"   End If   Set objExcel = Nothing   objRS.Close   Set objRS = Nothing   %>

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

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

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

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

導(dǎo)出到Excel代碼

<SCRIPT LANGUAGE="javascript">   <!--   function AutomateExcel()   {   // Start Excel and get Application object.   var oXL = new ActiveXObject("Excel.Application");   // Get a new workbook.   var oWB = oXL.Workbooks.Add();   var oSheet = oWB.ActiveSheet;   var table = document.all.data;   var hang = table.rows.length;   var lie = table.rows(0).cells.length;   // Add table headers going cell by cell.   for (i=0;i<hang;i++)   {   for (j=0;j<lie;j++)   {   oSheet.Cells(i+1,j+1).value = table.rows(i).cells(j).innerText;   }   }   oXL.Visible = true;   oXL.UserControl = true;   }   //-->   </SCRIPT>   導(dǎo)出到word代碼   <script language="vbscript">   Sub buildDoc   set table = document.all.data   row = table.rows.length   column = table.rows(1).cells.length   Set objwordDoc = CreateObject("word.Document")   objwordDoc.Application.Documents.Add theTemplate, False   objwordDoc.Application.Visible=True  Dim theArray(20,10000)   for i=0 to row-1   for j=0 to column-1   theArray(j+1,i+1) = table.rows(i).cells(j).innerTEXT   next   next   objwordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("綜合查詢結(jié)果集") //顯示表格標題   objwordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("")   Set rngPara = objwordDoc.Application.ActiveDocument.Paragraphs(1).Range   With rngPara   .Bold = True //將標題設(shè)為粗體   .ParagraphFormat.Alignment = 1 //將標題居中   .Font.Name = "隸書" //設(shè)定標題字體   .Font.Size = 18 //設(shè)定標題字體大小   End With   Set rngCurrent = objwordDoc.Application.ActiveDocument.Paragraphs(3).Range   Set tabCurrent = ObjwordDoc.Application.ActiveDocument.Tables.Add(rngCurrent,row,column)   for i = 1 to column   objwordDoc.Application.ActiveDocument.Tables(1).Rows(1).Cells(i).Range.InsertAfter theArray(i,1)   objwordDoc.Application.ActiveDocument.Tables(1).Rows(1).Cells(i).Range.ParagraphFormat.alignment=1   next   For i =1 to column   For j = 2 to row   objwordDoc.Application.ActiveDocument.Tables(1).Rows(j).Cells(i).Range.InsertAfter theArray(i,j)   objwordDoc.Application.ActiveDocument.Tables(1).Rows(j).Cells(i).Range.ParagraphFormat.alignment=1   Next   Next  End Sub   </SCRIPT>

三、直接在IE中打開,再存為Excel文件

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

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

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

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

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

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

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

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

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

“怎么用ASP導(dǎo)出Excel數(shù)據(jù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

分享文章:怎么用ASP導(dǎo)出Excel數(shù)據(jù)
鏈接地址:http://muchs.cn/article4/jpghie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、定制網(wǎng)站、外貿(mào)建站、網(wǎng)站內(nèi)鏈網(wǎng)站排名、移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管