User1016820894 posted
I am exporting data from sql to an excel spreadsheet in asp.net code behind (code is below). It is working fine but I need to add 2 header rows at the top before the normal header rows and data. One of the header rows needs to be merged and centered across
several columns. How do I do this?
Thanks.
try
{
SQLCmd = "SELECT Name, DOB, Address, City, State, Zip " +
"FROM dbo.tbldata; ";
sheetname = "sheet1;
filename = "Excel Report";
using (SqlCommand cmd = new SqlCommand(SQLCmd))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = connsql;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt, sheetname);
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
}
}
}
}
}
}
catch (Exception Ex)
{
ClientScript.RegisterStartupScript(GetType(), "showError", "alert('" + Ex.Message + "');", true);
return "error";
}
finally
{
Response.End();
}