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

System.Text.StringBuilder sb = new StringBuilder();
sb.AppendLine(@"<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>");
sb.AppendLine(@"<head>");
sb.AppendLine(@"<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
sb.AppendLine(@"<meta name='ProgId' content='Excel.Sheet'>");
sb.AppendLine(@"<style>");
sb.AppendLine(@".Header{background-color:#B8CCE4;font-family: Arial;}");
sb.AppendLine(@".Content{background-color:#B8CCE4;text-align: left;font-family: Times New Roman;}");
sb.AppendLine(@".N0{background-color:#B8CCE4;text-align: right ;font-family: Times New Roman;mso-number-format:'\#\,\#\#0';}");
sb.AppendLine(@".N2{background-color:#B8CCE4;text-align: right ;font-family: Times New Roman;mso-number-format:'\#\,\#\#0\.00';}");
sb.AppendLine(@".N6{background-color:#B8CCE4;text-align: right ;font-family: Times New Roman;mso-number-format:'\#\,\#\#0\.000000';}");

//下面一行的代码,是为了解决超过11位数字的纯文本导出Excel后,Excel默认会转为科学计数法表示,加入下面的class并标注在需要显示的字段的td后,导出Excel可以强制把数字用文本表示
sb.AppendLine(@".CvtTxt{background-color:#B8CCE4;text-align: left ;font-family: Times New Roman;mso-number-format:'\@';}");  
sb.AppendLine(@"</style>");
sb.AppendLine(@"</head>");
sb.AppendLine(@"<body>");
sb.AppendLine(@"<div id='divSplit' align='center' x:publishsource='Excel'>");
sb.AppendLine(@" <table border='2' style='border-collapse:collapse;'>");
sb.AppendLine(@" <tr>");
sb.AppendLine(@" </tr>");
sb.AppendLine(@" <tr>");
sb.AppendLine(@" <th colspan='14' bgcolor='#538ED5' style='text-align: center; font-family: Times New Roman'>" + "MyCoreInfo Provide" + "</th>");
sb.AppendLine(@" </tr>");
if (exchangeRate != null)
{
sb.AppendLine(@" <tr>");
sb.AppendLine(@" <th colspan='11' bgcolor='#B8CCE4' style='border-right:0'></th>");
sb.AppendLine(@" <th bgcolor='#B8CCE4' style='border-right:0;border-left:0;text-align:left'>Exchange Rate: </th>");
sb.AppendLine(@" <th colspan='2' bgcolor='#B8CCE4' style='border-left:0;text-align:right;mso-number-format:\#\,\#\#0\.000000'>" + exchangeRate.GetValueOrDefault().ToString("N6") + "</th>");
sb.AppendLine(@" </tr>");
}
if (stockPrice != null)
{
sb.AppendLine(@" <tr>");
sb.AppendLine(@" <th colspan='11' bgcolor='#B8CCE4' style='border-right:0'></th>");
sb.AppendLine(@" <th bgcolor='#B8CCE4' style='border-right:0;border-left:0;text-align:left'>Stock Closing Price: </th>");
sb.AppendLine(@" <th colspan='2' bgcolor='#B8CCE4' style='border-left:0;text-align:right;mso-number-format:\#\,\#\#0\.00'>" + stockPrice.GetValueOrDefault().ToString("N2") + "</th>");
sb.AppendLine(@" </tr>");
}

sb.AppendLine(@" <tr>");
sb.AppendLine(@" <td class='Content'>Forbid Exercise</td>");
sb.AppendLine(@" <td class='Content'>OrderID</td>");
sb.AppendLine(@" <td class='Content'>GrantID</td>");
sb.AppendLine(@" <td class='Content'>GrantDate</td>");
sb.AppendLine(@" <td class='Content'>Exercise Date(YYYY-MM-DD)</td>");
sb.AppendLine(@" <td class='Content'>Officer</td>");
sb.AppendLine(@" <td class='Content'>Employee ID</td>");
sb.AppendLine(@" <td class='Content'>Brokerage Account #</td>");
sb.AppendLine(@" <td class='Content'>English Name</td>");
sb.AppendLine(@" <td class='Content'>Shares Vested</td>");
sb.AppendLine(@" <td class='Content'>Shares Withheld for Taxes</td>");
sb.AppendLine(@" <td class='Content'>Withholding Tax" + taxCurrency + "</td>");
sb.AppendLine(@" <td class='Content'>Net Shares</td>");
sb.AppendLine(@" <td class='Content'>Option Cost" + currency + "</td>");
sb.AppendLine(@" </tr>");

if (!String.IsNullOrEmpty(orderDetail.GrantID))
{
sb.AppendLine(@" <td class='CvtTxt'>" + orderDetail.GrantID + "</td>");
}
else
{
sb.AppendLine(@" <td bgcolor='#B8CCE4' style='text-align: left;font-family: Times New Roman'></td>");
}

。。。

sb.AppendLine(@" </tr>");
sb.AppendLine(@" </table>");
sb.AppendLine(@" </div>");
sb.AppendLine(@" </body>");
sb.AppendLine(@"</html>");

bytes = ASCIIEncoding.UTF8.GetBytes(sb.ToString());

2. 下载时从DB中取出上面的bytes[], 然后扔给浏览器下载,代码如下:

string type = context.Request.QueryString["type"];

try
{
IFileDownloadHandler handler = GetHandler("xls");
if (handler == null)
{
throw new Exception("未知的文件类型");
}
string fileName;
byte[] fileContent;

//取出byte[]并给浏览器下载xls文件类型的文件

handler.GetFile(context, out fileName, out fileContent);

fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8);
fileName = fileName.Replace("+", "%20");
context.Response.Clear();
context.Response.Charset = "utf-8";
context.Response.Buffer = true;
context.Response.AddHeader("content-disposition",
string.Format("attachment;filename={0}", fileName));
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.ContentType = String.Format("application/octet-stream");

const int buffersize = 1024 * 16;

int count = fileContent.Length / buffersize;
int i;
for (i = 0; i < count; i++)
{
context.Response.OutputStream.Write(fileContent, i * buffersize, buffersize);
}
int remainder = fileContent.Length % buffersize;
if (remainder != 0)
{
context.Response.OutputStream.Write(fileContent, i * buffersize, remainder);
}

try
{
context.Response.End();
}
catch (ThreadAbortException ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
}
catch (Exception exp)
{
if (exp is ThreadAbortException)
{
System.Diagnostics.Debug.WriteLine(exp.Message);
}
else
{
context.Response.Write(exp.Message);
}
context.Response.End();
}

/// <summary>
/// 获取文件名及文件内容
/// </summary>
/// <param name="context">上下文</param>
/// <param name="fileName">文件名</param>
/// <param name="fileContent">文件内容</param>
public void GetFile(HttpContext context, out string fileName, out byte[] fileContent)
{
Guid fileID = new Guid(context.Request.QueryString["FileID"]);
IUploadFileService service = null;// ServiceFactory.FindService<IUploadFileService>();
try
{
// service = ServiceFactory.FindService<IUploadFileService>();
// var file = service.GetByUploadFileID(fileID);
UploadFileBizProcess bizObj = UploadFileBizProcess.GetInstance();
var file = bizObj.GetByUploadFileID(fileID);

if (file == null)
{
throw new Exception("文件不存在");
}
fileName = file.FileName;
fileContent = file.FileContent;
}
finally
{
Helper.Dispose(service);
}
}

在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)

html表格导出Excel的实例的更多相关文章

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

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

  2. 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; ...

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

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

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

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

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

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

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

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

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

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

  8. 使用NPOI或EPPlus来导出Excel文件实例,可在Excel文件加密

    使用NPOI.dll组件来导出Excel文件,并设置样式,Nuget引用即可. packages\NPOI.2.1.3.1\lib\net20\NPOI.dll #region Excel prote ...

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

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

随机推荐

  1. nginx编译安装之-./configure 参数详解

    参考官方文档 http://nginx.org/en/docs/configure.html --with开头的,默认是禁用的(没启动的,想使用的话需要在编译的时候加上) --without开头的,默 ...

  2. 新添加的磁盘大于2T 的分区方法

    环境CentOS7.1 2.9t磁盘 fdisk 只能分区小于2t的磁盘,大于2t的话,就要用到parted 1,将磁盘上原有的分区删除掉: 进入:#parted   /dev/sdb 查看:(par ...

  3. Apache的虚拟主机

    一.虚拟主机的分类 基于IP的虚拟主机:一台服务器,多个ip,搭建多个网站 基于端口的虚拟主机:一台服务器,一个ip,利用不同端口,搭建多个网站 基于域名的虚拟主机:一台服务器,一个ip,多个域名,搭 ...

  4. 一种使用gitlab的CI/CD功能实现Nginx配置更新的方法

    至于nginx的docker制作,前面已介绍过. 现在使用gitlab在线编辑的方式,可实现Nginx的自定义配置并更新. .gitlab-ci.yml内容如下: variables: project ...

  5. c#基础用法

    1.注释符 1)注销 2)解释 2.3种方式 1)单行注释 // 2)多行注释 /*要注释的内容*/ 3)文档注释 /// 多用来解释类或方法 3.数据类型 1)值类型 2)引用类型 1.对象 obj ...

  6. goto语句——慎用,但是可以用

    最近使用了goto语句,是因为if嵌套太深了,因此把错误处理同意了,直接使用goto语句. 举例: #include <stdio.h> int main () { /* local va ...

  7. native与H5优缺点及H5测试

    一.native(原生)与H5优缺点介绍 native(原生)优点 1.运行速度快 2.可以应用到底层的API 3.便捷性与易用性 4.打开会比较节省流量 native(原生)缺点 1.不同操作系统需 ...

  8. NOIP2018模板总结【数学】

    质因数分解 //质因数分解 int prime[MAXN], tim[MAXN], cnt; void Divide(int N) { printf("%d = ", N); fo ...

  9. spring boot 集成 redis lettuce(jedis)

    spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端 引入依赖 <!-- spring boot ...

  10. FasfDFS整合Java实现文件上传下载功能实例详解

    https://www.jb51.net/article/120675.htm 在上篇文章给大家介绍了FastDFS安装和配置整合Nginx-1.13.3的方法,大家可以点击查看下. 今天使用Java ...