zoukankan      html  css  js  c++  java
  • MemoryStream生成Excel

    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.06.19 DP-方格取数 + 花店橱窗布置
    2021.06.15 DP-编辑距离
    2021.06.12模拟总结
    2021.6.8 背包模拟 总结
    20210529-背包
    lnmp环境中的:supervisorctl
    python常用语法合集
    python 常用数据结构
    DVWA环境
    mysql中each( use () {})
  • 原文地址:https://www.cnblogs.com/zengwangjing/p/net.html
Copyright © 2011-2022 走看看