标题: ASP教程之WASP导出Excel数据的四种办法 [打印本页] 作者: 灵魂腐蚀 时间: 2015-2-16 00:23 标题: ASP教程之WASP导出Excel数据的四种办法 结论:和PHP一样,ASP简单而易于维护,很适合小型网站应用,通过DCOM和MTS技术,ASP甚至还可以完成小规模的企业应用,但ASP的致命缺点就是不支持跨平台的系统,在大型项目开发和维护上非常困难。 1、利用OWC 甚么是OWC?
OWC是Office Web Compent的缩写,即Microsoft的Office Web组件,它为在Web中绘制图形供应了天真的同时也是最根基的机制。在一个intranet情况中,假如可以假定客户机上存在特定的阅读器和一些功效壮大的软件(如IE5和Office 2000),那末就有才能使用Office Web组件供应一个交互式图形开辟情况。这类形式下,客户端任务站将在全部义务平分担很大的比重。
<%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
'设置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 "在保留过程当中有毛病!"
End If
Set objExcel = Nothing
objRS.Close
Set objRS = Nothing
%>
2、用Excel的Application组件在客户端导出到Excel或Word
注重:两个函数中的“data“是网页中要导出的table的 id
<input type="hidden" name="out_word" value="导出到word" class="notPrint">
<input type="hidden" name="out_excel" value="导出到excel" class="notPrint">
导出到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>
导出到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("综合查询了局集") //显示表格题目
objWordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("")
Set rngPara = objWordDoc.Application.ActiveDocument.Paragraphs(1).Range
With rngPara
.Bold = True //将题目设为粗体
.ParagraphFormat.Alignment = 1 //将题目居中
.Font.Name = "隶书" //设定题目字体
.Font.Size = 18 //设定题目字体巨细
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>
3、直接在IE中翻开,再存为EXCEL文件
把读出的数据用<table>格局,在网页中显示出来,同时,加高低一句便可把EXCEL表在客客户端显示。
<%response.ContentType ="application/vnd.ms-excel"%>
注重:显示的页面中,只把<table>输入,最好不要输入其他表格之外的信息。
4、导出以半角逗号离隔的csv
用fso办法生成文本文件的办法,生成一个扩大名为csv文件。此文件,一行即为数据表的一行。生成数据表字段用半角逗号离隔。(有关fso生成文本文件的办法,在此就不做引见了)
CSV文件引见 (逗号分隔文件)
选择该项体系将创立一个可供下载的CSV 文件; CSV是最通用的一种文件格局,它可以十分轻易地被导入各类PC表格及数据库中。
请注重即便选择表格作为输入格局,依然可以将了局下载CSV文件。在表格输入屏幕的底部,显示有 "CSV 文件"选项,点击它便可下载该文件。
假如您把阅读器设置装备摆设为将您的电子表格软件与文本(TXT)/逗号分隔文件(CSV) 相干联,当您下载该文件时,该文件将主动翻开。下载上去后,假如当地已装置EXCEL,点击此文件,便可主动用EXCEL软件翻开此文件。