复杂的页面数据表格导出成EXCEL
的操作类
多个方法的封装实现多中导出功能 详情如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
namespace Export
{
public enum ExportType
{
Word = 1,
Excel = 2,
HTML = 3
}
/// <summary>
/// ExportExcel 的摘要说明
/// </summary>
public class ExportExcel
{
public ExportExcel()
{
}
/// <summary>
/// 导出EXCEL
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="pn">承载导出内容的控件</param>
public static void exPort(string fileName, Panel pn,ExportType exportType)
{
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlText = new HtmlTextWriter(oStringWriter);
pn.RenderControl(htmlText);
outWrite(fileName, oStringWriter.ToString(), exportType);
}
/// <summary>
/// 导出EXCEL
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="table">承载导出内容的控件</param>
public static void exPort(string fileName, HtmlTable table, ExportType exportType)
{
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlText = new HtmlTextWriter(oStringWriter);
table.RenderControl(htmlText);
outWrite(fileName, oStringWriter.ToString(), exportType);
}
/// <summary>
/// 导出EXCEL
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="tb">承载导出内容的空间</param>
public static void exPort(string fileName, Table tb, ExportType exportType)
{
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlText = new HtmlTextWriter(oStringWriter);
tb.RenderControl(htmlText);
outWrite(fileName, oStringWriter.ToString(), exportType);
}
/// <summary>
/// 导出EXCEL
/// </summary>
/// <param name="fileNmae"></param>
/// <param name="body">内容</param>
/// <param name="exportType"></param>
public static void exPort(string fileName, string body, ExportType exportType)
{
outWrite(fileName, body, exportType);
}
private static void outWrite(string fileName, String body, ExportType exportType)
{
HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.Clear();
Response.Buffer = true;
fileName = HttpContext.Current.Server.UrlEncode(fileName);
//如果上面的方式再度出现文件名乱码就采用该方式
//fileName = HttpUtility.UrlEncode(fileName);
if (exportType == ExportType.Word)
{
fileName += ".doc";
Response.ContentType = "application/vnd.ms-word";
}
else if (exportType == ExportType.Excel)
{
fileName += ".xls";
Response.ContentType = "application/vnd.ms-excel";
}
else if (exportType == ExportType.HTML)
{
fileName += ".htm";
Response.ContentType = "application/vnd.ms-html";
}
//attachment 参数表示作为附件下载,可以改成 online在线打开
//filename=*.doc 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm
Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
//Response.ContentType指定文件类型 可以为application/ms-excel application/ms-word application/ms-txt application/ms-html 或其他浏览器可直接支持文档
string str = " <!--[if gte mso 9]><xml>"
+ " <x:ExcelWorkbook>"
+ " <x:ExcelWorksheets>"
+ " <x:ExcelWorksheet>"
+ " <x:Name></x:Name>"
+ " <x:WorksheetOptions>"
+ " <x:Print>"
+ " <x:ValidPrinterInfo />"
+ " </x:Print>"
+ " </x:WorksheetOptions>"
+ " </x:ExcelWorksheet>"
+ " </x:ExcelWorksheets>"
+ " </x:ExcelWorkbook>"
+ " </xml>"
+ " <![endif]-->";
if (body.IndexOf("<html>") != -1)
{
body.Replace("<html>", "<html xmlns:x=/"urn:schemas-microsoft-com:office:excel/">");
int headIndex = body.IndexOf("<head>");
body = body.Insert(headIndex + 5, str);
}
else {
body = "<html xmlns:x=/"urn:schemas-microsoft-com:office:excel/"><head>" + str + "</head><body>" + body;
body = body + "</body></html>";
}
//System.Web.HttpContext.Current.EnableViewState = false;
Response.Write(body);
Response.Flush();
Response.End();
}
/// <summary>
/// 返回指定控件的内容
/// </summary>
/// <param name="pn"></param>
/// <returns></returns>
public static string getBodys(Panel pn)
{
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlText = new HtmlTextWriter(oStringWriter);
pn.RenderControl(htmlText);
return oStringWriter.ToString();
}
/// <summary>
/// 返回指定控件的内容
/// </summary>
/// <param name="pn"></param>
/// <returns></returns>
public static string getBodys(HtmlTable table)
{
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlText = new HtmlTextWriter(oStringWriter);
table.RenderControl(htmlText);
return oStringWriter.ToString();
}
/// <summary>
/// 返回指定控件的内容
/// </summary>
/// <param name="pn"></param>
/// <returns></returns>
public static string getBodys(Table tb)
{
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlText = new HtmlTextWriter(oStringWriter);
tb.RenderControl(htmlText);
return oStringWriter.ToString();
}
public static string getBodys(GridView gv)
{
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlText = new HtmlTextWriter(oStringWriter);
gv.RenderControl(htmlText);
return oStringWriter.ToString();
}
}
}