Asp.net导出到Excel

Asp.net导出到Excel 的问题已经是老生常谈了,今天忽然用到,感觉以前写的有点乱,就顺手封装了一下,贴出代码,仅供参考

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Data;

namespace AK.T.FileDeal
{
    public class Excel
    {

    /// <summary>
        /// 导出到Excel titile为excel的头部,datatable字段必须和title向对应
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="title"></param>
        public void CreateExcel(DataTable ds, string[] title, System.Web.UI.Page page)
        {
            HttpResponse resp;
            resp = page.Response;
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=Cards_" + DateTime.Now.ToShortDateString() + ".xls");

            string ls_item = "";
            string colHeaders = "";
            DataTable dt = ds;
            DataRow[] myRow = dt.Select("");

            for (int i = 0; i < title.Length; i++)
            {
                if (i == title.Length - 1)
                {
                    colHeaders += title[i] + "\n";
                    continue;
                }
                colHeaders += title[i] + "\t";
            }

           int count = title.Length;

            foreach (DataRow row in myRow)
            {
                for (int i = 0; i < count; i++)
                {
                    if (i == count - 1)
                    {
                        ls_item += row[i].ToString() + "\n";
                        continue;
                    }
                    ls_item += row[i].ToString() + "\t";
                }
            }
            resp.Write(colHeaders + ls_item);
            resp.End();
        }

        /// <summary>
        ///  导出到Excel titile为excel的头部,DtRow为ds中字段的名称
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="title"></param>
        /// <param name="DtRow"></param>
        public void CreateExcel(DataTable ds, string[] title, string[] DtRow, System.Web.UI.Page page)
        {
            if (DtRow.Length != ds.Rows.Count)
            {
                return;
            }
            HttpResponse resp;
            resp = page.Response;
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=Cards_" + DateTime.Now.ToShortDateString() + ".xls");

            string ls_item = "";
            string colHeaders = "";
            DataTable dt = ds;
            DataRow[] myRow = dt.Select("");

            for (int i = 0; i < title.Length; i++)
            {
                if (i == title.Length - 1)
                {
                    colHeaders += title[i] + "\n";
                    continue;
                }
                colHeaders += title[i] + "\t";
            }
            foreach (DataRow row in myRow)
            {
                for (int i = 0; i < DtRow.Length; i++)
                {
                    if (i == DtRow.Length - 1)
                    {
                        ls_item += row[DtRow[i]].ToString() + "\n";
                        continue;
                    }
                    ls_item += row[DtRow[i]].ToString() + "\t";
                }
            }
            resp.Write(colHeaders + ls_item);
            resp.End();
        }

        /// <summary>
        /// 单行数据导出
        /// </summary>
        /// <param name="title"></param>
        /// <param name="Content"></param>
        public void CreateOnceExcel(string[] title, string[] Content, System.Web.UI.Page page)
        {
            HttpResponse resp;
            resp = page.Response;
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=Cards_" + DateTime.Now.ToShortDateString() + ".xls");

            string ls_item = "";
            string colHeaders = "";

            for (int i = 0; i < title.Length; i++)
            {
                if (i == title.Length - 1)
                {
                    colHeaders += title[i] + "\n";
                    continue;
                }
                colHeaders += title[i] + "\t";
            }
            for (int i = 0; i < Content.Length; i++)
            {
                if (i == Content.Length - 1)
                {
                    ls_item += Content[i].ToString() + "\n";
                    continue;
                }
                ls_item += Content[i].ToString() + "\t";
            }

            resp.Write(colHeaders + ls_item);
            resp.End();
        }

    }
}

posted @ 2012-05-27 14:32  老Key  阅读(413)  评论(0编辑  收藏  举报