[轉(zhuǎn)帖]ASP導(dǎo)出Excel數(shù)據(jù)的四種方法
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
:[轉(zhuǎn)帖]ASP導(dǎo)出Excel數(shù)據(jù)的四種方法 ASP導(dǎo)出Excel數(shù)據(jù)的四種方法 一、使用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)很大的比重。 <%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 iColOffset = iColOff Else iColOffset = 2 End If End Property Public Property Let RowOffset(iRowOff) If iRowOff > 0 then iRowOffset = 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 '居中 iCol = iCol + 1 Next 'objField 'Display all of the data Do While Not objRS.EOF iRow = 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 iCol = 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 "在保存過程中有錯(cuò)誤!" End If Set objExcel = Nothing objRS.Close Set objRS = Nothing %> 二、用Excel的Application組件在客戶端導(dǎo)出到Excel或Word 注意:兩個(gè)函數(shù)中的“data“是網(wǎng)頁中要導(dǎo)出的table的 id <input type="hidden" name="out_word" οnclick="vbscript:buildDoc" value="導(dǎo)出到word" class="notPrint"> <input type="hidden" name="out_excel" οnclick="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文件 把讀出的數(shù)據(jù)用<table>格式,在網(wǎng)頁中顯示出來,同時(shí),加上下一句即可把EXCEL表在客客戶端顯示。 <%response.ContentType ="application/vnd.ms-excel"%> 注意:顯示的頁面中,只把<table>輸出,最好不要輸出其他表格以外的信息。 四、導(dǎo)出以半角逗號(hào)隔開的csv 用fso方法生成文本文件的方法,生成一個(gè)擴(kuò)展名為csv文件。此文件,一行即為數(shù)據(jù)表的一行。生成數(shù)據(jù)表字段用半角逗號(hào)隔開。(有關(guān)fso生成文本文件的方法,在此就不做介紹了) CSV文件介紹 (逗號(hào)分隔文件) 選擇該項(xiàng)系統(tǒng)將創(chuàng)建一個(gè)可供下載的CSV 文件; CSV是最通用的一種文件格式,它可以非常容易地被導(dǎo)入各種PC表格及數(shù)據(jù)庫中。 請(qǐng)注意即使選擇表格作為輸出格式,仍然可以將結(jié)果下載CSV文件。在表格輸出屏幕的底部,顯示有 "CSV 文件"選項(xiàng),點(diǎn)擊它即可下載該文件。 如果您把瀏覽器配置為將您的電子表格軟件與文本(TXT)/逗號(hào)分隔文件(CSV) 相關(guān)聯(lián),當(dāng)您下載該文件時(shí),該文件將自動(dòng)打開。下載下來后,如果本地已安裝EXCEL,點(diǎn)擊此文件,即可自動(dòng)用EXCEL軟件打開此文件。 該文章在 2023/5/20 16:00:16 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |