All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Visual C++ 6.0教程:c++数据类型之结构
asp.net控件用法--在Repeater中嵌套使用Repeater_[Asp.Net教程]
C#中WebBrower控件应用实例
HTML 初学者指南(9)_[Html教程]
SQL,SERVER中字段类型及说明_mssql学习_编程技术
存储过程从入门到熟练(多个存储过程完整实例及调用方法)_mssql学习_编程技术
ASP.NET,2.0移动开发之属性重写和模板化_.net资料_编程技术
保护,SQL,Server,数据库的十大绝招_[SQL,Server教程]
如何最大限度提高.NET的性能,(续)_.net资料_编程技术
使用ASP.NET,2.0,GridView轻松操作数据_[Asp.Net教程]
Reading number is top 10 pictures
中国文革时期的色情图片2
In 2013 hercules Arnold classic2
美洲杯宝贝的雨中风情2
重口味人造肉
非常漂亮的泳装美女
A beautiful girl to bud1
云南大理的美女
全球十大灵异酒店
Fury xp desktop theme
Japan sexy beauty passion photo
Download software ranking
Unix video tutorial11
Tram sex maniac 2 (H) rar bag7
仙剑奇侠传98版歌曲
C++教程第四版
美女写真1
Be there or be square
终极变速大师Speeder3.26
Boxer vs Yellow5
jdk1.6 for windows
金山office2007
delv published in(发表于) 2014/1/23 3:13:03 Edit(编辑)
Response输出可以加批注的Excel_[Asp.Net教程]

Response输出可以加批注的Excel_[Asp.Net教程]

Response输出可以加批注的Excel_[Asp.Net教程]























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




代码如下:





using System;
using System.Text;
using System.Web;
using System.Web.UI;




namespace WebTest
{
/**////


/// ExcelWithComment 的摘要说明。
///

public class ResponseExcelWithComment
{
/**////
/// 当前 HttpResponse
///

private static HttpResponse Response
{
get
{
return HttpContext.Current.Response ;
}
}




/**////


/// 用于构建整个网页内容的 StringBuilder
///

private StringBuilder _htmlBuilder = new StringBuilder() ;
private StringBuilder _contentBuilder = new StringBuilder() ;




/**////


/// 准备输出的Excel的文件名,不含扩展名
///

private readonly string _fileName ;
/**////
/// Excel 作者
///

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() ;
}




/**////


/// 为 Body 中的 Content添加行
///

///
public void AppendBodyContent(string line)
{
if (line != null)
{
_contentBuilder.Append(line) ;
}
_contentBuilder.Append("\r\n") ;
}




/**////


/// 为 整个Html 添加一行内容
///

///
private void AppendLine(string line)
{
if (line != null)
{
_htmlBuilder.Append(line) ;
}
_htmlBuilder.Append("\r\n") ;
}




private void BuildHtml()
{
AppendLine(@"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("");
}




/**////


/// 写 部分
///

private void BuildHead()
{
AppendLine("");




BuildMeta();
BuildLink();
BuildCSS();
BuildJavascript();
BuildExcelProperties();




AppendLine((""));
}




/**////


/// 写 部分
///

private void BuildBody()
{
AppendLine("");




AppendLine(_contentBuilder.ToString());




//comment list
AppendLine(@"




");
AppendLine(_commentBuilder.ToString());
AppendLine("

");





AppendLine("");
}




Head Write Method#region Head Write Method




private int _styleIndex = 30 ;
private StringBuilder _styleBuilder = new StringBuilder() ;




/**////


/// 为单元格添加一种样式
///

/// 背景色
/// 顶部是否闭合
/// 底部是否闭合
/// 左边是否闭合
/// 右边
/// 文字大小
/// 是否为粗体
/// css类名
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() ;
}




/**////


/// 写 Meta 部分
///

private void BuildMeta()
{
AppendLine(" AppendLine("") ;
AppendLine("") ;
}




/**////


/// 写 Linked File
///

private void BuildLink()
{
AppendLine("") ;
AppendLine("") ;
AppendLine("") ;
}




private void BuildCSS()
{
string css = @"





" ;
AppendLine(css) ;
}




private void BuildJavascript()
{
AppendLine(@"





") ;
}




private void BuildExcelProperties()
{
AppendLine(string.Format(@"





",
_fileName));
}




#endregion




About Comment#region About Comment





/**////


/// 批注的 Builder
///

StringBuilder _commentBuilder = new StringBuilder() ;




int curIndex = 0 ;




/**////


/// Shape Type
///

const string SHAPE_TYPE = @"


" ;




/**////


/// 添加批注
///

/// 被批注单元格从0开始所在的行索引
/// 被批注单元格从0开始所在的列索引
/// 单元格内容
/// 批注内容
/// 增加了批注后的单元格内容
public string AddComment(int row, int column, string text, string comment)
{
if (row < 0)
{
throw new ArgumentOutOfRangeException("row") ;
}
if (column < 0)
{
throw new ArgumentOutOfRangeException("column") ;
}
if (text == null)
{
throw new ArgumentNullException("text") ;
}
if (comment == null)
{
throw new ArgumentNullException("comment") ;
}




curIndex++ ;




_commentBuilder.Append(string.Format(@"











onmouseover=""msoCommentShow('_com_{0}','_anchor_{0}')""
onmouseout=""msoCommentHide('_com_{0}')"" language=JavaScript>






name=""_msocom_{0}"">[{0}]






class=shape>{4}:

{5}

















",
curIndex,
(curIndex == 1 ? SHAPE_TYPE : ""),
row,
column,
_authorName,
comment)) ;




return string.Format(@"{1} class=msocomspan1> onmouseover=""msoCommentShow('_com_{0}','_anchor_{0}')""
onmouseout=""msoCommentHide('_com_{0}')"" language=JavaScript> class=msocomanch href=""#_msocom_{0}"" name=""_msoanchor_{0}"">[1]
",
curIndex,
text) ;
}





#endregion
}
}





示例:
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(@"

") ;
tableBuilder.Append("

") ;




style1 = excel.AddCellStyle(Color.Blue, true, true, true, true, 9, true) ;
tableBuilder.Append(string.Format("

", style1)) ;
tableBuilder.Append(excel.AddComment(curRow, curCol, "User Name", "用户名")) ;
tableBuilder.Append("

") ;




tableBuilder.Append(string.Format("

", style1)) ;
curCol++ ;
tableBuilder.Append(excel.AddComment(curRow, curCol, "Password", "密码")) ;
tableBuilder.Append("

") ;




tableBuilder.Append(string.Format("

", style1)) ;
curCol++ ;
tableBuilder.Append(excel.AddComment(curRow, curCol, "Email", "电子邮件")) ;
tableBuilder.Append("

") ;

tableBuilder.Append("

") ;




string style2 = excel.AddCellStyle(Color.Yellow, true, true, false, false, 9, false) ;
foreach (DataSet1.UserInformationRow userRow in dataSet11.UserInformation)
{
curRow++ ;
curCol = 0 ;
tableBuilder.Append(string.Format("

", style2)) ;
tableBuilder.Append(excel.AddComment(curRow, curCol, userRow.UserName, userRow.UserName)) ;
tableBuilder.Append("

") ;




tableBuilder.Append(string.Format("

", style2)) ;
curCol++ ;
tableBuilder.Append(excel.AddComment(curRow, curCol, userRow.Password, userRow.Password)) ;
tableBuilder.Append("

") ;




tableBuilder.Append(string.Format("

", style2)) ;
curCol++ ;
tableBuilder.Append(excel.AddComment(curRow, curCol, userRow.Email, userRow.Email)) ;
tableBuilder.Append("

") ;

tableBuilder.Append("

") ;
}




tableBuilder.Append(@"

") ;




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
















































添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.