I am working on a Human Resources app, that needs to show a working environment excel report, like it's demonstrated in this image.
Now I am looking for help with this topic, I am using ClosedXML.Excel
, currently I am generating my Excel files with a method i create myself, it enters a List of objects and creates an excel file in the response of a http request. Here is the code:
public static bool ConvertToExcel<T>(IList<T> data, string excelName, string sheetName)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name.Replace("_"," "), Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name.Replace("_", " ")] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
try
{
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(table, sheetName);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.Charset = "";
string FileName = excelName + ".xlsx";
System.Web.HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + FileName);
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(System.Web.HttpContext.Current.Response.OutputStream);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
}
}
catch (Exception e)
{
throw e;
return false;
}
return true;
}
Because of the long names of the bosses it extends a lot the excel column for a simple percentage, I want to know if I can achieve to rotate down (90 degrees) the letter of the header. It's possible to achieve this with my current library ClosedXML.Excel
? I want to used the same method to generate this working environment report.
Thanks beforehand :)