public static MemoryStream ToExcel<T>(List<T> list, string filePath = null)
{
var memoryStream = new MemoryStream();
 

IWorkbook workbook = new HSSFWorkbook();
string sheetName = typeof(T).Name;
ISheet sheet = workbook.CreateSheet(sheetName);
IRow headerRow = sheet.CreateRow(0);
Type elementType = typeof(T);
// handling header.

int headerIndex = 0;
elementType.GetProperties().ToList().ForEach(propInfo =>
{
ICell headerCell = headerRow.CreateCell(headerIndex);
headerIndex = headerIndex + 1;
headerCell.SetCellValue(propInfo.Name);


});
int rowIndex = 1;
foreach (T item in list)
{
IRow dataRow = sheet.CreateRow(rowIndex);
int rowcellIndex = 0;
elementType.GetProperties().ToList().ForEach(propInfo =>
{
ICell cell = dataRow.CreateCell(rowcellIndex);

string value = (propInfo.GetValue(item, null) ?? "").ToString();
cell.SetCellValue(value);
rowcellIndex++;
});
rowIndex++;
}


///storage/emulated/0/DCIM
//FileStream fs = new FileStream("/storage/emulated/0/DCIM/log.xls", FileMode.OpenOrCreate, FileAccess.ReadWrite);

workbook.Write(memoryStream);

//fs.Write(memoryStream.ToArray(), 0, (int)memoryStream.Length);
 
//fs.Dispose();
workbook = null;

return memoryStream;
 

}

相关文章:

  • 2021-12-02
  • 2021-10-12
  • 2021-07-23
  • 2021-10-14
  • 2022-03-05
  • 2021-11-03
  • 2021-09-28
猜你喜欢
  • 2022-12-23
  • 2021-12-04
  • 2021-12-09
  • 2021-11-18
  • 2021-11-18
  • 2022-01-15
相关资源
相似解决方案