asp.net 导出Excel并设置其字段为数字乱码的列

protected override void DoExcelClick()
        {
            ExportExcel(Page.Response, "房建业绩");
        }

        private void ExportExcel(HttpResponse Response, string FileName)
        {
            DataTable dt = SRV.SQLSrv.GeneralQuery(Grid.SQL, Grid.DB, Grid.QyeryList).Tables[0];
            string style = @"<style> .text { mso-number-format:\@; } </script> ";
            string Nowtime = "";            
            string str1 = String.Empty;
            string str2 = String.Empty;
            string str3 = String.Empty;
            Nowtime = Nowtime + System.DateTime.Now.ToString();
            Nowtime = Nowtime.Replace("/", "").Replace(" ", "").Replace(":", "");
            System.IO.StringWriter sw = new System.IO.StringWriter();

            Response.Write(style);

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                str2 = "<table border='1'><tr>";
                str3 = "</tr></table>";
                str1 = str1 + "<td class='text'>" + dt.Columns[i].ColumnName.ToString() + "</td>";
            }
            sw.WriteLine(str2 + str1 + str3);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                str1 = String.Empty;
                str2 = "<table border='1'><tr>";
                str3 = "</tr></table>";
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    str1 += "<td class='text'>" + dt.Rows[i][j] + "</td>";
                }
                sw.WriteLine(str2 + str1 + str3);
            }
            sw.Close();
            Response.Buffer = true;
            Response.Charset = "";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + "_" + Nowtime + ".xls");
            Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");//设置输出流为简体中文
            Response.Write(sw);
            Response.End();
        }

你可能感兴趣的:(asp.net 导出Excel并设置其字段为数字乱码的列)