Search code examples
c#export-to-excel

importing Data table to Excel text becomes numeric


Hi just want to ask I am trying to import Datatables to excel and one of the columns becomes exponent like 444201000100100 becomes 4442exp8? here's my script

protected void btnExportfromDt_Click(object sender, EventArgs e)
{
    string strFilename = "FinanceforBulkupload.xls";
    UploadDataTableToExcel(LoadData(), strFilename);
}
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();
}

Solution

  • Pre-append a single quote to the value before you store it. Instead of:

    444201000100100
    

    store:

    '444201000100100