How to set Text Orientation in Excel Cells
Java Excel API is an open source java library to read, write and modify Excel spread sheets.
This requires the library jxl-2.6.12.jar to be in classpath.
The following example shows how to set Cell Orientation in Excel Spread sheet.
package com.bethecoder.tutorials.jexcelapi.write;
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.format.Orientation;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class CellOrientationTest {
/**
* @param args
* @throws IOException
* @throws IOException
* @throws WriteException
* @throws BiffException
*/
public static void main ( String [] args ) throws IOException, WriteException {
//Creates a writable workbook with the given file name
WritableWorkbook workbook = Workbook.createWorkbook ( new File ( "C:/JXL/CellOrientation.xls" )) ;
WritableSheet sheet = workbook.createSheet ( "My Sheet" , 0 ) ;
/**
* Add Orientation cells
*/
addCell ( sheet, Orientation.VERTICAL, 0 , 0 , "Vertical Orientation" ) ;
addCell ( sheet, Orientation.PLUS_90, 1 , 0 , "Plus 90 Orientation" ) ;
addCell ( sheet, Orientation.MINUS_90, 2 , 0 , "Minus 90 Orientation" ) ;
addCell ( sheet, Orientation.PLUS_45, 3 , 0 , "Plus 45 Orientation" ) ;
addCell ( sheet, Orientation.MINUS_45, 6 , 0 , "Minus 45 Orientation" ) ;
addCell ( sheet, Orientation.STACKED, 7 , 0 , "Stacked Orientation" ) ;
//Writes out the data held in this workbook in Excel format
workbook.write () ;
//Close and free allocated memory
workbook.close () ;
}
private static void addCell ( WritableSheet sheet,
Orientation orientation,
int col, int row, String desc ) throws WriteException {
WritableCellFormat format = new WritableCellFormat () ;
format.setOrientation ( orientation ) ;
Label label = new Label ( col, row, desc, format ) ;
sheet.addCell ( label ) ;
}
}
It gives the following output,