现在使用:
<%
Dim ExcelSheet
Set ExcelSheet = CreateObject("Excel.Sheet")
Make Excel visible through the Application object.
ExcelSheet.Application.Visible = True
Place some text in the first cell of the sheet.
ExcelSheet.ActiveSheet.Cells(1,1).Value = "This is column A, row 1"
Save the sheet.
ExcelSheet.SaveAs "C:\TEST3.XLS"
Close Excel with the Quit method on the Application object.
ExcelSheet.Application.Quit
Release the object variable.
Set ExcelSheet = Nothing
%>
可以在我服务器的c:\创建一个test.xls,现在我想直接在客户端创建这个文件好像是不行了,可不可以直接从server把这个文件传送到客户端,然后再调用客户端的excel把这个文件打开。以上功能想用asp直接实现,可以否?据网友说,要使用组件的,否则有问题,是这样吗?能否不使用组件?如果不行,给一个组件代码好不好?谢谢各位。
当然可以用asp来实现
将返回文件类型设置为response.ContentType ="application/vnd.ms-excel",用response.binarywrite输出数据到客户端,如果他装有excel会自动打开
我觉得不需要组件
可以調用IE裡面的EXCEL控件,在IE里以EXCEL格式打開,裡面有一項EXPORT TO
EXCEL,調用EXCEL,然後另存為XLS格式的,就可以存在客戶端.
用ASP创建Microsoft Word 文件
随着人们对数据库及动态HTML (DHTML)的应用和对XML的兴趣的增加,所以在商业网站的数据中使用动态内容变得越来越普遍。这里我们就成功运用了一则最新的技巧——用ASP创建 Word 文件,我想您看后肯定会受到很大的启发。
原文出处:http://www.asptoday.com/articles/19990825.htm
背景
BuildDoc.asp是一个ASP文件,它读入一个网页表单的输出,并创建一个Microsoft Word文件作为输出, 其中包含根据表单内数据改变产生的一个表格。表单内容不再局限于静态信息。也就是说, 表单中所显示的内容可能随着用户的交互作用而改变。
BuildDoc所满足的商业需求是:根据销售人员网页列表变化的记录,建立表单信件。只有被销售人员修改过的数据才被发送到Word, 在那里这些数据被格式化到表格中。
BuildDoc读入表单上的所有信息,识别被改变的行,然后用被改变的行中包含的信息来创建一个 Microsoft Word文件。BuildDoc使用一个模板文件(buildDoc.dot),其中包含地址头和一些预先格式化的文本。然后向文件中写入一个表格,其中的每一行都对应与网页表单中被修改过的一行。
怎么做?
开始时,将所有的网页表单域读入接收网页的隐含表单域中。在下面的源代码中,请注意在Body标记中对“onLoad”的调用。它调用buildDoc VB脚本子程序,向它传递3个参数:页面中表单的内容、Word模板文件的位置、从输入表单中收到的行数。读所有的输入表单域,然后当页面装载后调用buildDoc子程序。为了简短起见,这里假定所有变量在使用之前都已被声明:
buildDoc.asp中装载输入表单域的代码如下:
〈!DOCTYPE HTML PUBLIC "-//W3C/DTD HTML 3.2 Final//EN">
〈HEAD>
〈TITLE>Build Document〈/TITLE>
〈META HTTP-EQUIV="Refresh" CONTENT="30;URL=orderForm.asp">
〈/HEAD>
〈%
dotLocation="servernamedirectory heTemplate.dot"
intRowCount = Request.Form("rowCount") initialize a row counter
%>
〈BODY Language="VBScript" onLoad="buildDoc document.theForm,
〈%=dotLocation%>,intRowCount>
〈FORM NAME="theForm">
〈%
itemCount = 0 set field counter to zero
For Each Item in Request.Form count up the form fields
itemCount = itemCount + 1 using For..Next loop
%>
〈INPUT TYPE="hidden" NAME="〈%=Item%>" VALUE="〈%=Request(Item)%>">
〈% Next %>
〈INPUT TYPE="hidden" NAME="numbRows" VALUE="〈%=intRowCount%>">
〈INPUT TYPE="hidden" NAME="fieldCount" VALUE="〈%=itemCount%>">
〈/FORM>
〈/BODY>
〈/HTML>
用下面例子中的代码来创建一个Word 文件对象。请注意在Internet Explorer 4+中,要将浏览器的安全性设置为Low或 Custom,以能使应用程序运行成功。
〈%
Set objWordDoc = CreateObject("Word.Document")
ObjWordDoc.Application.Documents.Add theTemplate, False
ObjWordDoc.Application.Visible=True
%>
调整数组的维数使它与网页表单所包含的行数相同。这时,将Y轴设为4个常量,这是输出文件中所需要 的栏数。X轴包含从表单中接收的行数。
〈% Redim Preserve theArray(4,intTableRows) %>
现在开始检查所有的表单行。在所有输入的网页表单域中循环,收集每个表单域名及其相应的值。逐个检查以决定将其放入哪个数列元素内,然后将其放入。以下举例代码中的SELECT CASE命令很重要,这决定表单域属于哪一列。为了方便,使用不确定编码的CASE选择。
〈%
For intCount = 0 to frmData.fieldCount.value
strOkay = "Y"
strSearch = frmData.elements(intCount).name load the field name
strValue = frmData.elements(intCount).value load the field value
strPosition = Instr(1,strSearch,"_") get pos val of "_"
intStringLen=strPosition-1
If intStrLen > 0 Then
strLeft = Left(strSearch,intStringLen)
strRight = Right(strSearch,(Len(strSearch)-Len(strLeft)-1))
Select Case strLeft
Case "SKU" intArrayY=0
Case "description" intArrayY=1
Case "price" intArrayY=2
Case "quantity" intArrayY=3
End Select
IntArrayX = strRight
If strOkay 〈> "N" Then
TheArray(intArrayY, intArrayX) = strValue
End If
End If
Next
%>
现在开始创建文件。对于激活的文件,用变量rngCurrent设置Microsoft Word文件对象RANGE,通过指定表格的位置( rngCurrent)以及行、列的数目来确定其大小。
〈%
Set rngCurrent = objWordDoc.Application.ActiveDocument.Content
Set tabCurrent = ObjWordDoc.Application.ActiveDocument.Tables.Add
rngCurrent,intNumrows,4)
%>
创建了有表格的文件之后,我们开始往表格中装入数据。首先指到第一行row(tabRow=1 ), 然后进行逐行循环。在每行结尾处插入回车[Chr(10)],以便产生行间空行,最后增加行计数器,用“FormatCurrency” 输出美圆值以保证使用$符号、逗号、小数点的位置。通过在
“ParagraphAlignment=2”处设置栏数来实现美圆数量的正确调整。用VBA容易一些,不象用VBScript那样难。
〈%
For j = 1 to intTableRows
ObjWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Borders.Enable=False
objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(1).Range.InsertAfter
theArray(1,j)
objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(2).Range.InsertAfter
theArray(2,j)
objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(3).Range.InsertAfter
FormatCurrency(theArray(3,j))
objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(4).Range.InsertAfter
theArray(4,j)
objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(4).Range.InsertAfter
Chr(10)
objWordDoc.Applicatoin.ActiveDocument.Tables(1).Rows(tabRow).Cells(3).
Range.ParagraphFormat.alignment=2
tabRow = tabRow + 1
Next
%>
最后用一些收尾性的文字来结束文件,指定模板位置,然后结束子程序。
〈%
objWordDoc.Application.ActiveDocument.Paragraph.Add.Range.
InsertAfter("Thank you for shopping at Acme Co., and please come again!")
objWordDoc.Application.ActiveDocument.Paragraph.Add.Range.InsertAfter(" ")
objWordDoc.Application.ActiveDocument.Paragraph.Add.Range.InsertAfter(" ")
objWordDoc.Application.ActiveDocument.Paragraph.Add.Range.InsertAfter("Regards,")
objWordDoc.Application.ActiveDocument.Paragraph.Add.Range.InsertAfter(" ")
objWordDoc.Application.ActiveDocument.Paragraph.Add.Range.
InsertAfter("Daryl B. Morticum")
objWordDoc.Application.ActiveDocument.Paragraph.Add.Range.
InsertAfter("Sales Associate")
End Sub
%>
希望对你做相同的工作能有一些帮助。我可以肯定需要从网页表单中创建文件的不止我自己。 这是我的方法。如果你有更好的方法,我很愿意学习。
<%@ Language=VBScript %>
<!--#include file=""-->
<%
数据库连接
<%
*************************
建立excel.application对象
*************************
dim objexcel
dim xlbook
dim sheetactive
dim strRange
SET objexcel=createobject("Excel.Application")
objexcel.visible=true
打开excel模板
SET xlbook=objexcel.workbooks.open(server.MapPath("../template/table1.xls"))
objexcel.sheets(1).select 选中工作页
SET sheetActive=objexcel.Activeworkbook.Activesheet
SET xlsheet=xlbook.worksheets(1)
SET xlsheet=nothing
sheetActive.range("g4").value=date()excel的常规操作,这里是添加时间
num=7从excel的第7行开始
***********************
循环至数据库的记录完毕
***********************
do until rs.eof
strRange="d"&num&":f"&num设定要填写内容的单元区域
sheetActive.range(strRange).font.size=10设定字体大小
sheetActive.range(strRange).WrapText=false设定文字回卷
sheetActive.range(strRange).ShrinkToFit=true 设定是否自动适应表格单元大小
sheetActive.range(strRange).value=array(rs(),......)把数据集中的数据填写到相应的单元
num=num+1
loop
*******************
excel临时文件的处理
*******************
function getTemporaryFile(myFileSystem)
dim tempFile,dotPos
tempFile=myFileSystem.getTempName
dotPos=instr(1,tempFile,".")
getTemporaryFile=mid(tempFile,1,dotPos)&"xls"
end function
SET myFs=createobject("scripting.FileSystemObject")
filePos=server.MapPath("")要存放打印临时文件的临时目录
fileName=getTemporaryFile(myFs)取得一个临时文件
myFs.DeleteFile(filePos&"*.xls")删除原先临时文件
SET myFs=nothing
推出excel应用
SET xlbook=nothing
objexcel.quit
SET objexcel=nothing
%>
ASP直接调用EXCEL数据的例子
例子:
在"c:\excel\book1.xls"存在一个EXCEL表book1.xsl,表的结构如下:
1 序号 名称 金额
2 1 张三 100
3 2 李四 200
4 3 王五 300
序号字段不为空
注意:excel 起始行是1而不是为0
<%@language=vbscript %>
<%
Set xlApp = server.CreateObject("Excel.Application")
strsource = "c:\excel\book1.xls"
Set xlbook = xlApp.Workbooks.Open(strsource)
Set xlsheet = xlbook.Worksheets(1)
i=1
response.write "<table cellpadding=0 cellspacing=0 border=1 width=500>"
while xlsheet.cells(i,1)<>""
response.write "<tr>"
response.write " <td height=20 align=center width=100>" & xlsheet.Cells(i, 1) & "</td>"
response.write " <td height=20 align=center width=200>" & xlsheet.Cells(i, 2) & "</td>"
response.write " <td height=20 align=center width=200>" & xlsheet.Cells(i, 3) & "</td>"
response.write "</tr>"
i=i+1
wend
response.write "</table>"
set xlsheet=nothing
set xlbook=nothing
xlApp.quit 千万记住要加这一句,否则每运行一次你的机器里就增加一个excel进程,而且无法释放。我试过"set
xlApp=nothing"是不行的。
%>