跳到主要內容

Html Table To Excel / Txt

string content = " <table border=\"1\" style=\"border-top:1px solid black;font-size:10px\" > ";
            content += "<tr>  <td colspan=\"2\"> <b>標題一</b> </td>    <td colspan=\"2\"> <b>標題二</b> </td>  </tr> ";
            content += "<tr style=\"color:red\"> <td> A</td> <td>B  </td>  <td>C </td> <td>D  </td> </tr> ";
            content += "<tr>  <td> 2 </td> <td  style=\"text-align:right\">" + (char)2 + "3.6100  </td>  <td>112 </td> <td>3.05 </td> </tr> ";
            content += " <tr> <td> A</td> <td>B  </td>  <td>C </td> <td>D </td> </tr>";
            content += " <tr> <td> A</td> <td>B  </td>  <td>C </td> <td>D </td> </tr>";
            content += "";
            Literal1.Text = content;
         
         
            Response.ContentType = "application/x-msexcel";
            Response.AddHeader("Content-Disposition", "attachment;filename="+ DateTime.Now.ToString("yyyyMMddHHmm") +".xls");
            Response.ContentEncoding = Encoding.GetEncoding("big5");
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);

            Literal1.RenderControl(hw);

            Response.Write(tw.ToString());
            Response.End();



----------------------------------------------------------------------------

StringBuilder sb = new StringBuilder();
            string output = "Ac8!".PadRight(10, '-');
            sb.Append(output);
            sb.Append("\r\n");
            output = "中文測試".PadLeft(10,'-');
            sb.Append(output);
         
            string text = sb.ToString();

            Response.Clear();
            Response.ClearHeaders();

            Response.AddHeader("Content-Length", text.Length.ToString());
            Response.ContentType = "text/plain";
            Response.AppendHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmm") + ".txt");

            Response.Write(text);
            Response.End();