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 に答える