我正在开发一个人力资源应用程序,需要显示工作环境 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 = 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;
}
Run Code Online (Sandbox Code Playgroud)
由于老板的名字很长,它以简单的百分比扩展了 Excel 列,我想知道是否可以实现向下旋转(90 度)标题的字母。我当前的图书馆可以实现这一目标ClosedXML.Excel吗?我想用同样的方法来生成这个工作环境报告。
预先感谢:)
您可以通过使用对齐文本旋转样式来做到这一点:
cell.Style.Alignment.SetTextRotation(90);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2236 次 |
| 最近记录: |