论坛登陆 用户: 密码:
联系我们
设为首页
加入收藏
业界新闻 网络编程 程序开发 网页图象 聊天通讯 软件应用 网络安全 硬件学堂 教育频道 站长club
  ·推荐新闻
 
·美前任官员认为amd告倒英特
·搜索引擎关键字排行简介
·网站优化教程(一)
·msn近期遭受木马病毒骚扰 用
·用qq管理你的系统^_^ 
·测评中心金山毒霸联合发布7
·qq群聊实名 普通用户不受影
·雅虎思科联手推数字邮件签名
·这18条背下来没人敢和你忽悠
·自己动手,拯救丢失的硬盘数
  ·资料搜索
 
  ·相关文章
·君子动口不动手--用好word xp语
·在word中高效率应用表格的选择
·让powerpoint播放软件自动播放
·提高word办公效率锦囊妙计
·excel中双击格式刷的妙用
·快速定制word页面
·在word中实现文件的预览
·excel工作表使用小技巧
·巧用word“版本”功能加密
·word 2000的另类选取
  ·热门新闻

asp导出excel数据的四种方法


 日期:2005-7-18 15:06:48     来源:易特网络技术   编辑:黑鹰  点击:

一、使用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
%> 

 


  二、用excel的application组件在客户端导出到excel或word

  注意:两个函数中的“data“是网页中要导出的table的 id

<input type="hidden" name="out_word" onclick="vbscript:builddoc" value="导出到word" class="notprint">
<input type="hidden" name="out_excel" onclick="automateexcel();" 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> 

  三、直接在ie中打开,再存为excel文件

  把读出的数据用<table>格式,在网页中显示出来,同时,加上下一句即可把excel表在客客户端显示。

<%response.contenttype ="application/vnd.ms-excel"%> 

  注意:显示的页面中,只把<table>输出,最好不要输出其他表格以外的信息。

  四、导出以半角逗号隔开的csv

  用fso方法生成文本文件的方法,生成一个扩展名为csv文件。此文件,一行即为数据表的一行。生成数据表字段用半角逗号隔开。(有关fso生成文本文件的方法,在此就不做介绍了)

  csv文件介绍 (逗号分隔文件)

  选择该项系统将创建一个可供下载的csv 文件; csv是最通用的一种文件格式,它可以非常容易地被导入各种pc表格及数据库中。

  请注意即使选择表格作为输出格式,仍然可以将结果下载csv文件。在表格输出屏幕的底部,显示有 "csv 文件"选项,点击它即可下载该文件。

  如果您把浏览器配置为将您的电子表格软件与文本(txt)/逗号分隔文件(csv) 相关联,当您下载该文件时,该文件将自动打开。下载下来后,如果本地已安装excel,点击此文件,即可自动用excel软件打开此文件。

 

上一篇:word xp中的图片也能随意旋转
下一篇:让word也来播放flash动画
[发送给好友] [打印本页] [关闭窗口] [返回顶部转载请注明来源:http://www.iyit.net
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
责任编辑: 黑鹰 投稿作者: 易特网络
信息来源: 易特网络技术 录入时间: 2005-7-18 15:06:48
浏览次数: 投稿信箱: shtghy@163.com
设置首页 - 版权声明 - 广告服务 - 关于我们 - 联系我们 - 友情连接
copyrights ©2004-2005 iyit.net all rights reserved. 网站合作、广告联系qq:147007642、466949678
易特网络技术 点击这里给我发消息