2007年9月30日 星期日

組html字串以串出excel表

//組成html字串以串出excel表
Response.Clear();

Response.Charset = "big5";

Response.AddHeader("Content-Disposition","attachment;filename="+ Server.UrlEncode("計畫基本資料一欄表.xls"));

//若改成ms-word及上行副檔名改為符合的副檔名就可轉為word

Response.ContentType = "application/vnd.ms-excel";

Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5");//字碼為big5碼格式
Response.Write("<html><body>");

Response.Write(
"<meta http-equiv=Content-Type
content=text/html; charset=big5>"
); //避免亂碼

Response.Write("<style type=text/css>");

Response.Write("td{mso-number-format:\"\\@\";}");
//將所有欄位格式改為"文字"

Response.Write("</style>");

System.IO.StringWriter sw = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(sw);

宣告sw為htmltextwriter元件
//sw.WriteLine("")=>組出html語法


sw.WriteLine("<table border=0>");


sw.WriteLine("</table>");

Response.Write("<table border=0><tr><td>");

Response.Write(sw.ToString());
Response.Write("</td></tr></table>");
Response.Write("</body></html>");

Response.End();//結束字串

沒有留言: