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);
}
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.