0

我正在使用以下代码将数据表转换为 excel: 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();
    }

}

但是对于值 3E2 或 0E2 或 4E3 ,它被转换为数字.. 1D2 等其他值被正确转换为字符串。

例如 3E2 变成 300 等等。

4

1 回答 1

1

在值周围加上双引号,以便 Excel 可以清楚地看出它是一个字符串,而不是科学计数法中的数字。

于 2010-01-31T16:32:07.210 回答