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

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

1.输出流内容的格式


<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关键属性有几个


//输出的应用类型
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(content);
Response

这样已经能发布最基本的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]-->

5. 应用中经常会遇到要从系统或数据库中导出数据平面文件,一般是导出到txt,csv或excel。txt和csv一般用在系统间的数据交换,而 excel一般有较好的显示效果,可以按照一定的模板导出,导出就不用再排版了,使用简单,如果是使用做报表一般都导出excel文件。

但是使用com组件导出到Excel数据很慢,有另一种生成excel文件的方式就是通过html和css快速导出数据同时并能设置样式,使用这种方式有两个优点:1是速度快,2是不需安装excel支持。

  当使用这种方法导出文件时,有时会遇到一个问题,就是导出的Excel经常会把我们的数据自动识别为其他格式,例如只有纯数字的字段在导出到 Excel后会被自动识别为数字格式,而一旦数字超过11位,Excel便会将其以科学计数法的形式来显示,比如身份证号码,带区号的电话号码等。

  解决方法有多种,这里只介绍一种本人认为最好的一种,即使用CSS给出现问题的表格字段(如<TD>)应用mso-number- format属性,用这个属性指定某单元格的数据格式,避免Excel自动转换格式。mso-number-format是Office提供的格式, 如果您将office文档转成HTML就会有这样的标志。MSO表示Microsoft Office。

示例:

<style type="text/css">
.format{
mso-number-format:'\@';
}
</style>

<td class="format">123456789012345</td>

  在css中加入:mso-number-format定义数据格式,格式可以在excel中查看自定义格式,具体可以参考一下:
mso-number-format:"0" NO Decimals 
mso-number-format:"0\.000" 3 Decimals 
mso-number-format:"\#\,\#\#0\.000" Comma with 3 dec 
mso-number-format:"mm\/dd\/yy" Date7 
mso-number-format:"mmmm\ d\,\ yyyy" Date9 
mso-number-format:"m\/d\/yy\ h\:mm\ AM\/PM" D -T AMPM 
mso-number-format:"Short Date" 01/03/1998 
mso-number-format:"Medium Date" 01-mar-98 
mso-number-format:"d\-mmm\-yyyy" 01-mar-1998 
mso-number-format:"Short Time" 5:16 
mso-number-format:"Medium Time" 5:16 am 
mso-number-format:"Long Time" 5:16:21:00 
mso-number-format:"Percent" Percent - two decimals 
mso-number-format:"0%" Percent - no decimals 
mso-number-format:"0\.E+00" Scientific Notation 
mso-number-format:"\@" Text 
mso-number-format:"\#\ ???\/???" Fractions - up to 3 digits (312/943) 

最后附上一个成果作为例子,扩展名为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">
45654
</th>
<th colspan="2">
&nbsp;
</th>
<th>
1110000
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
111
</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>
1110000
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
&nbsp;
</th>
<th>
111
</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>
 

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. vue+element 表格导出Excel文件

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

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

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

  8. html表格导出Excel的实例

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

  9. webform 不实用office控件导出excel StringBuilder 类型拼接字符串表格导出excel

    StringBuilder sb = new StringBuilder(); sb.AppendLine("<meta http-equiv=\"Content-Type\ ...

随机推荐

  1. C++(四十三) — 函数模板机制

     1.普通函数与模板函数调用原则 函数模板可以像普通函数一样被重载: 当函数模板和普通函数都符合条件时,编译器优先考虑普通函数: 但如果函数模板产生一个更好的匹配,则选择函数模板: 可以通过空模板实参 ...

  2. IDG资本

    https://baike.baidu.com/item/IDG/10412 美国国际数据集团(International Data Group) 是全世界最大的信息技术出版.研究.发展与风险投资公司 ...

  3. 远程桌面远程代码执行漏洞(CVE-2019-0708)Poc

    此漏洞影响范围: Windows 7 Windows Server 2008 R2 Windows Server 2008 Windows 2003 Windows XP Windows 8和Wind ...

  4. 使用BERT模型生成句子序列向量

    之前我写过一篇文章,利用bert来生成token级向量(对于中文语料来说就是字级别向量),参考我的文章:<使用BERT模型生成token级向量>.但是这样做有一个致命的缺点就是字符序列长度 ...

  5. 01.Vue前端路由学习目标

    路由的基本概念与原理 vue-router的基本使用 vue-router嵌套路由 vue-router动态路由匹配 vue-router命名路由 vue-router编程式导航 基于vue-rout ...

  6. [RxJS] Subject asObservable() method

    You can create your own state store, not using any state management libraray. You might have seen th ...

  7. MySQL读写分离项目配置

    一.项目信息 1.拓扑 二.环境规划 1.主机信息 2.软件信息 3.MySQL中间件 三.配置

  8. GitHub上的一些使用技巧

    1.搜索 转:掌握 3 个搜索技巧,在 GitHub 上快速找到实用软件资源 例如 查找位于深圳的C#开发者 2.查看文件历史提交记录 定位至需要查看的文件 修改地址栏github.com 为 git ...

  9. rpm命令是RPM软件包的管理工具

    rpm命令是RPM软件包的管理工具.rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎.逐渐受到其他发行版的采用.RPM ...

  10. 2017.10.3 国庆清北 D3T2 公交车

    题目描述 LYK在玩一个游戏. 有k群小怪兽想乘坐公交车.第i群小怪兽想从xi出发乘坐公交车到yi.但公交车的容量只有M,而且这辆公交车只会从1号点行驶到n号点. LYK想让小怪兽们尽可能的到达自己想 ...