asp.net网页以Excel格式输出,并动态控制分页

2008年11月9日星期日

asp.net网页以Excel格式输出,并动态控制分页

问题描述:需要将一个asp.net网页在客户端以Excel格式输出,并且输出的内容能动态控制Excel分页及字体行高等样式。

解决思路:难点在于如果控制分页。通过对Excel文件内部进行研究分析,发现可以通过Excel内部分页指令来动态控制在指定行进行分页。此解决思路对各种BS开发(Asp.net JSP PHP等)都是通用的。

具体样例代码如下:

protected void BtnRecord_Click(object sender, EventArgs e)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(this.HidText.Value + ".xls"));
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.Write(ExportSVRecord());
HttpContext.Current.Response.End();
}
public string ExportSVRecord()
{
try
{
....

StringBuilder html = new StringBuilder();
StringBuilder PageBreaks = new StringBuilder();
PageBreaks.Append("");
return PageBreaks.ToString()+" "+html.ToString();
}
catch (Exception err)
{
return "";
}
}


    0 评论:

    发表评论