首页 文章

如何将单元格合并到poi中的最后一行

提问于
浏览
0

我正在使用apache poi创建一个工作簿,我正在尝试将特定单元格合并到输出的末尾 . 我正在使用mergeRegion函数并且单元格正在合并但是,该单元格未合并到行的末尾,它始终是结束前一行,

我在这附加屏幕merged cell

我想要正确合并单元格,我在这里发布我的代码

for(MarshActiveUser marshActiveUser : listOfMarshUser){

           /***/
            sheet.addMergedRegion(new CellRangeAddress(
                    j, //first row (0-based)
                    j, //last row (0-based)
                    18, //first column (0-based)
                     20 //last column (0-based)
                      ));
            /***/

            int columnNo = 0;
            row = sheet.createRow(j+1);
            cell = row.createCell(columnNo);
            cell.setCellValue(new HSSFRichTextString(String.valueOf(row.getRowNum())));
            lockedCellStyle.setFont(hSSFFont);
            sheet.autoSizeColumn(0);
            cell.setCellStyle(lockedCellStyle);
            columnNo = 1;
            cell = row.createCell(columnNo);

            if(null != marshActiveUser.getFistName()){

                cell.setCellValue(new HSSFRichTextString(marshActiveUser.getFistName()));
                lockedCellStyle.setFont(hSSFFont);
                sheet.autoSizeColumn(1);
                cell.setCellStyle(lockedCellStyle);

            }else{
                cell.setCellValue(new HSSFRichTextString(" "));
                 cell.setCellStyle(lockedCellStyle);
            }

我试图从rowCount 1开始但是代码中不允许这样做,请帮助我 . 谢谢 .

1 回答

  • 2

    rowCount增量存在问题 . 预先增加行数是跳过最后一行进行合并 . 将其更改为post increment rowcount++ 并按预期工作 .

    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.util.CellRangeAddress;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    
    public class SimpleExcelWriterExample {
    
     public static void main(String[] args) throws IOException {
         HSSFWorkbook workbook = new HSSFWorkbook();
         HSSFSheet sheet = workbook.createSheet("Java Books");
    
            Object[][] bookData = {
                    {"Head First Java", "Kathy Serria", 79},
                    {"Effective Java", "Joshua Bloch", 36},
                    {"Clean Code", "Robert martin", 42},
                    {"Thinking in Java", "Bruce Eckel", 35},
            };
    
            int rowCount = 1;
    
            for (Object[] aBook : bookData) {
    
                /***/
                sheet.addMergedRegion(new CellRangeAddress(
                        rowCount, //first row (0-based)
                        rowCount, //last row (0-based)
                          3, //first column (0-based)
                          5 //last column (0-based)
                          ));
                /***/
                Row row = sheet.createRow(rowCount++);
    
                int columnCount = 0;
    
                for (Object field : aBook) {
                    Cell cell = row.createCell(++columnCount);
                    if (field instanceof String) {
                        cell.setCellValue((String) field);
                    } else if (field instanceof Integer) {
                        cell.setCellValue((Integer) field);
                    }
                }
    
            }
    
    
            try{
                FileOutputStream outputStream = new FileOutputStream("D://JavaBooks.xls");
                workbook.write(outputStream);
            }catch(Exception e){}
    
    }}
    

    Output:

    enter image description here

相关问题