Response输出可以加批注的Excel
不调用Excel对象模型,直接用Response输出可以加批注的Excel。
代码如下:
using System;
using System.Text;
using System.Web;
using System.Web.UI;
namespace WebTest

{
/**//// <summary>
/// ExcelWithComment 的摘要说明。
/// </summary>
public class ResponseExcelWithComment
{
/**//// <summary>
/// 当前 HttpResponse
/// </summary>
private static HttpResponse Response
{
get
{
return HttpContext.Current.Response ;
}
}

/**//// <summary>
/// 用于构建整个网页内容的 StringBuilder
/// </summary>
private StringBuilder _htmlBuilder = new StringBuilder() ;
private StringBuilder _contentBuilder = new StringBuilder() ;

/**//// <summary>
/// 准备输出的Excel的文件名,不含扩展名
/// </summary>
private readonly string _fileName ;
/**//// <summary>
/// Excel 作者
/// </summary>
private readonly string _authorName ;

private ResponseExcelWithComment()
{}
public ResponseExcelWithComment(string fileName, string authorName)
{
if (fileName == null)
{
throw new ArgumentNullException("fileName") ;
}
if (authorName == null)
{
throw new ArgumentNullException("authorName") ;
}
_fileName = fileName ;
_authorName = authorName ;
}

public void WriteResponse()
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition","attachment;filename=" + _fileName + ".xls");
Response.ContentEncoding = Encoding.Default ;
BuildHtml();
Response.Write(_htmlBuilder.ToString()) ;
Response.Flush() ;
Response.End() ;
}

/**//// <summary>
/// 为 Body 中的 Content添加行
/// </summary>
/// <param name="line"></param>
public void AppendBodyContent(string line)
{
if (line != null)
{
_contentBuilder.Append(line) ;
}
_contentBuilder.Append("\r\n") ;
}

/**//// <summary>
/// 为 整个Html 添加一行内容
/// </summary>
/// <param name="line"></param>
private void AppendLine(string line)
{
if (line != null)
{
_htmlBuilder.Append(line) ;
}
_htmlBuilder.Append("\r\n") ;
}
private void BuildHtml()
{
AppendLine(@"<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"">");
BuildHead();
BuildBody();
AppendLine("</html>");
}

/**//// <summary>
/// 写 <head></head> 部分
/// </summary>
private void BuildHead()
{
AppendLine("<head>");
BuildMeta();
BuildLink();
BuildCSS();
BuildJavascript();
BuildExcelProperties();
AppendLine(("</head>"));
}

/**//// <summary>
/// 写 <body></body> 部分
/// </summary>
private void BuildBody()
{
AppendLine("<body link=blue vlink=purple>");
AppendLine(_contentBuilder.ToString());
//comment list
AppendLine(@"<div style='mso-element:comment-list'><![if !supportAnnotations]>
<hr class=msocomhide align=left size=1 width=""33%"">
<![endif]>");
AppendLine(_commentBuilder.ToString());
AppendLine("</div>");

AppendLine("</body>");
}

Head Write Method#region Head Write Method
private int _styleIndex = 30 ;
private StringBuilder _styleBuilder = new StringBuilder() ;

/**//// <summary>
/// 为单元格添加一种样式
/// </summary>
/// <param name="bgColor">背景色</param>
/// <param name="top">顶部是否闭合</param>
/// <param name="bottom">底部是否闭合</param>
/// <param name="left">左边是否闭合</param>
/// <param name="right">右边</param>
/// <param name="fontSize">文字大小</param>
/// <param name="bold">是否为粗体</param>
/// <returns>css类名</returns>
public string AddCellStyle(System.Drawing.Color bgColor, bool top, bool bottom, bool left, bool right, int fontSize, bool bold)
{
_styleIndex++ ;
_styleBuilder.Append(string.Format(@".xl{0}
{8}mso-style-parent:style0;
mso-pattern:auto none;
border-top:{1};
border-right:{2};
border-bottom:{3};
border-left:{4};
font-size:{5}pt;
{6}
background:{7};{9}",
_styleIndex,
top ? ".5pt solid black" : "none",
right ? ".5pt solid black" : "none",
bottom ? ".5pt solid black" : "none",
left ? ".5pt solid black" : "none",
fontSize,
bold ? "font-weight:700;" : "",
bgColor.Name,
"{",
"}")) ;
_styleBuilder.Append("\r\n") ;
return "xl" + _styleIndex.ToString() ;
}

/**//// <summary>
/// 写 Meta 部分
/// </summary>
private void BuildMeta()
{
AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"") ;
AppendLine("<meta name=ProgId content=Excel.Sheet>") ;
AppendLine("<meta name=Generator content=\"Microsoft Excel 11\">") ;
}

/**//// <summary>
/// 写 Linked File
/// </summary>
private void BuildLink()
{
AppendLine("<link rel=File-List href=\"" + _fileName + ".files/filelist.xml\">") ;
AppendLine("<link rel=Edit-Time-Data href=\"" + _fileName + ".files/editdata.mso\">") ;
AppendLine("<link rel=OLE-Object-Data href=\"" + _fileName + ".files/oledata.mso\">") ;
}
private void BuildCSS()
{
string css = @"
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
x\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]--><!--[if gte mso 9]><xml>
<o:DocumentProperties>
<o:LastAuthor>" + _authorName + @"</o:LastAuthor>
<o:LastSaved>" + DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddThh:mm:ssZ") + @"</o:LastSaved>
<o:Version>11.8107</o:Version>
</o:DocumentProperties>
</xml><![endif]-->
<style>
<!--table
{mso-displayed-decimal-separator:""\."";
mso-displayed-thousand-separator:""\,"";}
@page
{margin:1.0in .75in 1.0in .75in;
mso-header-margin:.5in;
mso-footer-margin:.5in;}
.font6
{color:black;
font-size:9.0pt;
font-weight:700;
font-style:normal;
text-decoration:none;
font-family:SimSun;
mso-generic-font-family:auto;
mso-font-charset:134;}
.font7
{color:black;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:SimSun;
mso-generic-font-family:auto;
mso-font-charset:134;}
.font8
{color:black;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:SimSun;
mso-generic-font-family:auto;
mso-font-charset:134;}
.font9
{color:black;
font-size:9.0pt;
font-weight:700;
font-style:normal;
text-decoration:none;
font-family:SimSun;
mso-generic-font-family:auto;
mso-font-charset:134;}
tr
{mso-height-source:auto;
mso-ruby-visibility:none;}
col
{mso-width-source:auto;
mso-ruby-visibility:none;}
br
{mso-data-placement:same-cell;}
.style0
{mso-number-format:General;
text-align:general;
vertical-align:middle;
white-space:nowrap;
mso-rotate:0;
mso-background-source:auto;
mso-pattern:auto;
color:windowtext;
font-size:12.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:SimSun;
mso-generic-font-family:auto;
mso-font-charset:134;
border:none;
mso-protection:locked visible;
mso-style-name:\5E38\89C4;
mso-style-id:0;}
td
{mso-style-parent:style0;
padding:0px;
mso-ignore:padding;
color:windowtext;
font-size:12.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:SimSun;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-number-format:General;
text-align:general;
vertical-align:middle;
border:none;
mso-background-source:auto;
mso-pattern:auto;
mso-protection:locked visible;
white-space:nowrap;
mso-rotate:0;}
.xl24
{mso-style-parent:style0;
white-space:normal;}
" + _styleBuilder.ToString()
+ @"
ruby
{ruby-align:left;}
rt
{color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:SimSun;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-char-type:none;
display:none;}
-->
</style>" ;
AppendLine(css) ;
}
