直接用Response输出可以加批注的Excel

不调用Excel对象模型,直接用Response输出可以加批注的Excel。

代码如下:

None.gif using  System;
None.gif
using  System.Text;
None.gif
using  System.Web;
None.gif
using  System.Web.UI;
None.gif
None.gif
namespace  WebTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// ExcelWithComment 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ResponseExcelWithComment
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 当前 HttpResponse
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static HttpResponse Response
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return HttpContext.Current.Response ;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用于构建整个网页内容的 StringBuilder
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private StringBuilder _htmlBuilder = new StringBuilder() ;
InBlock.gif        
private StringBuilder _contentBuilder = new StringBuilder() ;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 准备输出的Excel的文件名,不含扩展名
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private readonly string _fileName ;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Excel 作者
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private readonly string _authorName ;
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private ResponseExcelWithComment()dot.gif{}
InBlock.gif        
public ResponseExcelWithComment(string fileName, string authorName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (fileName == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ArgumentNullException("fileName") ;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (authorName == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ArgumentNullException("authorName") ;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            _fileName 
= fileName ;
InBlock.gif            _authorName 
= authorName ;
ExpandedSubBlockEnd.gif        }

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

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 为 Body 中的 Content添加行
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="line"></param>

InBlock.gif        public void AppendBodyContent(string line)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (line != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _contentBuilder.Append(line) ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            _contentBuilder.Append(
"\r\n") ;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 为 整个Html 添加一行内容
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="line"></param>

InBlock.gif        private void AppendLine(string line)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (line != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _htmlBuilder.Append(line) ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            _htmlBuilder.Append(
"\r\n") ;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void BuildHtml()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            AppendLine(
@"<html xmlns:v=""urn:schemas-microsoft-com:vml""
InBlock.gifxmlns:o=""urn:schemas-microsoft-com:office:office""
InBlock.gifxmlns:x=""urn:schemas-microsoft-com:office:excel""
InBlock.gifxmlns=""http://www.w3.org/TR/REC-html40"">
");
InBlock.gif
InBlock.gif            BuildHead();
InBlock.gif            BuildBody();
InBlock.gif
InBlock.gif            AppendLine(
"</html>");
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 写 <head></head> 部分
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void BuildHead()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            AppendLine(
"<head>");
InBlock.gif
InBlock.gif            BuildMeta();
InBlock.gif            BuildLink();
InBlock.gif            BuildCSS();
InBlock.gif            BuildJavascript();
InBlock.gif            BuildExcelProperties();
InBlock.gif
InBlock.gif            AppendLine((
"</head>"));
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 写 <body></body> 部分
ExpandedSubBlockEnd.gif        
/// </summary>

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

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Head Write Method#region Head Write Method
InBlock.gif
InBlock.gif        
private int _styleIndex = 30 ;
InBlock.gif        
private StringBuilder _styleBuilder = new StringBuilder() ;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 为单元格添加一种样式
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="bgColor">背景色</param>
InBlock.gif        
/// <param name="top">顶部是否闭合</param>
InBlock.gif        
/// <param name="bottom">底部是否闭合</param>
InBlock.gif        
/// <param name="left">左边是否闭合</param>
InBlock.gif        
/// <param name="right">右边</param>
InBlock.gif        
/// <param name="fontSize">文字大小</param>
InBlock.gif        
/// <param name="bold">是否为粗体</param>
ExpandedSubBlockEnd.gif        
/// <returns>css类名</returns>

InBlock.gif        public string AddCellStyle(System.Drawing.Color bgColor, bool top, bool bottom, bool left, bool right, int fontSize, bool bold)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _styleIndex
++ ;
InBlock.gif            
InBlock.gif            _styleBuilder.Append(
string.Format(@".xl{0}
InBlock.gif    {8}mso-style-parent:style0;    
InBlock.gif    mso-pattern:auto none;
InBlock.gif    border-top:{1};
InBlock.gif    border-right:{2};
InBlock.gif    border-bottom:{3};
InBlock.gif    border-left:{4};
InBlock.gif    font-size:{5}pt;
InBlock.gif    {6}
InBlock.gif    background:{7};{9}
",
InBlock.gif                _styleIndex,
InBlock.gif                top 
? ".5pt solid black" : "none",
InBlock.gif                right 
? ".5pt solid black" : "none",
InBlock.gif                bottom 
? ".5pt solid black" : "none",
InBlock.gif                left 
? ".5pt solid black" : "none",
InBlock.gif                fontSize,
InBlock.gif                bold 
? "font-weight:700;" : "",
InBlock.gif                bgColor.Name,
InBlock.gif                
"{",
InBlock.gif                
"}")) ;
InBlock.gif            _styleBuilder.Append(
"\r\n") ;
InBlock.gif
InBlock.gif            
return "xl" + _styleIndex.ToString() ;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 写 Meta 部分
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void BuildMeta()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            AppendLine(
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"") ;
InBlock.gif            AppendLine(
"<meta name=ProgId content=Excel.Sheet>") ;
InBlock.gif            AppendLine(
"<meta name=Generator content=\"Microsoft Excel 11\">") ;            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 写 Linked File
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void BuildLink()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            AppendLine(
"<link rel=File-List href=\"" + _fileName + ".files/filelist.xml\">") ;
InBlock.gif            AppendLine(
"<link rel=Edit-Time-Data href=\"" + _fileName + ".files/editdata.mso\">") ;
InBlock.gif            AppendLine(
"<link rel=OLE-Object-Data href=\"" + _fileName + ".files/oledata.mso\">") ;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void BuildCSS()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string css = @"
InBlock.gif            <!--[if !mso]>
InBlock.gif<style>
InBlock.gifv\:* {behavior:url(#default#VML);}
InBlock.gifo\:* {behavior:url(#default#VML);}
InBlock.gifx\:* {behavior:url(#default#VML);}
InBlock.gif.shape {behavior:url(#default#VML);}
InBlock.gif</style>
InBlock.gif<![endif]--><!--[if gte mso 9]><xml>
InBlock.gif <o:DocumentProperties>
InBlock.gif  <o:LastAuthor>
" + _authorName + @"</o:LastAuthor>
InBlock.gif  <o:LastSaved>
" + DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddThh:mm:ssZ"+ @"</o:LastSaved>
InBlock.gif  <o:Version>11.8107</o:Version>
InBlock.gif </o:DocumentProperties>
InBlock.gif</xml><![endif]-->
InBlock.gif<style>
InBlock.gif<!--table
InBlock.gif    {mso-displayed-decimal-separator:""\."";
InBlock.gif    mso-displayed-thousand-separator:""\,"";}
InBlock.gif@page
InBlock.gif    {margin:1.0in .75in 1.0in .75in;
InBlock.gif    mso-header-margin:.5in;
InBlock.gif    mso-footer-margin:.5in;}
InBlock.gif.font6
InBlock.gif    {color:black;
InBlock.gif    font-size:9.0pt;
InBlock.gif    font-weight:700;
InBlock.gif    font-style:normal;
InBlock.gif    text-decoration:none;
InBlock.gif    font-family:SimSun;
InBlock.gif    mso-generic-font-family:auto;
InBlock.gif    mso-font-charset:134;}
InBlock.gif.font7
InBlock.gif    {color:black;
InBlock.gif    font-size:9.0pt;
InBlock.gif    font-weight:400;
InBlock.gif    font-style:normal;
InBlock.gif    text-decoration:none;
InBlock.gif    font-family:SimSun;
InBlock.gif    mso-generic-font-family:auto;
InBlock.gif    mso-font-charset:134;}
InBlock.gif.font8
InBlock.gif    {color:black;
InBlock.gif    font-size:9.0pt;
InBlock.gif    font-weight:400;
InBlock.gif    font-style:normal;
InBlock.gif    text-decoration:none;
InBlock.gif    font-family:SimSun;
InBlock.gif    mso-generic-font-family:auto;
InBlock.gif    mso-font-charset:134;}
InBlock.gif.font9
InBlock.gif    {color:black;
InBlock.gif    font-size:9.0pt;
InBlock.gif    font-weight:700;
InBlock.gif    font-style:normal;
InBlock.gif    text-decoration:none;
InBlock.gif    font-family:SimSun;
InBlock.gif    mso-generic-font-family:auto;
InBlock.gif    mso-font-charset:134;}
InBlock.giftr
InBlock.gif    {mso-height-source:auto;
InBlock.gif    mso-ruby-visibility:none;}
InBlock.gifcol
InBlock.gif    {mso-width-source:auto;
InBlock.gif    mso-ruby-visibility:none;}
InBlock.gifbr
InBlock.gif    {mso-data-placement:same-cell;}
InBlock.gif.style0
InBlock.gif    {mso-number-format:General;
InBlock.gif    text-align:general;
InBlock.gif    vertical-align:middle;
InBlock.gif    white-space:nowrap;
InBlock.gif    mso-rotate:0;
InBlock.gif    mso-background-source:auto;
InBlock.gif    mso-pattern:auto;
InBlock.gif    color:windowtext;
InBlock.gif    font-size:12.0pt;
InBlock.gif    font-weight:400;
InBlock.gif    font-style:normal;
InBlock.gif    text-decoration:none;
InBlock.gif    font-family:SimSun;
InBlock.gif    mso-generic-font-family:auto;
InBlock.gif    mso-font-charset:134;
InBlock.gif    border:none;
InBlock.gif    mso-protection:locked visible;
InBlock.gif    mso-style-name:\5E38\89C4;
InBlock.gif    mso-style-id:0;}
InBlock.giftd
InBlock.gif    {mso-style-parent:style0;
InBlock.gif    padding:0px;
InBlock.gif    mso-ignore:padding;
InBlock.gif    color:windowtext;
InBlock.gif    font-size:12.0pt;
InBlock.gif    font-weight:400;
InBlock.gif    font-style:normal;
InBlock.gif    text-decoration:none;
InBlock.gif    font-family:SimSun;
InBlock.gif    mso-generic-font-family:auto;
InBlock.gif    mso-font-charset:134;
InBlock.gif    mso-number-format:General;
InBlock.gif    text-align:general;
InBlock.gif    vertical-align:middle;
InBlock.gif    border:none;
InBlock.gif    mso-background-source:auto;
InBlock.gif    mso-pattern:auto;
InBlock.gif    mso-protection:locked visible;
InBlock.gif    white-space:nowrap;
InBlock.gif    mso-rotate:0;}
InBlock.gif.xl24
InBlock.gif    {mso-style-parent:style0;
InBlock.gif    white-space:normal;}
InBlock.gif
" + _styleBuilder.ToString() 
InBlock.gif
+ @"
InBlock.gifruby
InBlock.gif    {ruby-align:left;}
InBlock.gifrt
InBlock.gif    {color:windowtext;
InBlock.gif    font-size:9.0pt;
InBlock.gif    font-weight:400;
InBlock.gif    font-style:normal;
InBlock.gif    text-decoration:none;
InBlock.gif    font-family:SimSun;
InBlock.gif    mso-generic-font-family:auto;
InBlock.gif    mso-font-charset:134;
InBlock.gif    mso-char-type:none;
InBlock.gif    display:none;}
InBlock.gif-->
InBlock.gif</style>
" ;
InBlock.gif            AppendLine(css) ;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void BuildJavascript()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            AppendLine(
@"<![if !supportAnnotations]><style id=""dynCom"" type=""text/css""><!-- --></style>
InBlock.gif
InBlock.gif<script language=""JavaScript""><!--
InBlock.gif
InBlock.giffunction msoCommentShow(com_id,anchor_id) {
InBlock.gif    if(msoBrowserCheck()) {
InBlock.gif       c = document.all(com_id);
InBlock.gif       a = document.all(anchor_id);
InBlock.gif       if (null != c) {
InBlock.gif        var cw = c.offsetWidth;
InBlock.gif        var ch = c.offsetHeight;
InBlock.gif        var aw = a.offsetWidth;
InBlock.gif        var ah = a.offsetHeight;
InBlock.gif        var x = a.offsetLeft;
InBlock.gif        var y = a.offsetTop;
InBlock.gif        var el = a;
InBlock.gif        while (el.tagName != ""BODY"") {
InBlock.gif           el = el.offsetParent;
InBlock.gif           x = x + el.offsetLeft;
InBlock.gif           y = y + el.offsetTop;
InBlock.gif           }        
InBlock.gif        var bw = document.body.clientWidth;
InBlock.gif        var bh = document.body.clientHeight;
InBlock.gif        var bsl = document.body.scrollLeft;
InBlock.gif        var bst = document.body.scrollTop;
InBlock.gif        if (x + cw + ah/2 > bw + bsl && x + aw - ah/2 - cw >= bsl ) {
InBlock.gif           c.style.left = x + aw - ah / 2 - cw; 
InBlock.gif        }
InBlock.gif        else {
InBlock.gif           c.style.left = x + ah/2; 
InBlock.gif        }
InBlock.gif        if (y + ch + ah/2 > bh + bst && y + ah/2 - ch >= bst ) {
InBlock.gif            c.style.top = y + ah/2 - ch;
InBlock.gif        } 
InBlock.gif        else {
InBlock.gif           c.style.top = y + ah/2;
InBlock.gif        }
InBlock.gif        c.style.visibility = ""visible"";
InBlock.gif       }
InBlock.gif    }
InBlock.gif}
InBlock.gif
InBlock.giffunction msoCommentHide(com_id) {
InBlock.gif    if(msoBrowserCheck()) {
InBlock.gif      c = document.all(com_id)
InBlock.gif      if (null != c) {
InBlock.gif        c.style.visibility = ""hidden"";
InBlock.gif        c.style.left = ""-10000"";
InBlock.gif        c.style.top = ""-10000"";
InBlock.gif      }
InBlock.gif    }
InBlock.gif}
InBlock.gif
InBlock.giffunction msoBrowserCheck() {
InBlock.gif ms=navigator.appVersion.indexOf(""MSIE"");
InBlock.gif vers = navigator.appVersion.substring(ms+5, ms+6);
InBlock.gif ie4 = (ms>0) && (parseInt(vers) >=4);
InBlock.gif return ie4
InBlock.gif}
InBlock.gif
InBlock.gifif (msoBrowserCheck()) {
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomspan1"",""position:absolute"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomspan2"",""position:absolute"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomspan2"",""left:-1.5ex"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomspan2"",""width:2ex"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomspan2"",""height:0.5em"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomanch"",""font-size:0.5em"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomanch"",""color:red"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomhide"",""display: none"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomtxt"",""visibility: hidden"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomtxt"",""position: absolute"");        
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomtxt"",""top:-10000"");         
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomtxt"",""left:-10000"");         
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomtxt"",""width: 33%"");                 
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomtxt"",""background: infobackground"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomtxt"",""color: infotext"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomtxt"",""border-top: 1pt solid threedlightshadow"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomtxt"",""border-right: 2pt solid threedshadow"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomtxt"",""border-bottom: 2pt solid threedshadow"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomtxt"",""border-left: 1pt solid threedlightshadow"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomtxt"",""padding: 3pt 3pt 3pt 3pt"");
InBlock.gifdocument.styleSheets.dynCom.addRule("".msocomtxt"",""z-index: 100"");
InBlock.gif}
InBlock.gif
InBlock.gif// -->
InBlock.gif</script>
InBlock.gif<![endif]>
") ;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void BuildExcelProperties()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            AppendLine(
string.Format(@"<!--[if gte mso 9]><xml>
InBlock.gif <x:ExcelWorkbook>
InBlock.gif  <x:ExcelWorksheets>
InBlock.gif   <x:ExcelWorksheet>
InBlock.gif    <x:Name>{0}</x:Name>
InBlock.gif    <x:WorksheetOptions>
InBlock.gif     <x:DefaultRowHeight>285</x:DefaultRowHeight>
InBlock.gif     <x:Selected/>
InBlock.gif     <x:DoNotDisplayGridlines/>
InBlock.gif     <x:Panes/>           
InBlock.gif     <x:ProtectContents>False</x:ProtectContents>
InBlock.gif     <x:ProtectObjects>False</x:ProtectObjects>
InBlock.gif     <x:ProtectScenarios>False</x:ProtectScenarios>
InBlock.gif    </x:WorksheetOptions>
InBlock.gif   </x:ExcelWorksheet>
InBlock.gif  </x:ExcelWorksheets>
InBlock.gif  <x:WindowHeight>8550</x:WindowHeight>
InBlock.gif  <x:WindowWidth>14940</x:WindowWidth>
InBlock.gif  <x:WindowTopX>240</x:WindowTopX>
InBlock.gif  <x:WindowTopY>45</x:WindowTopY>
InBlock.gif  <x:ProtectStructure>False</x:ProtectStructure>
InBlock.gif  <x:ProtectWindows>False</x:ProtectWindows>
InBlock.gif </x:ExcelWorkbook>
InBlock.gif</xml><![endif]--><!--[if gte mso 9]><xml>
InBlock.gif <o:shapedefaults v:ext=""edit"" spidmax=""1027""/>
InBlock.gif</xml><![endif]--><!--[if gte mso 9]><xml>
InBlock.gif <o:shapelayout v:ext=""edit"">
InBlock.gif  <o:idmap v:ext=""edit"" data=""1""/>
InBlock.gif </o:shapelayout></xml><![endif]-->
",
InBlock.gif                _fileName));
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
About Comment#region About Comment
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 批注的 Builder
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        StringBuilder _commentBuilder = new StringBuilder() ;
InBlock.gif
InBlock.gif        
int curIndex = 0 ;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Shape Type
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        const string SHAPE_TYPE = @"<v:shapetype id=""_x0000_t202"" coordsize=""21600,21600"" o:spt=""202"" path=""m,l,21600r21600,l21600,xe"">
InBlock.gif  <v:stroke joinstyle=""miter""/>
InBlock.gif  <v:path gradientshapeok=""t"" o:connecttype=""rect""/>
InBlock.gif </v:shapetype>
" ;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 添加批注
InBlock.gif        
/// </summary>        
InBlock.gif        
/// <param name="row">被批注单元格从0开始所在的行索引</param>
InBlock.gif        
/// <param name="column">被批注单元格从0开始所在的列索引</param>
InBlock.gif        
/// <param name="text">单元格内容</param>
InBlock.gif        
/// <param name="comment">批注内容</param>
ExpandedSubBlockEnd.gif        
/// <returns>增加了批注后的单元格内容</returns>

InBlock.gif        public string AddComment(int row, int column, string text, string comment)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (row < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ArgumentOutOfRangeException("row") ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (column < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ArgumentOutOfRangeException("column") ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (text == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ArgumentNullException("text") ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (comment == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ArgumentNullException("comment") ;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            curIndex
++ ;
InBlock.gif
InBlock.gif            _commentBuilder.Append(
string.Format(@"
InBlock.gif
InBlock.gif            <div style='mso-element:comment'><![if !supportAnnotations]>
InBlock.gif
InBlock.gif<div id=""_com_{0}"" class=msocomtxt
InBlock.gifοnmοuseοver=""msoCommentShow('_com_{0}','_anchor_{0}')""
InBlock.gifοnmοuseοut=""msoCommentHide('_com_{0}')"" language=JavaScript><![endif]>
InBlock.gif
InBlock.gif<div><![if !supportAnnotations]><a class=msocomhide href=""#_msoanchor_{0}""
InBlock.gifname=""_msocom_{0}"">[{0}]</a><![endif]><!--[if gte mso 9]><xml>
InBlock.gif {1}<v:shape id=""_x0000_s102{0}"" type=""#_x0000_t202"" style='position:absolute;
InBlock.gif  margin-left:87.75pt;margin-top:-12.75pt;width:96pt;height:59.25pt;z-index:1;
InBlock.gif  visibility:hidden' fillcolor=""infoBackground [80]"" o:insetmode=""auto"">
InBlock.gif  <v:fill color2=""infoBackground [80]""/>
InBlock.gif  <v:shadow on=""t"" color=""black"" obscured=""t""/>
InBlock.gif  <v:path o:connecttype=""none""/>
InBlock.gif  <v:textbox style='mso-direction-alt:auto'/>
InBlock.gif  <x:ClientData ObjectType=""Note"">
InBlock.gif   <x:MoveWithCells/>
InBlock.gif   <x:SizeWithCells/>
InBlock.gif   <x:AutoFill>False</x:AutoFill>
InBlock.gif   <x:Row>{2}</x:Row>
InBlock.gif   <x:Column>{3}</x:Column>
InBlock.gif   <x:Author>{4}</x:Author>
InBlock.gif  </x:ClientData>
InBlock.gif </v:shape></xml><![endif]--><![if !vml]><span style='mso-ignore:vglayout'><![endif]>
InBlock.gif
InBlock.gif<div v:shape=""_x0000_s102{0}"" style='padding:.75pt 0pt 0pt .75pt;text-align:left'
InBlock.gifclass=shape><font class=""font6"">{4}:</font><font class=""font7""><br>
InBlock.gif{5}</font></div>
InBlock.gif
InBlock.gif<![if !vml]></span><![endif]></div>
InBlock.gif
InBlock.gif<![if !supportAnnotations]></div>
InBlock.gif
InBlock.gif<![endif]></div>
",
InBlock.gif                curIndex,
InBlock.gif                (curIndex 
== 1 ? SHAPE_TYPE : ""),
InBlock.gif                row,
InBlock.gif                column,
InBlock.gif                _authorName,
InBlock.gif                comment)) ;
InBlock.gif
InBlock.gif            
return string.Format(@"{1}<![if !supportAnnotations]><span
InBlock.gif  class=msocomspan1><span class=msocomspan2 id=""_anchor_{0}""
InBlock.gif  οnmοuseοver=""msoCommentShow('_com_{0}','_anchor_{0}')""
InBlock.gif  οnmοuseοut=""msoCommentHide('_com_{0}')"" language=JavaScript><a
InBlock.gif  class=msocomanch href=""#_msocom_{0}"" name=""_msoanchor_{0}"">[1]</a></span></span><![endif]>
",
InBlock.gif                curIndex,
InBlock.gif                text) ;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


示例:
None.gif private   void  Button1_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string fileName = "Crude_Data" ;
InBlock.gif            
string authorName = "Author Name" ;
InBlock.gif            ResponseExcelWithComment excel 
= new ResponseExcelWithComment(fileName, authorName) ;
InBlock.gif
InBlock.gif            sqlConnection1.Open() ;
InBlock.gif            dataSet11 
= new DataSet1() ;
InBlock.gif            sqlDataAdapter1.Fill(dataSet11.UserInformation) ;
InBlock.gif            sqlConnection1.Close() ;
InBlock.gif
InBlock.gif            
int curRow = 0 ;
InBlock.gif            
int curCol = 0 ;
InBlock.gif            
string style1 = "" ;
InBlock.gif
InBlock.gif            StringBuilder tableBuilder 
= new StringBuilder() ;
InBlock.gif            tableBuilder.Append(
@"<table>") ;
InBlock.gif            tableBuilder.Append(
"<tr>") ;
InBlock.gif
InBlock.gif            style1 
= excel.AddCellStyle(Color.Blue, truetruetruetrue9true) ;
InBlock.gif            tableBuilder.Append(
string.Format("<td class={0}>", style1)) ;
InBlock.gif            tableBuilder.Append(excel.AddComment(curRow, curCol, 
"User Name""用户名")) ;
InBlock.gif            tableBuilder.Append(
"</td>") ;
InBlock.gif
InBlock.gif            tableBuilder.Append(
string.Format("<td class={0}>", style1)) ;
InBlock.gif            curCol
++ ;
InBlock.gif            tableBuilder.Append(excel.AddComment(curRow, curCol, 
"Password""密码")) ;
InBlock.gif            tableBuilder.Append(
"</td>") ;
InBlock.gif
InBlock.gif            tableBuilder.Append(
string.Format("<td class={0}>", style1)) ;
InBlock.gif            curCol
++ ;
InBlock.gif            tableBuilder.Append(excel.AddComment(curRow, curCol, 
"Email""电子邮件")) ;
InBlock.gif            tableBuilder.Append(
"</td>") ;
InBlock.gif            
InBlock.gif            tableBuilder.Append(
"</tr>") ;
InBlock.gif
InBlock.gif            
string style2 = excel.AddCellStyle(Color.Yellow, truetruefalsefalse9false) ;
InBlock.gif            
foreach (DataSet1.UserInformationRow userRow in dataSet11.UserInformation)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                curRow
++ ;
InBlock.gif                curCol 
= 0 ;
InBlock.gif                tableBuilder.Append(
string.Format("<td class={0}>", style2)) ;
InBlock.gif                tableBuilder.Append(excel.AddComment(curRow, curCol, userRow.UserName, userRow.UserName)) ;
InBlock.gif                tableBuilder.Append(
"</td>") ;
InBlock.gif
InBlock.gif                tableBuilder.Append(
string.Format("<td class={0}>", style2)) ;
InBlock.gif                curCol
++ ;
InBlock.gif                tableBuilder.Append(excel.AddComment(curRow, curCol, userRow.Password, userRow.Password)) ;
InBlock.gif                tableBuilder.Append(
"</td>") ;
InBlock.gif
InBlock.gif                tableBuilder.Append(
string.Format("<td class={0}>", style2)) ;
InBlock.gif                curCol
++ ;
InBlock.gif                tableBuilder.Append(excel.AddComment(curRow, curCol, userRow.Email, userRow.Email)) ;
InBlock.gif                tableBuilder.Append(
"</td>") ;
InBlock.gif            
InBlock.gif                tableBuilder.Append(
"</tr>") ;                
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            tableBuilder.Append(
@"</table>") ;
InBlock.gif
InBlock.gif            excel.AppendBodyContent(tableBuilder.ToString()) ;
InBlock.gif            excel.WriteResponse() ;
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/karoc/archive/2006/12/13/591472.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值