• POI读写Excel-操作包含合并单元格操作


    在上篇博客中写到关于Excel操作解析成相关的类,下面将写入一种Excel对Excel表格读取和写入。

    对于Excel表格操作,最重要的是创建workBook。其操作顺序是:

    1.获得WorkBook实例;

    Workbook workbook = WorkbookFactory.create(file);
    

    2.获得每一个Sheet对象,也就是Excel表格的每一个表格

    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
      Sheet sheet = workbook.getSheetAt(i);
    }
    

     3.获得sheet中的每一行

    for (int i = sheet.getFirstRowNum() ; i < sheet.getLastRowNum(); i++){
      Row row  =  sheet.getRow(i);
    }
    

     4.对每一行获得单元格数据

    for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
      Cell cell = row.getCell(i);
    }
    

     5.解析每个单元格的数据

    那么对于单元格的合并问题:

    使用sheet的addMergedRegion方法:

    sheet.addMergedRegion(new CellRangeAddress( int startRow,int endRow, int startCol, int endCol));// 设置单元格合并
    

     在这里需要注意:

    1.使用该方法时,需要先创建合并的单元行,也就是从startRow到EndRow的每一行

    2.startRow应大于等于endRow

    3.通过sheet.getRow(startRow).createCell(startCol);创建cell

    具体实现见GitHub上的代码:https://github.com/JinGangRed/poi-exceluntil

  • 相关阅读:
    error occurred during initialization of vm
    Service Discovery protocol(SDP)
    nRF51822EK_PRO
    Binder
    android samsung note3  device not found
    BLE pairing vs. bonding
    remote link Centos6.6 Horrible Slow
    depmod -a
    start and end call use itelephony and how to pick up a call
    WebService
  • 原文地址:https://www.cnblogs.com/kingkangstudy/p/6722375.html
Copyright © 2020-2023  润新知