XSSF Apache POI XSSFCellStyle

时间:2016-06-01 12:51:37

标签: xssf poi-hssf

使用方法setRotation使用HSSFCellStyle旋转列标题正常工作如下程序---

 public static void main(String[] args)throws Exception 
{
  HSSFWorkbook workbook = new HSSFWorkbook(); 
  HSSFSheet spreadsheet = workbook.createSheet(
  "Text direction");
  HSSFRow row = spreadsheet.createRow(2);
  HSSFCellStyle myStyle = workbook.createCellStyle();
  myStyle.setRotation((short) 0);
  HSSFCell cell = row.createCell(1);
  cell.setCellValue("0D angle");
  cell.setCellStyle(myStyle);

  myStyle=workbook.createCellStyle();
  myStyle.setRotation((short) 90);
  cell = row.createCell(3);
  cell.setCellValue("30D angle");
  cell.setCellStyle(myStyle);

  myStyle=workbook.createCellStyle();
  myStyle.setRotation((short) -90);
  cell = row.createCell(5);
  cell.setCellValue("90D angle");
  cell.setCellStyle(myStyle);

  FileOutputStream out = new FileOutputStream(
  new File("textdirection.xlsx"));
  workbook.write(out);
  out.close();
  System.out.println( 
  "textdirection.xlsx written successfully");
}

但使用XSSF输出文件列标题写入的相同代码不会旋转。 下面的代码使用XSSF -

public static void main(String[] args)throws Exception 
{
  XSSFWorkbook workbook = new XSSFWorkbook(); 
  XSSFSheet spreadsheet = workbook.createSheet(
  "Text direction");
  XSSFRow row = spreadsheet.createRow(2);
  XSSFCellStyle myStyle = workbook.createCellStyle();
  myStyle.setRotation((short) 0);
  XSSFCell cell = row.createCell(1);
  cell.setCellValue("0D angle");
  cell.setCellStyle(myStyle);

  myStyle=workbook.createCellStyle();
  myStyle.setRotation((short) 180);
  cell = row.createCell(3);
  cell.setCellValue("30D angle");
  cell.setCellStyle(myStyle);

  myStyle=workbook.createCellStyle();
  myStyle.setRotation((short) -180);
  cell = row.createCell(5);
  cell.setCellValue("90D angle");
  cell.setCellStyle(myStyle);

  FileOutputStream out = new FileOutputStream(
  new File("textdirection.xlsx"));
  workbook.write(out);
  out.close();
  System.out.println( 
  "textdirection.xlsx written successfully");
}

所以任何人都可以给我一些暗示。

感谢。

1 个答案:

答案 0 :(得分:1)

所有关于XLS和XLSX表格和工作簿的格式 - 它们都不同。

以下是setRotation()方法JavaDoc的一部分:

  

public void setRotation(short rotation)

     

设置单元格中文本的旋转度

     

以度数表示。值范围从0到180.第一个字母   文本被认为是弧的中心点。对于0 - 90,   值代表地平线以上的度数。对于91-180度以下   地平线计算如下:[度低于地平线] = 90 -   textRotation。

     

注意:HSSF使用-90到90度的值,而   XSSF使用0到180度的值。这个的实现   然而,方法将相应地映射这两个值范围   相应的getter返回的值在命令范围内   应用此CellStyle的当前Excel文件格式   到。

所以,这是你愿意做的正确的例子:

package com.github.xsavikx.apachepoitest;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


public class ApachePOITest {
    public static void main(String[] args) throws Exception {
        XSSF();
        HSSF();
    }

    private static void XSSF() throws IOException {
        String filename = "textdirection_xssf.xlsx";
        try (XSSFWorkbook workbook = new XSSFWorkbook();
             FileOutputStream out = new FileOutputStream(new File(filename));) {

            XSSFSheet spreadsheet = workbook.createSheet(
                "Text direction");
            XSSFRow row = spreadsheet.createRow(2);
            XSSFCellStyle myStyle = workbook.createCellStyle();
            myStyle.setRotation((short) 0);
            XSSFCell cell = row.createCell(1);
            cell.setCellValue("0D angle");
            cell.setCellStyle(myStyle);

            myStyle = workbook.createCellStyle();
            myStyle.setRotation((short) 90);
            cell = row.createCell(3);
            cell.setCellValue("30D angle");
            cell.setCellStyle(myStyle);

            myStyle = workbook.createCellStyle();
            myStyle.setRotation((short) 180);
            cell = row.createCell(5);
            cell.setCellValue("90D angle");
            cell.setCellStyle(myStyle);
            workbook.write(out);
            System.out.println(String.format("%s written successfully", filename));
        }
    }

    private static void HSSF() throws IOException {
        String filename = "textdirection_hssf.xls";
        try (HSSFWorkbook workbook = new HSSFWorkbook();
             FileOutputStream out = new FileOutputStream(new File(filename));) {
            HSSFSheet spreadsheet = workbook.createSheet(
                "Text direction");
            HSSFRow row = spreadsheet.createRow(2);
            HSSFCellStyle myStyle = workbook.createCellStyle();
            myStyle.setRotation((short) 0);
            HSSFCell cell = row.createCell(1);
            cell.setCellValue("0D angle");
            cell.setCellStyle(myStyle);

            myStyle = workbook.createCellStyle();
            myStyle.setRotation((short) 90);
            cell = row.createCell(3);
            cell.setCellValue("30D angle");
            cell.setCellStyle(myStyle);

            myStyle = workbook.createCellStyle();
            myStyle.setRotation((short) -90);
            cell = row.createCell(5);
            cell.setCellValue("90D angle");
            cell.setCellStyle(myStyle);


            workbook.write(out);
            System.out.println(String.format("%s written successfully", filename));
        }
    }

}