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

收藏本信息编号:174 发布时间:2009-07-17 截止日期: 地区:

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

    }
}

发布人信息

[错误报告] [推荐] [收藏][打印] [关闭] [返回顶部]

当前共有0人发表了评论.

推荐信息

最新信息