首页
学习
活动
专区
圈层
工具
发布

XSSF Apache
EN

Stack Overflow用户
提问于 2016-06-01 12:51:36
回答 1查看 1K关注 0票数 1

使用HSSFCellStyle使用setRotation方法旋转列标题,如下程序所示--

代码语言:javascript
复制
 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

代码语言:javascript
复制
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");
}

有谁能给我一点提示吗。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-06-02 08:35:48

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

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

public void setRotation(short rotation) 设置单元格中文本的旋转度。 以度表示。值从0到180不等。文本的第一个字母被认为是弧的中心点.对于0-90,该值表示地平线以上的度数.对于91-180度,视界以下的度计算为:视界以下的度= 90 - textRotation. 注意: HSSF使用从-90度到90度的值,而XSSF使用0到180度的值。此方法的实现将相应地在这两个值范围之间映射,但是相应的getter返回的值是应用此CellStyle的当前类型的Excel文件格式所规定的范围。

下面是你愿意做的事情的正确例子:

代码语言:javascript
复制
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));
        }
    }

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37569225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档