asp.net - datatable to Excel in c# -
while exporting datatable excel columns fall new line. don't know problem is. code below:
string attachment = "attachment; filename=test.xls"; response.clearcontent(); response.addheader("content-disposition", attachment); response.contenttype = "application/vnd.ms-excel"; string tab = ""; foreach (datacolumn dc in transposedtable.columns) { response.write(tab + dc.columnname); //tab = "\t"; } response.write("\t"); int i; foreach (datarow dr in transposedtable.rows) { response.write("\n"); tab = ""; (i = 0; < transposedtable.columns.count; i++) { response.write(tab + dr[i].tostring()); tab = "\t"; } response.write("\t"); } response.end();
i have tried many ways, didn't exact issue. issue excel in machine or code?
it seems possible datarow data contains newline characters, 1 reason why newlines might appearing in data.
swapping:
response.write(tab + dr[i].tostring());
for:
string replacement = regex.replace(dr[i].tostring(), @"\t|\n|\r", ""); response.write(tab + replacement);
may fix problem if did diagnose correctly.
Comments
Post a Comment