I use the following code to export a datatable to excel
Is there a way to add a title to the excel sheet??
Here is the code I use to generate the excel sheet.
enter code here
public void export2(DataTable dt)
{
System.Web.UI.WebControls.DataGrid grid =
new System.Web.UI.WebControls.DataGrid();
grid.HeaderStyle.Font.Bold = true;
grid.DataSource = dt;
grid.DataBind();
using (StreamWriter sw = new StreamWriter("C:\\Reports\\Report.xls"))
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
grid.RenderControl(hw);
}
}
}
The output from this looks like this
Name Employee ID
abc 123
def 456
ghi 789
jkl 987
mno 654
is it possible to get the output as shown below??
Employee Report
Name Employee ID
abc 123
def 456
ghi 789
jkl 987
mno 654
Thanks in advance!!
One way would be to add the title(e.g. as Label) and the GridView/DataGrid to an UserControl wrapped in an ASP.NET Panel or DIV with appropriate CSS. Then you can render the whole
UserControlinstead of only the Grid.Another would be to create real excel files instead of html-tables for example with EPPlus.