简体   繁体   English

如何在单元格中右对齐RichTextString?

[英]How can I right align a RichTextString in a cell?

When I set the alignment on a cell that's a number it aligns, when the cell is text it won't. 当我在一个单元格上设置对齐时,它是一个对齐的数字,当单元格是文本时,它不会。

Workbook workbook = new XSSFWorkbook();
CreationHelper creationHelper = workbook.getCreationHelper();
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(0);

// doesn't
Cell richTextCell = row.createCell(0);
RichTextString richTextString = creationHelper.createRichTextString("So rich!");
richTextCell.setCellValue(richTextString);
CellStyle richTextCellStyle = richTextCell.getCellStyle();
richTextCellStyle.setAlignment(CellStyle.ALIGN_RIGHT);

// works
Cell numberCell = row.createCell(1);
numberCell.setCellValue(12.34);
CellStyle numberCellStyle = numberCell.getCellStyle();
numberCellStyle.setAlignment(CellStyle.ALIGN_RIGHT);


FileOutputStream fileOutputStream = new FileOutputStream("workbook.xlsx");
try {
    workbook.write(fileOutputStream);
} finally {
    fileOutputStream.close();
}

I duplicated this behavior when creating an .xlsx workbook with an XSSFWorkbook . 我在使用XSSFWorkbook创建.xlsx工作簿时重复此行为。 Interestingly, when I changed to an HSSFWorkbook , the behavior changed and both cells were aligned right. 有趣的是,当我改为HSSFWorkbook ,行为发生了变化,两个单元格都正确对齐。

I took a closer look at the .xlsx when I opened it in Excel. 当我在Excel中打开它时,我仔细查看了.xlsx。 The Excel toolbar button that indicates "aligned right" was not highlighted for either cell, even though the number looked like it was aligned to the right. 对于任一单元格,都没有突出显示“右对齐”的Excel工具栏按钮,即使该数字看起来与右侧对齐。 For reference, the toolbar button is in "Home", "Alignment" section, and looks like this (big closeup): 作为参考,工具栏按钮位于“Home”,“Alignment”部分,看起来像这样(大特写):

________
  ______
________
  ______
________
  ______

However, by default, all numbers in Excel are already right-aligned. 但是,默认情况下,Excel中的所有数字都已经右对齐。 It looks like for .xlsx workbooks, you can't just get the current cell style if it's the default cell style and modify it. 对于.xlsx工作簿来说,如果它是默认的单元格样式并修改它,则不能只获取当前的单元格样式。 You must create a new CellStyle , set its properties, and set the new cell style for the cell. 您必须创建一个新的CellStyle ,设置其属性,并为该单元格设置新的单元格样式。

The following code works in .xls and .xlsx workbooks. 以下代码适用于.xls和.xlsx工作簿。 Additionally, it also creates only one CellStyle object, to be used on all applicable cells, demonstrating the appropriate reuse of CellStyle objects. 此外,它还只创建一个CellStyle对象,用于所有适用的单元格,演示CellStyle对象的适当重用。

CellStyle rightAligned = workbook.createCellStyle();
rightAligned.setAlignment(CellStyle.ALIGN_RIGHT);

Cell richTextCell = row.createCell(0);
RichTextString richTextString = creationHelper.createRichTextString("So rich!");
richTextCell.setCellValue(richTextString);
richTextCell.setCellStyle(rightAligned);

Cell numberCell = row.createCell(1);
numberCell.setCellValue(12.34);
numberCell.setCellStyle(rightAligned);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM