web页面导出到Excel乱码解决,基于PHP导出Excel的小经验

自己在PHP项目里须求把数量导出为Excel,何况数据中隐含中文.
网上海大学概理解一下然则使用PHPExcel,但是相对作者的供给,那个框架太复杂了.于是依旧想找找简单做法.
网络开掘实际上最轻便易行能够如此写,但难题是这种做法中文的编码不可相信..

web页面导出到Excel乱码解决,基于PHP导出Excel的小经验。    #region 导出至Excel

因此追踪Asp.net服务器代码,未有乱码,可是导出Excel到浏览器后,展开时出现乱码。

引言:
前天 在做web项指标时候 供给导出页面上的数码 到Excel里面
但一些时候出现乱码(有de时候不出新 很意外)
原来的代码是如此的:
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(“content-disposition”,
        “attachment;filename=FileName.xls”);
        HttpContext.Current.Response.Charset = “GB2312”;
        HttpContext.Current.Response.ContentEncoding =
System.Text.Encoding.GetEncoding(“GB2312”);//.Unicode;//.UTF8;//
        HttpContext.Current.Response.ContentType = “xls”;
//”application/vnd.xls”;
        System.IO.StringWriter stringWrite = new
System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new
HtmlTextWriter(stringWrite);
        mygridview.RenderControl(htw);
        HttpContext.Current.Response.Write(sw.ToString());
        HttpContext.Current.Response.End();

复制代码 代码如下:

        protected void btnExport_Click(object sender, EventArgs e)
        {
            //获得数据
            DataTable dtExportItemLy = null;

解决方法是增添编码格式的前缀字节码:
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble;

哪些解决?

<?php
 header(“Content-type:application/vnd.ms-excel”);
 header(“Content-Disposition:attachment;filename=export_data.xls”);
 echo   “姓名”.”\t”; 
 echo   “繁體”.”\t”; 
 echo   “博客”.”\t”; 
 echo   “\n”; 
 echo   “jason”.”\t”; 
 echo   “@”.”\t”; 
 echo   “javaeye”.”\t”; 
 ?>

            string where = ” 1 =1 “;
            DataSet ds =
DalHelper.Logic_Performance_Area.GetExportList(where);
            ds.Tables[0].DefaultView.Sort = “C11_9”;
            ds.Tables[0].Columns.Add(“OrderId”);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                int k = ds.Tables[0].Rows.Count – i;
                ds.Tables[0].Rows[i][“OrderId”] = k.ToString();
            }
            ds.Tables[0].DefaultView.Sort = “OrderId Asc”;
            //年度
            DataTable tempdt = new DataTable();
            DataColumn tempdc1 = new DataColumn(“Year”);
            tempdt.Columns.Add(tempdc1);
            //排名
            DataColumn tempdc2 = new DataColumn(“OrderId”);
            tempdt.Columns.Add(tempdc2);
            //公司名称
            DataColumn tempdc3 = new DataColumn(“Enterprise_Name”);
            tempdt.Columns.Add(tempdc3);
            //得分
            DataColumn tempdc4 = new DataColumn(“C11_9”);
            tempdt.Columns.Add(tempdc4);
            //制表人
            DataColumn tempdc5 = new DataColumn(“CommitMan”);
            tempdt.Columns.Add(tempdc5);
            //提交时间
            DataColumn tempdc6 = new DataColumn(“CommitDate”);
            tempdt.Columns.Add(tempdc6);

Response.Clear();Response.AddHeader("content-disposition","attachment;filename=Test.xls");   Response.ContentType = "application/ms-excel";Response.ContentEncoding = System.Text.Encoding.Unicode;Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble;System.IO.StringWriter sw = new System.IO.StringWriter();System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter;FormView1.RenderControl;Response.Write(sw.ToString;Response.End();

修改为上边包车型地铁代码 问题就一举成功了

多少同学会想到header加入字符集

           
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow tempdr = tempdt.NewRow();
                tempdr[“Year”] =
ds.Tables[0].Rows[i][“Year”].ToString();
                tempdr[“OrderId”] =
ds.Tables[0].Rows[i][“OrderId”].ToString();
                tempdr[“Enterprise_Name”] =
ds.Tables[0].Rows[i][“Enterprise_Name”].ToString();
                tempdr[“C11_9”] =
ds.Tables[0].Rows[i][“C11_9”].ToString();
                tempdr[“CommitMan”] =
ds.Tables[0].Rows[i][“CommitMan”].ToString();
                tempdr[“CommitDate”] =
ds.Tables[0].Rows[i][“CommitDate”].ToString();
                tempdt.Rows.Add(tempdr);
            }
            dtExportItemLy = tempdt;

举个例子Utf-8的Preamble是3个字节:239,187,191。那八个字节位于文件的底部,表示大家的文件是utf-8格式的。

        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.Write(“<meta
http-equiv=Content-Type;
content=text/html;charset=gb2312>”);            
        HttpContext.Current.Response.AddHeader(“content-disposition”,
“attachment; filename=MyExcelFile.xls”);
        HttpContext.Current.Response.ContentEncoding =
System.Text.Encoding.GetEncoding(“GB2312”);
        HttpContext.Current.Response.ContentType =
“application/excel”;
        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htw = new
System.Web.UI.HtmlTextWriter(sw);
        mygridview.RenderControl(htw);
        HttpContext.Current.Response.Write(sw.ToString());
        HttpContext.Current.Response.End();

复制代码 代码如下:

            if (dtExportItemLy == null) return;
            if (dtExportItemLy.Rows.Count <= 0)
            {
                AlertShow(this.Page, “暂无数据足以导出!”);
                return;
            }
          
            try
            {
                dtExportItemLy.Columns[0].ColumnName = “年度”;
                dtExportItemLy.Columns[1].ColumnName = “排名”;
                dtExportItemLy.Columns[2].ColumnName = “公司名称”;
                dtExportItemLy.Columns[3].ColumnName = “得分”;
                dtExportItemLy.Columns[4].ColumnName = “制表人”;
                dtExportItemLy.Columns[5].ColumnName = “提交时间”;
                this.CreateExcel(dtExportItemLy, “区域查处排行”);

精心相比一下 正是

网站地图xml地图