gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:16:06 26 4
gpt4 key购买 nike

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

最佳答案

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

}}

输出:

enter image description here

关于java - 如何将单元格合并到poi中的最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40275403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com