html 表格 導出 excel 實例 欄目 HTML 简体版
原文   原文鏈接

1. 拼成出完整的HMTL的Table代碼片斷後,轉成二進制byte[]類型並存入數據庫中,供下載時調出來使用。css

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';}");html

//下面一行的代碼,是爲了解決超過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>");
}git

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>");
}瀏覽器

。。。app

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

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

2. 下載時從DB中取出上面的bytes[], 而後扔給瀏覽器下載,代碼以下:spa

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

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)

相關文章
相關標籤/搜索
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。
本站公眾號
   歡迎關注本站公眾號,獲取更多信息