ForWork.Net

欢迎参观ForWork.Net,快乐工作·快乐生活!你我的快乐生活:)

上一篇: 截取带html标志的字符串函数 下一篇:Word文档页码从第n页起出现的设置方法

今天为93315文章后台做了一个导出Excel文档的功能,现将里面的主要代码提取出来于大家分享

xls.asp

<%
Set xlApplication = Server.CreateObject("Excel.Application") '调用excel对象
xlApplication.Visible = False '无需打开excel
xlApplication.SheetsInNewWorkbook=1 '指定excel中表的数量
xlApplication.Workbooks.Add '添加工作簿
Set xlWorksheet = xlApplication.Worksheets(1) '生成第1个工作表的子对象
xlWorksheet.name="统计" '指定工作表名称
'指定列的宽度以及对齐方式
xlApplication.ActiveSheet.Columns(1).ColumnWidth=5
xlApplication.ActiveSheet.Columns(1).HorizontalAlignment=3
xlApplication.ActiveSheet.Columns(2).ColumnWidth=40
xlApplication.ActiveSheet.Columns(2).HorizontalAlignment=1
xlApplication.ActiveSheet.Columns(3).ColumnWidth=5
xlApplication.ActiveSheet.Columns(3).HorizontalAlignment=3
xlApplication.ActiveSheet.Columns(4).ColumnWidth=15
xlApplication.ActiveSheet.Columns(4).HorizontalAlignment=1
xlApplication.ActiveSheet.Columns(5).ColumnWidth=12
xlApplication.ActiveSheet.Columns(5).HorizontalAlignment=1
xlApplication.ActiveSheet.Columns(6).ColumnWidth=12
xlApplication.ActiveSheet.Columns(6).HorizontalAlignment=3
'xlApplication.ActiveSheet.Rows(i).RowHeight = 30'行的高度
'指定列的高度以及特定列
xlWorksheet.Range(xlWorksheet.Cells(1,1), xlWorksheet.Cells(1,6)).MergeCells =True '合并列
xlWorksheet.Range("A1").value="2005年统计"
xlWorksheet.Range("A1").font.Size=14'字体大小
xlWorksheet.Range("A1").font.bold=true'粗体
xlWorksheet.Range("A1").HorizontalAlignment=3'水平对齐
xlWorksheet.Range("A1").VerticalAlignment=3'垂直对齐

xlWorksheet.Cells(2,1).Value = "序号"
xlWorksheet.Cells(2,2).Value = "标题"
xlWorksheet.Cells(2,3).Value = "图"
xlWorksheet.Cells(2,4).Value = "部门"
xlWorksheet.Cells(2,5).Value = "作者"
xlWorksheet.Cells(2,6).Value = "时间"

xlWorksheet.Range("A2:F2").Borders.LineStyle=1

'--------------------------------------------------自己可做循环i=i+1(数据库数据)
'xlWorksheet.Cells(2+i,1).Value = i
'xlWorksheet.Cells(2+i,2).Value = topic
'xlWorksheet.Cells(2+i,3).Value = img_str
'xlWorksheet.Cells(2+i,4).Value = nfrom
'xlWorksheet.Cells(2+i,5).Value = writer
'xlWorksheet.Cells(2+i,6).Value = ntime
'--------------------------------------------------
Set fs = CreateObject("Scripting.FileSystemObject")
tfile=Server.MapPath("test.xls")
if fs.FileExists(tfile) then
Set f = fs.GetFile(tfile)
f.delete true
Set f = nothing
end if
Set fs = nothing
xlWorksheet.SaveAs tfile '保存文件
xlApplication.Quit '释放对象
Set xlWorksheet = Nothing
Set xlApplication = Nothing
%>
<p align="center"><a href="downfile.asp?fileSpec=<%=tfile%>">下载</a></p>


downfile.asp

<%
Function downLoadFile(FileSpec)
on error resume next
 Const ForReading=1
 Const TristateTrue=-1
 Const FILE_TRANSFER_SIZE=1024 '16384
 Dim objFileSystem, objFile, objStream
 Dim char
 Dim sent
 Set objFileSystem = CreateObject("Scripting.FileSystemObject")
 If objFileSystem.FileExists(fileSpec)=false Then
 response.write("<Script>alert(""请求文件不存在!"");history.back();</script>")
 Exit Function
 End If
 FileName = objFileSystem.GetFileName(FileSpec)
 send=0
 TransferFile = True
 Set objFileSystem = Server.CreateObject("Scripting.FileSystemObject")
 Set objFile = objFileSystem.GetFile(FileSpec)
 Set objStream = objFile.OpenAsTextStream(ForReading, TristateTrue)
 Response.AddHeader "content-type", "application/octet-stream"
 Response.AddHeader "Content-Disposition","attachment;filename=" & filename
 
 Response.AddHeader "content-length", objFile.Size
 Do While Not objStream.AtEndOfStream
 char = objStream.Read(1)
 Response.BinaryWrite(char)
 sent = sent + 1
 If (sent MOD FILE_TRANSFER_SIZE) = 0 Then
 Response.Flush
 If Not Response.IsClientConnected Then
 TransferFile = False
 Exit Do
 End If
 End If
 Loop
 Response.Flush
 If Not Response.IsClientConnected Then TransferFile = False
 objStream.Close
 Set objStream = Nothing
 Set objFileSystem = Nothing
End Function
fileSpec =Lcase(Cstr(Trim(Request("fileSpec"))))
 downLoadFile(fileSpec)
%>

 

 

如果运行不了 请为Everyone用户增加 Excel 组件的访问权限

开始菜单-->运行-->输入"DCOMCNFG"-->展开"组件服务"-->计算机-->我的电脑-->DCOM配置-->找到你需要修改的组件(如 microsoft Excel 应用程序)-->属性-->安全-->启动和激活权限-->选择"自定义"-->编辑. 如果是WEB使用这个组件 请加入Everyone --> 然后勾上需要的权限.

点击这里获取该日志的TrackBack引用地址

  • Gravatar Icon
    回复
  • quote 1.李娟2007-9-6 22:05:43
  • 这些东西我是看不懂啦    不过觉得你好厉害
  • Gravatar Icon
    回复
  • quote 2.earth2007-11-1 13:48:42
  • 插入图片可以不~~~
    Person01 于 2007-11-1 13:48:42 回复
    这个暂时倒没有试过,如果是外部数据,这个可以直接调用,如果是写入数据库的话,估计没有,因为excel不支持
  • Gravatar Icon
    回复
  • quote 3.kusi2009-11-15 16:36:06
  • 我也遇到类似的问题,你解说得很清楚,问题已解决,谢谢

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于本文

您正在阅读的是:asp导出Excel文档代码
Person01 发表于:2007-9-6 15:9:22
分类:技术文摘
关键词:asp  excel  代码  

最近发表

最新评论及回复

最近留言

友情链接

控制面板

Search

网站分类

文章归档

图标汇集

订阅ForWork.Net

什么是RSS订阅?查看解释
订阅到您的在线阅读器

抓虾 google reader my yahoo bloglines 鲜果 哪吒

博客作者

Person01

Copyright 2006-2008 ForWork.Net 【快乐工作·快乐生活】 Rights Reserved.  苏ICP备05070716号

Powered By Z-Blog 1.8 Walle Build 100427 Code detection by Codefense 网络支持 网服数据