使用apache poi合并并对齐中心单元

shr*_*rey 24 java excel apache-poi

我想使用Apache poi将数据导出到excel .
现在我遇到的问题是我无法合并行并将它们对齐在中心.

出口数据代码是:

List<LinkedHashMap<String,Object>> lstReportHeader = null;
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();

//Set Header Font
HSSFFont headerFont = wb.createFont();
headerFont.setBoldweight(headerFont.BOLDWEIGHT_BOLD);
headerFont.setFontHeightInPoints((short) 12);

//Set Header Style
CellStyle headerStyle = wb.createCellStyle();
headerStyle.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
headerStyle.setAlignment(headerStyle.ALIGN_CENTER);
headerStyle.setFont(headerFont);
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
int rowCount= 0;
Row header;
header = sheet.createRow(0);//its for header 
Cell cell ;//= header.createCell(0);
for(int j = 0;j < 4; j++) {
    cell = header.createCell(j);
    if(j == 0) {
        cell.setCellValue("ItemWise List");
    }
    cell.setCellStyle(headerStyle);
}
sheet.addMergedRegion(new CellRangeAddress(rowCount, rowCount, 0, lstReportFormHeader.size()-1));
header = sheet.createRow(0);
        cell = header.createCell(0);
cell.setCellValue("Sr. No");
        cell = header.createCell(1);
cell.setCellValue("Item Name");
        cell = header.createCell(2);
cell.setCellValue("Qty");
        cell = header.createCell(3);
cell.setCellValue("Rate");
Run Code Online (Sandbox Code Playgroud)

现在我想要ItemWise List合并并使其对齐中心.

Rag*_*ine 16

我的解决方案是按位置合并单元格,然后创建一个单元格(引用合并单元格的第一个块)以分配值,然后通过CellUtil设置对齐方式

// Merges the cells
CellRangeAddress cellRangeAddress = new CellRangeAddress(start, start, j, j + 1);
sheet.addMergedRegion(cellRangeAddress);

// Creates the cell
Cell cell = CellUtil.createCell(row, j, entry.getKey());

// Sets the allignment to the created cell
CellUtil.setAlignment(cell, workbook, CellStyle.ALIGN_CENTER);
Run Code Online (Sandbox Code Playgroud)

  • 使用`CellUtil.setAlignment(cell,Horizo​​ntalAlignment.CENTER);`,方法***setAlignment(Cell cell,Workbook workbook,short align)***现在从3.15-beta2弃用. (3认同)

小智 11

合并:::

 Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");

Row row = sheet.createRow((short) 1);
Cell cell = row.createCell((short) 1);
cell.setCellValue("This is a test of merging");

sheet.addMergedRegion(new CellRangeAddress(
        1, //first row (0-based)
        1, //last row  (0-based)
        1, //first column (0-based)
        2  //last column  (0-based)
));

// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
Run Code Online (Sandbox Code Playgroud)

对齐也请查看Apache poi的以下官方链接:::

http://poi.apache.org/spreadsheet/quick-guide.html#Alignment


shr*_*rey 9

经过研究,我发现在合并7个单元格后,合并的单元格id将为0,因此我使用以下样式将以下样式应用于单元格ID 0.

headerStyle.setAlignment(headerStyle.ALIGN_CENTER);
Run Code Online (Sandbox Code Playgroud)


Iva*_*ang 5

这对我有用,我认为它更干净:

/**
 * Merge and center the cells specified by range
 * @param startCell the first cell in the cells to be merged
 * @param range the range of the cells to be merged
 */
private static void mergeAndCenter(Cell startCell, CellRangeAddress range) {
    startCell.getSheet().addMergedRegion(range);
    CellStyle style = startCell.getSheet().getWorkbook().createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    startCell.setCellStyle(style);
}
Run Code Online (Sandbox Code Playgroud)