首页 > 其他 > 详细

download excle的幾個問題的解決

时间:2017-02-09 10:51:48      阅读:233      评论:0      收藏:0      [点我收藏+]

 

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

 

download excle的幾個問題的解決

原文:http://www.cnblogs.com/wonder223/p/6380583.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!