asp学习网: 首页 >> asp.net >> Response输出可以加批注的Excel

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