将数据表导出到excel,值3E2变成300,应该是文本3E2

发布于 2024-08-19 22:49:56 字数 1605 浏览 0 评论 0原文

我使用以下代码将数据表转换为 Excel: 公共静态无效 ExportDataTabletoExcel(String fileName, DataTable thetable) { if (表!= null) {

        //clear anything in io buffer
        HttpContext.Current.Response.Clear();
        //set to excel for to save file.
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        HttpContext.Current.Response.AddHeader(
        "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.BufferOutput = true;
        //write columns
        string tab = "";
        foreach (DataColumn dc in thetable.Columns)
        {
            HttpContext.Current.Response.Write(tab + dc.ColumnName);
            tab = "\t";

        }
        HttpContext.Current.Response.Write("\n");

        //write rows
        int i;
        foreach (DataRow dr in thetable.Rows)
        {
            tab = "";
            for (i = 0; i < thetable.Columns.Count; i++)
            {
                //if ( i != 1 ) 
                HttpContext.Current.Response.Write(tab + dr[i].ToString()); 
                //else
                //HttpContext.Current.Response.Write(tab + "'" + dr[i].ToString());
                tab = "\t";
            }
            HttpContext.Current.Response.Write("\n");
        }
        HttpContext.Current.Response.End();
    }

}

但是,对于值 3E2 或 0E2 或 4E3 ,它会转换为数字。其他值(如 1D2)会正确转换为字符串。

例如 3E2 变为 300 等。

I am converting datatable to excel using the following code:
public static void ExportDataTabletoExcel(String fileName, DataTable thetable)
{
if (thetable != null)
{

        //clear anything in io buffer
        HttpContext.Current.Response.Clear();
        //set to excel for to save file.
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        HttpContext.Current.Response.AddHeader(
        "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.BufferOutput = true;
        //write columns
        string tab = "";
        foreach (DataColumn dc in thetable.Columns)
        {
            HttpContext.Current.Response.Write(tab + dc.ColumnName);
            tab = "\t";

        }
        HttpContext.Current.Response.Write("\n");

        //write rows
        int i;
        foreach (DataRow dr in thetable.Rows)
        {
            tab = "";
            for (i = 0; i < thetable.Columns.Count; i++)
            {
                //if ( i != 1 ) 
                HttpContext.Current.Response.Write(tab + dr[i].ToString()); 
                //else
                //HttpContext.Current.Response.Write(tab + "'" + dr[i].ToString());
                tab = "\t";
            }
            HttpContext.Current.Response.Write("\n");
        }
        HttpContext.Current.Response.End();
    }

}

However for value 3E2 or 0E2 or 4E3 , it is converted to number.. other value like 1D2 is correctly converted to string.

for example 3E2 become 300 etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2024-08-26 22:49:56

将值放在双引号中,以便 Excel 可以明显看出它是字符串而不是科学记数法中的数字。

Put double quotes around the value so that it is obvious to Excel that it is a string and not a number in scientific notation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文