最近在做统计功能,要求统计结果(表格)既能查看(BS系统,在浏览器查看),又能输出为excel文件。对于输出excel文件,在网上找到n种方案,因为还需查看,最终选择了统计结果输出为table,查看时直接显示table,输出excel时把table写进输出流,ContentType设置为application/vnd.ms-excel再输出,具体方法如下:

软件环境:VS2008,C#,IE,office2010

<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<table>
......
</table>
</body>
</html>

2.Response的属性设置

Response关键属性有几个

 Public Sub toexecl(ByVal filenames As String)
'输出的应用类型Response.ContentType = "application/vnd.ms-excel";
'设定编码方式,若输出的excel有乱码,可优先从编码方面解决Response.Charset = "gb2312";
Response.ContentEncoding = System.Text.Encoding.UTF8
'//关闭ViewState,此属性在Page中
EnableViewState = False
'//filenames是自定义的文件名
Response.AppendHeader("Content-Disposition", "attachment;filename=" & filenames)
'//content是步骤1的html,注意是string类型
Response.Write(tablelist.Text)
Response.End()
End Sub

这样已经能发布最基本的excel,下面说些细节

1.样式最好用css,即设style属性或class属性,有些样式属性excel不认的。th、td的宽度最好在colgroup中设

2.在excel的格子中换行,可以用这个<br style='mso-data-placement:same-cell;'/>

3.关于边框宽度的问题,如果你的table是全边框,可以设置table的border属性,其中0=不显示边框。

如果表的格式比较复杂,特别是表头,有些边要隐藏的,这个要先把table的border=0,之后对每个th td用css的边框样式进行设置,但这里有个地方要注意,就是宽度,设为0.5pt(例:border-left: 0.5pt solid #000;),如果设为1px输出的边框会很粗。

4.excel空余部分边框的问题。用此方法输出的excel,空余部分边框都不显示,如果要做成想普通excel的样子,需在输出流的head部分加上excel的设置,例子如下:

<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
   <x:ExcelWorksheet>

<x:Name></x:Name>
    <x:WorksheetOptions>    
     <x:Selected/>     
    </x:WorksheetOptions>
   </x:ExcelWorksheet>  
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml><![endif]-->

最后附上一个成果作为例子,扩展名为xls可用excel打开

<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name></x:Name><x:WorksheetOptions><x:Selected/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
<style type="text/css">
.td
{
width: 84px;
}
.gdtjContainer .tb tr
{
text-align: center;
vertical-align: middle;
}
.gdtjContainer .tb th
{
border-left: 0.5pt solid #000;
border-bottom: 0.5pt solid #000;
text-align: center;
font-weight: normal;
font-size: 10pt;
middle: ;;height:30px;}
.gdtjContainer .header th
{
font-size: 12pt;
}
.gdtjContainer .tb tr th.noleftborder
{
border-left: none;
}
.gdtjContainer .tb tr th.rightborder
{
border-right: 0.5pt solid #000;
}
</style>
</head>
<body>
<div class="gdtjContainer">
<table class="tb" cellspacing="0" cellpadding="0" border="0" width="2184px">
<colgroup>
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
</colgroup>
<tr style="height: 40px">
<th style="font-size: 20pt; font-family: 宋体; border: none;" colspan="26">
2011年增城市单位土地使用权出让情况登记表(统计时间从2011-06-29至2011-06-30)
</th>
</tr>
<tr>
<th colspan="23" style="border-left: none;">
&nbsp;
</th>
<th style="text-align: left; font-size: 12pt; border-left: none;" colspan="3">
单位:万元、平方米
</th>
</tr>
<tr class="header">
<th rowspan="2">
合同编号
</th>
<th colspan="2" rowspan="2">
用地单位
</th>
<th colspan="2" rowspan="2">
土地座落
</th>
<th rowspan="2">
供地面积
</th>
<th style="border-left: none">
&nbsp;
</th>
<th>
&nbsp;
</th>
<th rowspan="2">
用途
</th>
<th colspan="3" rowspan="1">
出让金
</th>
<th rowspan="2">
容积率
</th>
<th rowspan="2">
建筑密度
</th>
<th rowspan="2">
绿地率
</th>
<th rowspan="2">
规划建筑面积
</th>
<th rowspan="2">
出让方式
</th>
<th rowspan="2">
审批日期
</th>
<th rowspan="2">
合同签订日期
</th>
<th rowspan="2">
动工期限
</th>
<th rowspan="2">
竣工日期
</th>
<th rowspan="2">
批次情况
</th>
<th rowspan="2">
合同约定缴费期限
</th>
<th rowspan="2">
缴费情况
</th>
<th rowspan="2">
滞纳金
</th>
<th rowspan="2" class="rightborder">
备注
</th>
</tr>
<tr style="height: 40px" class="header">
<th>
新增面积
</th>
<th style="font-size: 10pt;">
保障性住房用地占用面积
</th>
<th>
应缴
</th>
<th>
已缴
</th>
<th>
未缴
</th>
</tr>
<tr>
<th>
440183-2011-
</th>
<th colspan="2">
</th>
<th colspan="2">
&nbsp;
</th>
<th>
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
拍卖出让
</th>
<th>
&nbsp;
</th>
<th>
2011-06-29
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th class="rightborder">
&nbsp;
</th>
</tr>
<tr>
<th>
&nbsp;
</th>
<th colspan="2">
合计
</th>
<th colspan="2">
&nbsp;
</th>
<th>
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th class="rightborder">
&nbsp;
</th>
</tr>
</table>
</div>
</body>
</html>

转载于http://www.cnblogs.com/cannel/archive/2011/06/30/2094189.html

乱码解决:

HttpContext.Current.Response.Write("<meta   http-equiv=Content-Type   content=text/html;charset=GB2312>");
string fileName = HttpUtility.UrlEncode(ExcelFileName+".xls", Encoding.GetEncoding("GB2312"));
HttpContext.Current.Response.AddHeader("content-disposition",
"attachment;filename=" + fileName);

参考:http://www.cnblogs.com/jillzhang/archive/2007/09/26/906588.html

html表格导出Excel的一点经验心得(转载)的更多相关文章

  1. html表格导出Excel的一点经验心得

    最近在做统计功能,要求统计结果(表格)既能查看(BS系统,在浏览器查看),又能输出为excel文件.对于输出excel文件,在网上找到n种方案,因为还需查看,最终选择了统计结果输出为table,查看时 ...

  2. vue+iview中的table表格导出excel表格

    一.iveiw框架中table中有exportCsv()方法可以导出.csv后缀文件,类似于excel文件,但是并不是excel文件. 二.实现table表格导出excel文件利用Blob.js 和 ...

  3. html table表格导出excel的方法 html5 table导出Excel HTML用JS导出Excel的五种方法 html中table导出Excel 前端开发 将table内容导出到excel HTML table导出到Excel中的解决办法 js实现table导出Excel,保留table样式

    先上代码   <script type="text/javascript" language="javascript">   var idTmr; ...

  4. [ExtJS5学习笔记]第三十三节 sencha extjs 5 grid表格导出excel

    使用extjs肯定少不了使用表格控件,用到表格,领导们(一般)还是惯于使用excel看数据,所以用到extjs表格的技术猿们肯定也会有导出表格excel这一个需求,本文主要针对如何在用extjs将gr ...

  5. 前端实现table表格导出excel

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. POI导出EXCEL经典实现(转载)

    1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...

  7. vue+element 表格导出Excel文件

    https://www.cnblogs.com/bobodeboke/p/8867481.html  非常感谢 这个大佬 才让我搞到了Blob.js 和 Export2Excel.js 如果最后运行时 ...

  8. Element-ui组件库Table表格导出Excel表格

    安装npm install --save xlsx file-saver 两个插件的详细地址在下面 https://github.com/SheetJS/js-xlsxhttps://github.c ...

  9. html表格导出Excel的实例

    1. 拼成出完整的HMTL的Table代码片段后,转成二进制byte[]类型并存入数据库中,供下载时调出来使用. System.Text.StringBuilder sb = new StringBu ...

随机推荐

  1. radio,checkbox,select,input text获取值,设置哪个默认选中

    11 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title& ...

  2. ELK日志系统

    ELK stack是又Elasticsearch,lostash,kibana 三个开源软件的组合而成,形成一款强大的实时日志收集分析展示系统. Logstash:日志收集工具,可以从本地磁盘,网络服 ...

  3. hive报错汇总

    1.需要注意的是,要在namenode(超级用户)上操作,貌似是 hive> insert into table record_partition partition(trancation_da ...

  4. oracle安装完成后目录中不论有没有tnsnames.ora和listener.ora文件 PLSQL都能连上的问题解决方法

    今天遇到这个问题了,发现listener.ora文件和tnsnames.ora文件在Net Work文件夹下没有,正常情况下安装完oracle或者是oracle Client是会有的,但是在Net M ...

  5. 20145216史婧瑶《Java程序设计》第三次实验报告

    实验三 敏捷开发与XP实践 实验内容 使用git上传代码,两个人进行小组合作,队友下载代码并修改再重新上传. 实验步骤 一. 使用git上传代码 1.找到需要push的文件所在文件夹,右键点击Git ...

  6. 20135320赵瀚青LINUX第五章读书笔记

    第五章--系统调用 5.1 与内核通信 作用 1.为用户空间提供一种硬件的抽象接口 2.保证系统稳定和安全 3.除异常和陷入,是内核唯一的合法入口. API.POSIX和C库 关于Unix接口设计:提 ...

  7. WIN7环境安装informatica 提示 不能创建Domain或者node

    查看infa安装的bat文件install.bat,会发现,它调用的是.\Server\Windows\Disk1\InstData\VM\install.exe.所以,我们在安装执行,右键insta ...

  8. Dijkstra FORMCM

    Dijkstra 算法 date: 2018/2/2 author:pprp theme:Dijstra 简述 辅助空间 vis数组:记录是否已经判断过 dis数组:记录指定原点到其他点的距离 mp二 ...

  9. base64 原理

    Base64编码之所以称为Base64,是因为其使用64个字符来对任意数据进行编码,同理有Base32.Base16编码.标准Base64编码使用的64个字符为: 这64个字符是各种字符编码(比如AS ...

  10. nlp基本知识点(不断更新)

    1.精确率与召回率 召回率也称为查全率,指的是:你查询到的相关数目/(你查询到的相关的数目+你没有查询到的相关的数目). 精确率: A/A+B 召回率:A/A+C 再比如: 我 是 中国人 这里正确的 ...