简体   繁体   English

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

[英]How to merge cells to the last row in poi

I am creating a workbook using apache poi where i am trying to merge a particular cell to the end of the output.I am using mergeRegion function and cell is merging but, that cell is not merging to the end of the row , it is always ending one line before , 我正在使用apache poi创建一个工作簿,其中我正在尝试将特定的单元格合并到输出的末尾。我正在使用mergeRegion函数并且单元格正在合并,但是该单元格未合并到行的末尾,它总是在前一行结束,

i am attaching the screen here merged cell 我将屏幕合并到此处合并的单元格

I want the cell to be merged properly, i am posting my code here 我希望单元格正确合并,我在这里发布代码

    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);
            }

I have tried to start from rowCount +1 but that is not allowed in code , please help me .Thanks in advance. 我尝试从rowCount +1开始,但是代码中不允许这样做,请帮助我。谢谢。

There is a problem with rowCount increment. rowCount增量有问题。 Pre incrementing the row count is skipping your last row for merging. 预增加行数会跳过最后一行进行合并。 Changed it to post increment rowcount++ and its working as expected. 将其更改为发布增量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: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM