.net导出EXCEL文件操作类包括格式较复杂表格导出

复杂的页面数据表格导出成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();
}

}
}

相见: http://www.lvtaostudio.com/2009/0717/174.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值