【问题标题】:How to format exponent to number from Export to excel?如何将指数格式化为从导出到excel的数字?
【发布时间】:2013-10-11 08:07:43
【问题描述】:

我正在将数据表导出到 Excel,其中一列在数据表中包含电话号码,但在 Excel 中导出后,电话号码列显示为指数。

我需要号码,如何解决这个问题?

 string fileName = "File" + DateTime.Now.ToString("MMddyyyy_HHmmss") + ".xls";
        Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
        //Response.AddHeader("content-disposition", "attachment;filename=File.xls");
        Response.ContentType = "application/vnd.ms-excel";

        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
        DataGrid dataExportExcel = new DataGrid();
        dataExportExcel.ItemDataBound += new DataGridItemEventHandler(dataExportExcel_ItemDataBound);
        dataExportExcel.DataSource = dt;
        dataExportExcel.DataBind();
        dataExportExcel.RenderControl(htmlWrite);
        System.Text.StringBuilder sbResponseString = new System.Text.StringBuilder();
        sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:xlExcel8\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head></head> <body>");
        sbResponseString.Append(stringWriter + "</body></html>");
        Response.Write(sbResponseString.ToString());
        Response.End();

【问题讨论】:

  • this ?
  • 您想以编程方式在电话号码前放置一个撇号
  • 这对我的问题有帮助吗?我写的上面的代码导出到excel,还有其他最好的选择吗?我正在将数据表转换为 html,实际上我需要将数据表导出到 excel?

标签: c# asp.net


【解决方案1】:

将以下 STYLE 添加到您的 HTML 标记中,它会对您有所帮助..

<style> table { mso-number-format:'0'; } </style>

像这样:

sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:xlExcel8\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><style> td { mso-number-format:'0'; } </style></head> <body>");

完整代码:

   protected void btnExcel_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Clear();
        dt.Columns.Add("Phone");
        dt.Columns.Add("Name");
        DataRow Sample = dt.NewRow();
        Sample["Phone"] = 125316245612456124;
        Sample["Name"] = "Pandian";
        dt.Rows.Add(Sample);
        GetExcel(dt);            
    }
    public void GetExcel(DataTable dt)
    {
        string fileName = "File" + DateTime.Now.ToString("MMddyyyy_HHmmss") + ".xls";
        Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
        DataGrid dataExportExcel = new DataGrid();
        dataExportExcel.DataSource = dt;
        dataExportExcel.DataBind();
        dataExportExcel.RenderControl(htmlWrite);
        System.Text.StringBuilder sbResponseString = new System.Text.StringBuilder();
        sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:xlExcel8\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><style> table { mso-number-format:'0'; } </style></head> <body>");
        sbResponseString.Append(stringWriter + "</body></html>");
        Response.Write(sbResponseString.ToString());
        Response.End();
    }

Excel 输出:

【讨论】:

  • 嗨 pandian 它不适用于超过 500 个字符,对于日期单元格,请帮助我首选哪种格式?
【解决方案2】:

使用这个 td { mso 数字格式:“@”; }

【讨论】:

    【解决方案3】:

    您可以使用NumberValue and NumberFormat properties

    这里是一个示例,设置你的范围

    yourSheet.Range[".."].NumberValue = 1234.5678;
    yourSheet.Range[".."].NumberFormat = "0.00";
    

    【讨论】:

      【解决方案4】:

      您可以在电话号码前面加上一个单引号,然后再将其写入电子表格的单元格。

      喜欢“'1234567890”而不是“1234567890”

      谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多