【问题标题】:How to merge cells to the last row in poi如何将单元格合并到poi中的最后一行
【发布时间】:2016-10-27 03:02:09
【问题描述】:

我正在使用 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 开始,但代码中不允许这样做,请帮助我。提前致谢。

【问题讨论】:

  • 您是否尝试使用后增量 j++ 而不是 j+1 in row = sheet.createRow(j+1); ??
  • @Khuzi 我做了,但它跳过了一行

标签: java excel apache-poi


【解决方案1】:

rowCount 增量存在问题。预先增加行数会跳过最后一行进行合并。将其更改为发布增量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){}

}}

输出:

【讨论】:

  • 感谢您为这段代码使用的解决方案,但我正在将您的解决方案应用于另一段不起作用的代码。我正在编辑我的帖子,请查看一次。
猜你喜欢
  • 2020-12-27
  • 2015-12-20
  • 1970-01-01
  • 2019-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
相关资源
最近更新 更多