You must Sign In to post a response.
  • Category: ASP.NET

    Excel export leading zero problemi in c# from datatable

    I am using the following procedure to create an Excel spreadsheet from a DataTable:

    protected void UploadDataTableToExcel(DataTable dtEmp, string filename)
    {
    string attachment = "attachment; filename=" + filename;
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/vnd.ms-excel";
    string tab = string.Empty;
    foreach (DataColumn dtcol in dtEmp.Columns)
    {
    Response.Write(tab + dtcol.ColumnName);
    tab = "\t";
    }
    Response.Write("\n");
    foreach (DataRow dr in dtEmp.Rows)
    {
    tab = "";
    for (int j = 0; j < dtEmp.Columns.Count; j++)
    {
    Response.Write(tab + Convert.ToString(dr[j]));
    tab = "\t";
    }
    Response.Write("\n");
    }
    Response.End();
    }

    It works fine except when the cell only has numbers. For example:

    This "6969062201223401504" becomes "6.96902E+18"
    This "076854957" becomes "76854957"

    I would like the Excel spreadsheet to preserve every column as text, instead of converting them into integers
  • #763291
    Hi,

    Refer below link here they give some formats, may be this might be helpful to you, "csharp.net-informations.com/excel/csharp-format-excel.htm"

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments