在软件中体悟人生 在人生中感悟软件

专注Web项目设计、实现和管理
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

【经验分享】解决Excel中乱码问题

Posted on 2009-01-08 09:23  王景  阅读(664)  评论(0编辑  收藏  举报

    #region 导出至Excel

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

            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);

           
            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;

            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, "区域审核排名");

                Response.Buffer = true;
                Response.Clear();
                Response.Flush();
                AlertShow(this.Page, "导出成功!");
            }
            catch (Exception ex)
            {
                AlertShow(this.Page, "" + ex.Message + "导出失败!");
                return;
            }
            finally
            {
                Response.End();
            }
        }

        public void CreateExcel(DataTable dt, string FileName)
        {
            System.Web.UI.WebControls.DataGrid m_Datagrid = new DataGrid();
            m_Datagrid.DataSource = dt;
            m_Datagrid.DataBind();

            Response.Clear();
            Response.Buffer = true;
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AppendHeader("content-disposition","attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("中文名称",System.Text.Encoding.UTF8) + ".xls\"");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

            Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            m_Datagrid.RenderControl(oHtmlTextWriter);
            Response.Output.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();

        }

        public override void VerifyRenderingInServerForm(Control control)
        { }
        #endregion