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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - karoc

《重构-改善既有代码的设计》读书笔记 扩展GridView,增加单选按钮列 任务开始时间和完成时间 最大长度验证控件:MaxLengthValidator 又是关于AjaxControlToolkit的ModalPopup的问题 有个总结 使用svn的小插曲 开发小经验总结(不断更新) 今天发现一个VS2008中文版和英文版的差别 解决DocType引起AjaxTookit的ModulPopup显示异常问题的方法 Reporting Services 2005 常见问题解决方法(不断更新) - karoc 用VSTO开发Project插件心得 自定义MemberShipProvider和PersonalizationProvider使用WebParts实现个性化页面 - karoc 在线修改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() ;
        }