html table export instance of Excel

1. Table makes up the complete code snippet of HMTL, converted into binary byte [] types and stored in the database, used for downloading tune out.

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

// the following line of code to solve the more than 11 digital plain text export Excel, Excel default will be converted into scientific notation, add the following class and marked after td fields to be displayed, can be forced to export Excel the numbers indicate the text
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 = left =' Center 'X: the 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. When the downloading is removed from above the DB bytes [], and then throw browser downloads the code is as follows:

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

the try
{
IFileDownloadHandler GetHandler Handler = ( "XLS");
IF (Handler == null)
{
the throw new new Exception ( "Unknown file type");
}
String fileName;
byte [] fileContent;

// Remove the byte [] to the browser and download the xls file type

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);
}
}

 

 

Adding the css: mso-number-format definition data format, a format for viewing a custom format, specifically refer in excel:
MSO-Number-the format: "0" NO Decimals 
MSO-Number-the format: "0 \. 000 ". 3 Decimals 
MSO-Number-the format:" \ # \, \ # \ # 0 \ .000 "Comma On Dec. 3 with 
MSO-Number-the format:" mm \ / dd \ / YY "Date7 
MSO-Number-the format: "the mmmm \ D \, \ YYYY" Date9 
MSO-Number-the format: "m \ / D \ / YY \ H \: mm \ AM \ / the PM" the AMPM -T D 
MSO-Number-the format: "Short a Date" 01 / 03/1998 
MSO-Number-the format: "Medium a Date" 01-Mar-98- 
MSO-Number-the format: "D \ -mmm \ -YYYY" 01-Mar-1998- 
MSO-Number-the format: "Short Time". 5 : 16 
MSO-Number-the format: "Medium Time" 5:16 AM 
MSO-Number-the format: "Long Time". 5: 16: 21 is: 00 
MSO-Number-the 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)

Guess you like

Origin www.cnblogs.com/itjeff/p/11401785.html