我正在开发一个人力资源应用程序,需要显示工作环境 Excel 报告,如下图所示。
现在我正在寻找有关此主题的帮助,我正在使用ClosedXML.Excel,当前我正在使用我自己创建的方法生成 Excel 文件,它输入对象列表并在 http 请求的响应中创建 Excel 文件。这是代码:
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 …Run Code Online (Sandbox Code Playgroud)