I'm trying to export data to excel from a datatable. currently all the data goes into column one and I can't figure out why. Can someone help?
public static void ExportToExcel(System.Data.DataTable dt, string excelFilePath = null)
{
// string FileName = "Test.xls";
string fullpath = excelFilePath;
int tblborder = 2;
StreamWriter SW;
SW = File.CreateText(fullpath);
StringBuilder objSB = new StringBuilder();
objSB.Append("<Table border=" + tblborder + " width=100%>");
objSB.Append("<tr>");
for (int i = 0; i < dt.Columns.Count; i++)
{
objSB.Append("<th valign=middle>" + dt.Columns[i].ColumnName + "</th>");
}
objSB.Append("</tr>");
objSB.Append("<tr>");
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
objSB.Append("<tr>");
objSB.Append("<td align=center>" + dt.Rows[i][j].ToString() + "</td>");
objSB.Append("</tr>");
}
}
objSB.Append("</Table>");
SW.Write(objSB.ToString());
SW.Close();
// Response.Redirect(filePath);
}
Select all Open in new window
Open in new window