2016-06-01 20 views
1

---XSSF ApacheのPOI XSSFCellStyle

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

同じコードがWRITEN 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

XLSとXLSXのシートとワークブックのフォーマットはすべて異なります。

は度で表さセル内のテキストの回転角度を設定し

public void setRotation(short rotation)

はここsetRotation()方法にJavaDocの一部です。値の範囲は0〜180です。 という最初の文字は、円弧の中心点とみなされます。 0〜90の場合、 の値は水平より上の度を表します。 91〜180の場合、度は 以下です。水平線は、[水平線の下の角度] = 90 - textRotationとして計算されます。

注:HSSFは-90〜90度の値を使用しますが、 XSSFは0〜180度の値を使用します。この メソッドの実装は、これらの2つの値の範囲に応じて対応しますが、 で指定された範囲の値が返されます。このCellStyleが適用される現在のタイプは です。だから、

、ここにあなたがしても構わないと思っているものの正しい例を示します

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

}

+0

@Basavarajは、私の答えが正しいかどうか - そう、最高の一人としてそれをマークしてください他のコミュニティーのメンバーも役に立つと思うかもしれません。 – xSAVIKx