2016-10-27 86 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

相關問題