欢迎光临
我们一直在努力

Web页面数据导出方法概述之导出excel-.NET教程,Web Service开发

建站超值云服务器,限时71元/月

前言:

随着bs体系结构的广泛使用,相对应的数据保存技术也要改进,对应web页面,也就是我们通常认识到的html文件,由标示关键字与数据混合组成的文件。web页面数据导出简单地说,就是分离数据与格式,同时保存数据为另外一种格式。

正文:

分析http response等文件头信息,对如何处理web页面非常有用,下面简单地介绍几种常用的方法,算是在技术上学习,非理论上学习。

下面的代码分析如何通过修改http头文件,产生excel的文件供用户下载,以达到数据导出的功能。这种非实际在服务器端生成文件的优点就是,对于并发请求该页面的用户来说不会发生服务器端文件同名覆盖的问题!一般的解决方法有根据用户名,ip,随机数等。

dim resp as httpresponse

dim colcount as integer = mydatagrid.columns.count – 1

resp = page.response

resp.contentencoding = system.text.encoding.getencoding("gb2312") 解决中文乱码之关键

resp.charset = "utf-8"

resp.addfiledependency(filename)

resp.contenttype = "text/html"

resp.appendheader("content-type", "text/html; charset=gb2312")

resp.contenttype = "text/csv" 通过修改文件类型可以让用户下载为csv类型的文件,修改text/**内容

resp.appendheader("content-disposition", "attachment;filename=" + filename + ".csv") 必要,做成下载文件

实际从下载的保存文件类型来看,可以使txt,可以html,可以xls,用户未必知道?且保存为xls的文件,数据包含在一列内

dim colheaders as string = ""

imports system.text

dim stritems as stringbuilder = new stringbuilder

dim mycol as datagridcolumn

dim i as integer

for i = 0 to colcount

mycol = mydatagrid.columns(i)

if mycol.visible = true then

colheaders = colheaders & mycol.headertext.tostring & ","

end if

next

if colheaders.length > 0 then

colheaders = colheaders.substring(0, colheaders.lastindexof(","))

end if

colheaders = colheaders & chr(13) & chr(10)

resp.write(colheaders)

dim colrow as string

dim item as datagriditem

for each item in mydatagrid.items

resp.write(formatexportrow(colcount, item, mydatagrid))

‘将数据写入对应的单元格

next item

resp.end()

也有描述如何将水晶报表导出为pdf文件保存的代码,但是这种方式以及具体实现的过程,还在研究中。基本的想法是可以通过pdf然后利用插件打开,或者直接保存pdf

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Web页面数据导出方法概述之导出excel-.NET教程,Web Service开发
分享到: 更多 (0)

相关推荐

  • 暂无文章