C#导出数据到Excel通用的方法类

导出数据到Excel通用的方法类,请应对需求自行修改。

资源下载列表

using System.Data;
using System.IO;


namespace IM.Common.Tools
{
    public class Export
    {
        public string Encoding = "UTF-8";
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;

        public void EcportExcel(DataTable dt, string fileName)
        {

            if (dt != null)
            {
                StringWriter sw = new StringWriter();
                CreateStringWriter(dt, ref sw);
                sw.Close();
                response.Clear();
                response.Buffer = true;
                response.Charset = Encoding;
                //this.EnableViewState = false;
                response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
                response.ContentType = "application/ms-excel";

                //response.ContentEncoding = System.Text.Encoding.GetEncoding(Encoding)
                response.ContentEncoding = System.Text.Encoding.UTF8;
                response.Write(sw);
                response.End();
            }

        }

        private void CreateStringWriter(DataTable dt, ref StringWriter sw)
        {
            string sheetName = "sheetName";

            sw.WriteLine("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
            sw.WriteLine("<head>");
            sw.WriteLine("<!--[if gte mso 9]>");
            sw.WriteLine("<xml>");
            sw.WriteLine(" <x:ExcelWorkbook>");
            sw.WriteLine(" <x:ExcelWorksheets>");
            sw.WriteLine(" <x:ExcelWorksheet>");
            sw.WriteLine(" <x:Name>" + sheetName + "</x:Name>");
            sw.WriteLine(" <x:WorksheetOptions>");
            sw.WriteLine(" <x:Print>");
            sw.WriteLine(" <x:ValidPrinterInfo />");
            sw.WriteLine(" </x:Print>");
            sw.WriteLine(" </x:WorksheetOptions>");
            sw.WriteLine(" </x:ExcelWorksheet>");
            sw.WriteLine(" </x:ExcelWorksheets>");
            sw.WriteLine("</x:ExcelWorkbook>");
            sw.WriteLine("</xml>");
            sw.WriteLine("<![endif]-->");
            sw.WriteLine("</head>");
            sw.WriteLine("<body>");
            sw.WriteLine("<table>");
            sw.WriteLine(" <tr>");
            string[] columnArr = new string[dt.Columns.Count];
            int i = 0;
            foreach (DataColumn columns in dt.Columns)
            {

                sw.WriteLine(" <td>" + columns.ColumnName + "</td>");
                columnArr[i] = columns.ColumnName;
                i++;
            }
            sw.WriteLine(" </tr>");

            foreach (DataRow dr in dt.Rows)
            {
                sw.WriteLine(" <tr>");
                foreach (string columnName in columnArr)
                {
                    sw.WriteLine(" <td style='vnd.ms-excel.numberformat:@'>" + dr[columnName] + "</td>");
                }
                sw.WriteLine(" </tr>");
            }
            sw.WriteLine("</table>");
            sw.WriteLine("</body>");
            sw.WriteLine("</html>");
        }
    }
}

 

转载于:https://www.cnblogs.com/huhangfei/p/4997201.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值