export datatable to excel using C# with leading zeros

  • Thread starter Thread starter lalitha annem
  • Start date Start date
L

lalitha annem

Guest
Hi,

While exporting the datatable to Excel in asp.net application,leading zeros are missing.
For example : Data in the cell is "00102030"
After exporting to excel it is displaying me like this "102030".
Actually i should get "00102030" after exporting to excel.
Please help me out of this.

Code for your reference:

DataTable dtExcel = new DataTable();
dtExcel = getOldData(rptbndl_childs);

Response.ClearContent();
Response.AddHeader("content-disposition","attachment;filename=TestData.xls");
Response.ContentType = "application/ms-excel";///vnd.ms-excel";
string tab = string.Empty;
//string style = @"<style> .textmode { mso-number-format:\@; } </style>";
//Response.Write(style);
Response.Write("<style>.text { mso-number-format:\\@; } </style>");
foreach (DataColumn datacol in dtExcel.Columns)
{
Response.Write(tab + datacol.ColumnName);
tab = "\t";
}
Response.Write("\n");

foreach (DataRow dr in dtExcel.Rows)
{
tab = "";
for (int j = 0; j < dtExcel.Columns.Count; j++)
{
Response.Write(tab + Convert.ToString(dr[j]));
tab = "\t";
}
Response.Write("\n");
}
//Response.Write("<style>.text { mso-number-format:\\@; } </style>");
Response.End();

I applied styles also but no hopes :(

Thanks,Lalitha. A

Continue reading...
 
Back
Top