• NPOI简单示例2—合并表头


    /// <summary>
    /// 创建sheet中的行和列
    /// </summary>
    /// <param name="sheet"></param>
    /// <param name="rowcount"></param>
    /// <param name="cellcount"></param>
    /// <returns></returns>
    private static ISheet Create(ISheet sheet, int rowcount, int cellcount)
    {
    for (int i = 0; i < rowcount; i++)
    {
    IRow targetRow = null;
    targetRow = sheet.CreateRow(i);
    for (int j = 0; j < cellcount; j++)
    {
    IRow row = sheet.GetRow(i);
    ICell cell = row.CreateCell(j);
    }
    }
    return sheet;
    }

    public static MemoryStream CommonBaseExcel(DataTable dtsource)
    {
    HSSFWorkbook workbook = new HSSFWorkbook();
    ISheet sheet = workbook.CreateSheet("test_01");
    IDataFormat format = workbook.CreateDataFormat();

    ICellStyle style = workbook.CreateCellStyle();
    style.VerticalAlignment = VerticalAlignment.Center;
    style.Alignment = HorizontalAlignment.Center;

    int rowCount = dtsource.Rows.Count + 1;
    int cellCount = dtsource.Columns.Count;
    Create(sheet, rowCount, cellCount);

    for (int i = 0; i < dtsource.Columns.Count; i++)
    {
    string cellValue = dtsource.Columns[i].ColumnName.ToString();
    //sheet.GetRow(0).GetCell(i).SetCellValue(cellValue);
    ICell cell = sheet.GetRow(0).GetCell(i);
    cell.SetCellValue(cellValue);
    cell.CellStyle = style;
    }
    for (int i = 0; i < dtsource.Rows.Count; i++)
    {
    int row = i + 1;
    for (int j = 0; j < dtsource.Columns.Count; j++)
    {
    string cellValue = dtsource.Rows[i][j].ToString();
    ICell cell = sheet.GetRow(row).GetCell(j);
    cell.SetCellValue(cellValue);
    cell.CellStyle = style;
    }

    }
    sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0,0));
    sheet.AddMergedRegion(new CellRangeAddress(0, 0, 1,2));

    using (MemoryStream ms = new MemoryStream())
    {
    FileStream file = new FileStream(@"F:\test.xls", FileMode.Create);
    workbook.Write(file);
    file.Close();
    return ms;
    }

    }

    public int Export(ExportBLLRequest body, ref string fileData, ref string folderName)
    {
    string userId = body.UserId;
    string companyId = body.CompanyId;

    //获取要导出的数据
    var users = DbService._User.LoadEntities(p => p.IsValid == true).ToList();

    DataTable dt = new DataTable();
    dt.Columns.Add("登录名", typeof(string));
    dt.Columns.Add("密码", typeof(string));
    dt.Columns.Add("", typeof(string));

    var row0 = dt.NewRow();
    row0[0] = "";
    row0[1] = "密码1";
    row0[2] = "密码2";
    dt.Rows.Add(row0);

    foreach (var userItem in users)
    {
    #region

    var row = dt.NewRow();
    int rowIndex = 0;
    row[rowIndex] = userItem.UserName;
    rowIndex++;
    row[rowIndex] = userItem.Password + "1";
    rowIndex++;
    row[rowIndex] = userItem.Password+"2";
    rowIndex++;
    dt.Rows.Add(row);

    #endregion

    }
    var ms = CommonBaseExcel(dt);
    var data = ms.ToArray();
    fileData = Convert.ToBase64String(data);

    return 0;
    }

  • 相关阅读:
    vue 项目中assets文件夹与static文件夹引用的区别
    v-on绑定特性命名带小横杠 ‘-’与props属性中变量怎么对应
    解决 The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
    解决win10无法完成更新 正在撤销更改
    Felix HttpServer call iPojo Demo
    Felix Http server Demo
    osgi学习
    windows一个目录下最大文件数目
    oracle默认配置ora文件位置
    iptables配置(/etc/sysconfig/iptables)
  • 原文地址:https://www.cnblogs.com/luoxiaoxiao102/p/6138549.html
Copyright © 2020-2023  润新知