首页 文章

XSSF Apache POI XSSFCellStyle

提问于
浏览
1

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

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输出文件列 Headers 不能旋转 . 下面的代码使用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 回答

  • 1

    这些都与XLS和XLSX表格和工作簿的格式有关 - 它们是不同的 .

    这是 setRotation()setRotation() 方法上的一部分:

    public void setRotation(短旋转)设置单元格中文本的旋转度 . 以度为单位 . 值的范围为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));
            }
        }
    

    }

相关问题