使用Apache POI合并和居中单元格

31

我希望使用 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");

现在我希望将按项目列出的列表合并,并使其居中对齐。

9个回答

19

我的解决方案是按它们的位置合并单元格,然后创建一个单元格(引用合并单元格的第一个块),分配一个值,并通过 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);

6
使用CellUtil.setAlignment(cell, HorizontalAlignment.CENTER);,方法***setAlignment(Cell cell, Workbook workbook, short align)***现在从3.15-beta2版本开始已经过时。 - Am_I_Helpful

16

合并类似的内容:

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

为了对齐,还可以查看以下Apache poi的官方链接:::

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


是的,你说得对,我能够合并单元格,但我无法使其居中对齐。 - shrey

12

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

headerStyle.setAlignment(headerStyle.ALIGN_CENTER);

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

你能详细解释一下你为什么认为这更加“简洁”吗?对于为什么或如何运作的洞察,可以大大帮助其他用户。 - Regular Jo
我同意发帖者的观点。使用 D.R.Y. 方法确实更加简洁。如果这段代码出现问题,你只需要在这里修复即可。 - ButtahNBred
在更新的版本中,请使用以下代码: style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); - Mugeesh Husain

3

我使用的方法是将所有合并单元格的单元格样式设置为居中对齐。无论是在设置单元格样式值为居中之前还是之后调用XSSFSheet.addMergedRegion()方法都没有关系。

    private void insertXlsHeader(XSSFSheet sheet){
    ....
    //first cell for row1       
    cell = row1.createCell(colstart);
    cell.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
    cell.setCellValue("COURSES");
    setHeaderCellStyle(sheet,cell);

    //first cell for row2
    cell = row2.createCell(colstart);
    setHeaderCellStyle(sheet,cell);

    //first cell for row3
    cell = row3.createCell(colstart);
    setHeaderCellStyle(sheet,cell);

    //merged the first cells of rows 1 to 3
    sheet.addMergedRegion(new CellRangeAddress(ROW1, ROW3, colstart, colstart));
    ...
    }

private void setHeaderCellStyle(XSSFSheet sheet,org.apache.poi.ss.usermodel.Cell cell) {
    CellStyle s = null;

        s = sheet.getWorkbook().createCellStyle();
        cell.setCellStyle(s);

    Font f = sheet.getWorkbook().createFont();

    f.setBoldweight(Font.BOLDWEIGHT_BOLD);


    s.setBorderBottom(CellStyle.BORDER_THIN);
    s.setBorderLeft(CellStyle.BORDER_THIN);
    s.setBorderRight(CellStyle.BORDER_THIN);
    s.setBorderTop(CellStyle.BORDER_THIN);
    s.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    s.setAlignment(CellStyle.ALIGN_CENTER);
    s.setFont(f);

}

3
根据我的理解,您有合并单元格的起始和结束单元格,并希望合并单元格范围并对齐单元格内容。如果我没错的话,您可以使用以下方法:
/**
 * @param startCell: first cell of merging area
 * @param endCell: last cell of merging area
 */

public static void mergeAndAlignCenter(HSSFCell startCell, HSSFCell endCell){
    //finding reference of start and end cell; will result like $A$1
    CellReference startCellRef= new CellReference(startCell.getRowIndex(),startCell.getColumnIndex());
    CellReference endCellRef = new CellReference(endCell.getRowIndex(),endCell.getColumnIndex());
    // forming string of references; will result like $A$1:$B$5 
    String cellRefernce = startCellRef.formatAsString()+":"+endCellRef.formatAsString();
    //removing $ to make cellRefernce like A1:B5
    cellRefernce = cellRefernce.replace("$","");
    //passing cellRefernce to make a region 
    CellRangeAddress region = CellRangeAddress.valueOf(cellRefernce);
    //use region to merge; though other method like sheet.addMergedRegion(new CellRangeAddress(1,1,4,1));
    // is also available, but facing some problem right now.
    startCell.getRow().getSheet().addMergedRegion( region );
    //setting alignment to center
    CellUtil.setAlignment(startCell, wb, CellStyle.ALIGN_CENTER);
}

2

如上所述,可以使用合并单元格来实现此操作。

sheet.addMergedRegion(new CellRangeAddress(frstRow, lastRow, firstColumnIndex, lastColumnIndex));

然而,对于垂直对齐单元格,最近我遇到了类似的问题,我尝试了上面的答案,但是使用

CellUtil.setAlignment(dataCell, workbook, CellStyle.VERTICAL_CENTER);

将日期格式的单元格对齐到水平左侧。因此,我使用以下方法仅设置单元格内容的垂直对齐方式。

CellUtil.setCellStyleProperty(dataCell, workbook,CellUtil.VERTICAL_ALIGNMENT,CellStyle.VERTICAL_CENTER);

I hope this helps!!

Happy Coding


0

使用

style.setVerticalAlignment()

设置垂直对齐方式而不是

style.setAlignment().                             

0

我们可以合并列,同时也可以进行垂直和水平对齐。

我有列A的第2到10行具有相同的值。

enter image description here

我使用以下代码合并数据,其中变量sheetXSSFSheetCellRangeAddress的参数是起始行、最后一行、起始列和最后一列的参数。在我的示例中,值USA从第2行(索引为1)开始,最后一个值USA在第10行,列为第1列。
CellRangeAddress ca = new CellRangeAddress(1,9,0,0);
sheet.addMergedRegion(ca);

当我执行上述代码时,单元格已合并,但文本未居中对齐。

enter image description here

为了解决这个问题,我利用了 CellStyleCell 类。将第二行第一列的文本获取到 cell 变量中。现在将垂直和水平对齐设置到 cellStyle 中,并将此样式设置到 cell 上,以使文本居中对齐。
Cell cell = sheet.getRow(1).getCell(0);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cell.setCellStyle(cellStyle);

以下是最终结果

enter image description here



其他参考资料

  1. 使用的Jar文件

enter image description here

  • 导入语句

    在此输入图片描述


  • 网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接