I have field called “Comments” in my DataTable which has few records more than 255 characters.When we export the DataTable to Excel using the below code, the data is pushed into Excel but the Comments field record which has more than 255 characters overlaps other cells in Excel and the next column record is pushed to next row.
Code:
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=XXXXXX.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
foreach (DataRow dr in dt.Rows){
tab = "";
for (int i = 0; i < dt.Columns.Count; i++){
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();
Can you guys please help
Use EPPlus excel library and create real excel files, it’s free and open source and works really well.
I tested this scenario and fill cell with text longer than 4096 bytes, and it works OK, but with excel 2010 and xslx format.
Here is the example code :
btw. you can find here example how to export DataTable to excel file :
https://stackoverflow.com/a/9569827/351383