一个用组件动态创建excel文件的实例
在精华区中有一篇关于在asp中动态创建的excel文章, 但实际上我们会发现如果我们在asp中用set myexcelchart = server.createobject("excel.sheet")是行不通的. 这样做的话会出现如下的错误信息:
only inproc server components should be used. if you want to use localserver components, you must set the aspallowoutofproccomponents metabase setting. please consult the help file for important considerations关于此出错信息的详细内容你可以看:
http://msdn.microsoft.com/workshop/server/components/outproc.asp
所以, 要想在服务器自动生成excel文件还是必须通过组件来实现(个人意见,如果你有更好的方法请告诉我:-)).
设计环境:vb6.0
运行环境:nt4.0(sp5)+iis4.0+mts
1.新建一个dll工程.工程名为p_excel,类名为c_excel
2.在"project"->"references"中选中"microsoft excel 9 object library".
3.代码
option explicit
dim oexcel as excel.application
dim osheet as excel.worksheet
dim otitle as excel.range
public sub createexcel()
set oexcel = new excel.application
oexcel.visible = false
oexcel.workbooks.add
set osheet = oexcel.workbooks(1).worksheets("sheet1")
osheet.activate
set otitle = osheet.range("a1")
otitle.value = "excel title"
otitle.font.bold = -1
otitle.font.size = 18
otitle.font.name = "arial"
osheet.saveas "allen.xls"
oexcel.quit
set oexcel = nothing
end sub
4.编译生成p_excel.dll
5.使用mts注册p_excel.dll
6.asp文件代码并在iis中设置要生成excel文件的虚拟目录对用户有写的权限.
excel.asp
<%
set myexcel=server.createobject("p_excel.c_excel")
myexcel.createexcel
set myexcel=nothing
%>
7.运行excel.asp,在相关目录下我们就可以找到生成的excel文件.
改进的建议:
1.在p_excel.dll中增加(range,value)的属性就可以利用从数据库中查询返回的记录动态生成excel文档.
2.增加email功能自动将生成的excel文件发送给相关用户.
如果你还有其他的建议请告诉我:-)
