簡體   English   中英

如何使用 Apache POI 在電子表格單元格中旋轉文本?

[英]How to rotate text in a spreadsheet cell using Apache POI?

如何在 Apache POI 的 HSSFCell 類中旋轉文本?

使用 HSSFCellStyle,該類有一個名為 setRotation(short rotation) 的方法,它將旋轉文本。 您所做的就是將單元格樣式應用於單元格:

HSSFCellStyle myStyle = workbook.createCellStyle();
myStyle.setRotation((short)90);

HSSFCell c = row.createCell(columnNumber);
c.setCellStyle(myStyle);
CellStyle cssVertical = wb.createCellStyle();
cssVertical.setFont(f);
cssVertical.setRotation((short)90);
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
XSSFRow row = sheet.createRow(1);
XSSFCell cell = row.createCell(1);

XSSFCellStyle cs = workbook.createCellStyle();
cs.setRotation((short) 90);              // set text rotation
cs.getStyleXf().setApplyAlignment(true); // <<< Important

cell.setCellValue("Vertical Text");
cell.setCellStyle(cs);

workbook.write(new FileOutputStream("out.xlsx"));

Apache POI 3.17,需要在cellXfs部分手動添加alignment="true"屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM