您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“怎么用ASP導(dǎo)出Excel數(shù)據(jù)”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
我們有時候需要把數(shù)據(jù)導(dǎo)出來,作為參考等等。下面就為你介紹,ASP導(dǎo)出Excel書籍的四種方法。
一、使用OWC
什么是OWC?
OWC是office Web Compent的縮寫,即Microsoft的office Web組件,它為在Web中繪制圖形提供了靈活的同時也是最基本的機(jī)制。在一個intranet環(huán)境中,如果可以假設(shè)客戶機(jī)上存在特定的瀏覽器和一些功能強(qiáng)大的軟件(如IE5和office 2000),那么就有能力利用office Web組件提供一個交互式圖形開發(fā)環(huán)境。這種模式下,客戶端工作站將在整個任務(wù)中分擔(dān)很大的比重。
以下為引用的內(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é)果集") //顯示表格標(biāo)題 objwordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("") Set rngPara = objwordDoc.Application.ActiveDocument.Paragraphs(1).Range With rngPara .Bold = True //將標(biāo)題設(shè)為粗體 .ParagraphFormat.Alignment = 1 //將標(biāo)題居中 .Font.Name = "隸書" //設(shè)定標(biāo)題字體 .Font.Size = 18 //設(shè)定標(biāo)題字體大小 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方法生成文本文件的方法,生成一個擴(kuò)展名為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)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。