惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Jina AI
Jina AI
H
Help Net Security
月光博客
月光博客
Y
Y Combinator Blog
I
InfoQ
博客园 - Franky
W
WeLiveSecurity
The Register - Security
The Register - Security
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Blog — PlanetScale
Blog — PlanetScale
H
Hacker News: Front Page
L
LangChain Blog
S
Security @ Cisco Blogs
Hacker News - Newest:
Hacker News - Newest: "LLM"
罗磊的独立博客
V2EX - 技术
V2EX - 技术
H
Heimdal Security Blog
博客园_首页
SecWiki News
SecWiki News
N
News and Events Feed by Topic
B
Blog RSS Feed
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
AI
AI
Google Online Security Blog
Google Online Security Blog
MongoDB | Blog
MongoDB | Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
WordPress大学
WordPress大学
小众软件
小众软件
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
PCI Perspectives
PCI Perspectives
博客园 - 聂微东
N
News | PayPal Newsroom
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Martin Fowler
Martin Fowler
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
博客园 - 司徒正美
Forbes - Security
Forbes - Security
酷 壳 – CoolShell
酷 壳 – CoolShell
S
Security Affairs

博客园 - karoc

《重构-改善既有代码的设计》读书笔记 扩展GridView,增加单选按钮列 任务开始时间和完成时间 最大长度验证控件:MaxLengthValidator 又是关于AjaxControlToolkit的ModalPopup的问题 有个总结 使用svn的小插曲 开发小经验总结(不断更新) 今天发现一个VS2008中文版和英文版的差别 解决DocType引起AjaxTookit的ModulPopup显示异常问题的方法 Reporting Services 2005 常见问题解决方法(不断更新) 用VSTO开发Project插件心得 自定义MemberShipProvider和PersonalizationProvider使用WebParts实现个性化页面 在线修改KeyValue配置节 瞎搞八搞,另类PageBase 改造Duwamish中的Configuration C#控制Windows Messenger和Windows Live Messenger窗口发送消息 一个读取扩展名为xml的资源文件的方法 用C#+WMI实现获取w3wp进程对应的应用程序池
直接用Response输出可以加批注的Excel
karoc · 2006-12-13 · via 博客园 - karoc

不调用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 

        
About Comment
    }

}

示例:

private void Button1_Click(object sender, System.EventArgs e)
        
{
            
string fileName = "Crude_Data" ;
            
string authorName = "Author Name" ;
            ResponseExcelWithComment excel 
= new ResponseExcelWithComment(fileName, authorName) ;

            sqlConnection1.Open() ;
            dataSet11 
= new DataSet1() ;
            sqlDataAdapter1.Fill(dataSet11.UserInformation) ;
            sqlConnection1.Close() ;

            
int curRow = 0 ;
            
int curCol = 0 ;
            
string style1 = "" ;

            StringBuilder tableBuilder 
= new StringBuilder() ;
            tableBuilder.Append(
@"<table>") ;
            tableBuilder.Append(
"<tr>") ;

            style1 
= excel.AddCellStyle(Color.Blue, truetruetruetrue9true) ;
            tableBuilder.Append(
string.Format("<td class={0}>", style1)) ;
            tableBuilder.Append(excel.AddComment(curRow, curCol, 
"User Name""用户名")) ;
            tableBuilder.Append(
"</td>") ;

            tableBuilder.Append(
string.Format("<td class={0}>", style1)) ;
            curCol
++ ;
            tableBuilder.Append(excel.AddComment(curRow, curCol, 
"Password""密码")) ;
            tableBuilder.Append(
"</td>") ;

            tableBuilder.Append(
string.Format("<td class={0}>", style1)) ;
            curCol
++ ;
            tableBuilder.Append(excel.AddComment(curRow, curCol, 
"Email""电子邮件")) ;
            tableBuilder.Append(
"</td>") ;
            
            tableBuilder.Append(
"</tr>") ;

            
string style2 = excel.AddCellStyle(Color.Yellow, truetruefalsefalse9false) ;
            
foreach (DataSet1.UserInformationRow userRow in dataSet11.UserInformation)
            
{
                curRow
++ ;
                curCol 
= 0 ;
                tableBuilder.Append(
string.Format("<td class={0}>", style2)) ;
                tableBuilder.Append(excel.AddComment(curRow, curCol, userRow.UserName, userRow.UserName)) ;
                tableBuilder.Append(
"</td>") ;

                tableBuilder.Append(
string.Format("<td class={0}>", style2)) ;
                curCol
++ ;
                tableBuilder.Append(excel.AddComment(curRow, curCol, userRow.Password, userRow.Password)) ;
                tableBuilder.Append(
"</td>") ;

                tableBuilder.Append(
string.Format("<td class={0}>", style2)) ;
                curCol
++ ;
                tableBuilder.Append(excel.AddComment(curRow, curCol, userRow.Email, userRow.Email)) ;
                tableBuilder.Append(
"</td>") ;
            
                tableBuilder.Append(
"</tr>") ;                
            }


            tableBuilder.Append(
@"</table>") ;

            excel.AppendBodyContent(tableBuilder.ToString()) ;
            excel.WriteResponse() ;
        }