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

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

问题描述

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

我用这个竖排文本制作了一个excel文件,并导出为一个xml文件.然后我发现样式有一个名为'VerticalText'的属性:

根据经验,我选择了 Apache POI.但是我找不到任何方法来生成带有 POI 的单元格样式.我只能找到rotate方法,不能满足要求.

于是我阅读了更多POI的代码,发现cellstyles是从一些xsb文件构建的,也不包含垂直文本.

非常感谢任何帮助.

解决方案

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

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

用于垂直文本.

这可以使用 apache poi 设置,如下所示:

import java.io.FileOutputStream;导入 org.apache.poi.ss.usermodel.*;导入 org.apache.poi.xssf.usermodel.XSSFWorkbook;公共类 CreateXSSFVerticalText {public static void main(String[] args) 抛出异常 {工作簿工作簿 = new XSSFWorkbook();CellStyle cellStyle = workbook.createCellStyle();cellStyle.setRotation((short)255);Sheet sheet = workbook.createSheet();行行 = sheet.createRow(0);单元格单元格 = row.createCell(0);cell.setCellValue("测试");cell.setCellStyle(cellStyle);FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx");workbook.write(fileOut);fileOut.close();workbook.close();}}

由于 Office Open XML 格式(如 *.xlsx)是包含 XML 文件的 ZIP 档案,因此很容易确定必要的 XML 属性.只需使用 Excel GUI 创建一个具有所需格式的简单 *.xlsx 文件.然后解压 *.xlsx 文件并查看 xl/styles.xml.

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

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':

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.

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.

Any help much appreciated.

解决方案

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>
...

There <alignment textRotation="255"/> is for vertical text.

This can be set using apache poi like so:

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

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天全站免登陆