2016-10-27 54 views
0

我正在使用apache poi创建一个工作簿,我试图将特定单元格合并到输出结尾。我使用mergeRegion函数和单元格进行合并,但是,该单元格不合并到该行的结束,它总是在结束之前一行,如何合并单元格到poi中的最后一行

我附上这里的屏幕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但这是不允许的代码,请帮助我。提前感谢。

+0

你尝试以上后增量J ++代替J + 1行= sheet.createRow第(j + 1)的代码; ?? – Khuzi

+0

@Khuzi我做了,但它跳过了一行 – storyteller

回答

2

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

+0

感谢您的解决方案为这段代码而烦恼,但我正在将您的解决方案应用到另一段代码,这是不工作。我正在编辑我的帖子,请看看一次。 – storyteller

相关问题