如何使用Apache POI制作verticaltext cellstyle? [英] How to make verticaltext cellstyle with Apache POI?

查看:229
本文介绍了如何使用Apache POI制作verticaltext cellstyle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我遇到了一个问题:
我需要用java导出一个excel(xlsx),它必须包含这种单元格样式:

Recently, I met a question: I need to export an excel(xlsx) with java, which must contain this kind of cell style:

我使用此垂直文本制作了一个excel文件,并导出为xml文件。然后我发现该样式有一个名为'VerticalText'的属性:

I made a excel file with this vertical text, and exported as a xml file. Then I found that the style has an attribute named 'VerticalText':

根据经验,我选择了Apache POI。但我找不到用POI生成单元格样式的任何方法。我只能找到无法满足要求的旋转方法。

By experience, I chose Apache POI. But I couldn't find any way to generate the cell style with POI. I could only find rotate method, which could't meet the requirement.

所以我读了更多的POI代码,发现cellstyles是从一些xsb文件构建的,其中也不包含垂直文本。

So I read more code of POI, and found that the cellstyles are build from some xsb file, which do not contain vertical text either.

非常感谢任何帮助。

推荐答案

图片中的XML是Excel 2003 SpreadsheetML。但是 * .xlsx 文件是包含Office Open XML文件的ZIP存档。在该ZIP存档中, styles.xml 包含:

The XML in your picture is Excel 2003 SpreadsheetML. But an *.xlsx file is a ZIP archive containing Office Open XML files. In that ZIP archive the styles.xml contains:

...
<cellXfs count="2">
 ...
 <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0">
  <alignment textRotation="255"/>
 </xf>
</cellXfs>
...

< alignment textRotation =255 /> 用于垂直文字。

可以使用 apache poi 像这样:

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class CreateXSSFVerticalText {

 public static void main(String[] args) throws Exception {
  Workbook workbook = new XSSFWorkbook();

  CellStyle cellStyle = workbook.createCellStyle();
  cellStyle.setRotation((short)255);

  Sheet sheet = workbook.createSheet();
  Row row = sheet.createRow(0);
  Cell cell = row.createCell(0);
  cell.setCellValue("test");
  cell.setCellStyle(cellStyle);


  FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx");
  workbook.write(fileOut);
  fileOut.close();
  workbook.close();
 }
}

自Office Open XML格式(如<$ c) $ c> * .xlsx )是包含XML文件的ZIP压缩文件,很容易确定必要的XML属性。只需使用 Excel GUI创建一个简单的 *。xlsx 文件,其中包含所需的格式。然后解压缩 * .xlsx 文件并查看 xl / styles.xml

Since the Office Open XML formats (like *.xlsx) are ZIP archives containing XML files it is pretty easy to determine the necessary XML attributes. Simply create a simple *.xlsx file having the needed formatting using the Excel GUI. Then unzip the *.xlsx file and have a look at xl/styles.xml.

这篇关于如何使用Apache POI制作verticaltext cellstyle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆