不生成Excel文件,将Datatable数据 Response.write 输出生成Excel

不生成Excel文件,将Datatable数据 Response.write 输出生成Excel

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Text;

namespace ExeclSaveAS
{
    public partial class _Default : System.Web.UI.Page
    {
        //可以输出到Excel,并显示正确。但是会有错误提示信息。不知哪位高手有无办法解决
        protected void Page_Load(object sender, EventArgs e)
        {
            //ShowExecl();//提示三次

            DataTable1Excel();//只提示一次
        }

        private void ShowExecl()
        {
            DataTable dt = GetDataSource();
            Response.Charset = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AppendHeader("Content-Disposition", "attachment;filename=ccc_mys.xls");
            Response.ContentType = "application/ms-excel";
            string colHeaders = "", ls_item = "";
            //定义表对象与行对象,同时用DataSet对其值进行初始化 
            DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的 
            int i = 0;
            int cl = dt.Columns.Count;

            //取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符 
            for (i = 0; i < cl; i++)
            {
                if (i == (cl - 1))//最后一列,加n 
                {
                    colHeaders += dt.Columns[i].Caption.ToString() + "\n";
                }
                else
                {
                    colHeaders += dt.Columns[i].Caption.ToString() + "\t";
                }

            }
            // resp.Write(colHeaders); 
            Response.Write(colHeaders);
            //向HTTP输出流中写入取得的数据信息

            //逐行处理数据 
            foreach (DataRow row in myRow)
            {
                //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据 
                for (i = 0; i < cl; i++)
                {
                    if (i == (cl - 1))//最后一列,加n 
                    {
                        ls_item += row[i].ToString() + "\n";
                    }
                    else
                    {
                        ls_item += row[i].ToString() + "\t";
                    }

                }
                Response.Write(ls_item);
                ls_item = "";

            } 
            Response.Flush();
            Response.End(); 
        }

        /// <summary>
        /// 数据源(这里给的数据源是一个Datatable )
        /// </summary>
        /// <returns></returns>
        private DataTable GetDataSource()
        {
            DataTable newdata = new DataTable();
            newdata.Columns.Add("ID");
            newdata.Columns.Add("Name");
            
            object [] OneRowValue=new object []{"1","AAAA"};
            object[] TwoRowValue = new object[] { "2", "BBBBB" };
            
            newdata.Rows.Add(OneRowValue);
            newdata.Rows.Add(TwoRowValue);

            return newdata;
        }


        /// <summary>
        /// 只提示一次
        /// </summary>
        public void DataTable1Excel()
        {
           DataTable dtData = GetDataSource();

            System.Web.UI.WebControls.GridView gvExport = null;
            
            // 当前对话 
            System.Web.HttpContext curContext = System.Web.HttpContext.Current;
            
            // IO用于导出并返回excel文件 
            System.IO.StringWriter strWriter = null;
            System.Web.UI.HtmlTextWriter htmlWriter = null;

            if (dtData != null)
            {
                // 设置编码和附件格式 
                curContext.Response.ContentType = "application/vnd.ms-excel";
                curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                curContext.Response.Charset = "utf-8";

                // 导出excel文件 
                strWriter = new System.IO.StringWriter();
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
                // 为了解决gvData中可能进行了分页的情况,需要重新定义一个无分页的GridView 
                gvExport = new System.Web.UI.WebControls.GridView();

                gvExport.DataSource = dtData.DefaultView;
                gvExport.AllowPaging = true;
                gvExport.DataBind();

                // 返回客户端 
                gvExport.RenderControl(htmlWriter);
                curContext.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" + strWriter.ToString());
                curContext.Response.End();
            }
        }
    }

}

你可能感兴趣的:(Datatable)