今天遇到了这个问题,记录下来,方便不知道的朋友以后用,当在生成Excel表格时直接将网页表格直接保存成excel文件后,用excel能打开表格中的数字自动用科学计数法表示了,比如table中的0.0在excel中显示为0。

解决方案如下。

   protected void downloadButton_Click(object sender, EventArgs e)
    {
        BindPage();

        Response.ClearContent();
        string attachment = "attachment; filename=Payroll.xls";
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        tabData.Border = 1;
        tabData.RenderControl(htw);
        string startHtml = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">";
        string endHtml = "</html>";
        Response.Write(startHtml + sw.NewLine + sw.ToString().Replace("<td align=\"right\" style=\"width:80px;\">","<td align=\"right\" style=\"width:80px;\" x:str>") + endHtml);
        Response.End(); 
    }

相关文章:

  • 2021-11-11
  • 2022-03-04
  • 2021-06-09
  • 2022-03-05
  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
猜你喜欢
  • 2021-08-25
  • 2021-11-27
  • 2022-12-23
  • 2022-01-10
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案