download excle的幾個問題的解決

1)直接將Table轉成excle

2)合併儲存格(表中套表)

3)純數字的文字的會變成了數字而消除了前面的0==>這個解決方式比較簡單粗暴,全部變成了文字格式的,轉出來的excle中是數字的欄需自己轉

            Response.Clear();
            Response.BufferOutput = true;
            string fileName = "123.xls";
            //设定输出的字符集 
            Response.Charset = "GB2312";
            //假定导出的文件名为FileName.xls 
            Response.AppendHeader("Content-Disposition", "attachment;filename= " + fileName);
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            //设置导出文件的格式 
            Response.ContentType = "application/ms-excel";
            //关闭ViewState 
            EnableViewState = false;
            System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
            System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);

            //DataTable dt = new DataTable();

            //DataGrid excel = new DataGrid();
            //excel.DataSource = dt.DefaultView;
            //excel.DataBind();
            //excel.RenderControl(textWriter);

            textWriter.Write("<table><tr><th>1.1</th><th>1.2</th><th>1.3</th></th>");
            textWriter.Write("<tr><td>2.1</td><td>2.2</td><td>2.3</td></tr>");
            textWriter.Write("<tr><td>3.1</td><td><table><th><th>3.2.1</th><td>3.2.2</td></tr><tr><td>3.2.3</td><td>3.2.4</td></tr></table></td><td>2.3</td></tr></table>");

            //把HTML写回浏览器
            //解决第一位字符为零时不显示的问题和条码被认定为数字问题。
            Response.Write("<head><style>table td,th{vnd.ms-excel.numberformat:@;text-align: center;} table th{color:red;background-color:blue}</style></head>");
            Response.Write(stringWriter.ToString());
            Response.End();
原文地址:https://www.cnblogs.com/wonder223/p/6380583.html