Bug 58043

Summary: POI will not allow negative rotation of text using XSSF but does work for HSSF
Product: POI Reporter: Marty Wilber <mwilber>
Component: XSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: enhancement    
Priority: P2    
Version: 3.12-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: Demonstrates inability to rotate text -90 degrees using XSSF

Description Marty Wilber 2015-06-16 19:46:15 UTC
Created attachment 32828 [details]
Demonstrates inability to rotate text -90 degrees using XSSF

I need to rotate text within a cell by -90 degrees.  This feature works for a HSSF xls spreadsheet with POI.  But it does not work for a XSSF xlsx spreadsheet with POI.  The text remains at 0 degrees rotation.

Attached is sample code that demonstrates the successful rotation using HSSF and the unsuccessful rotation using XSSF.

Please correct this.

Marty


import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestExcel
{

    private void createXls()
    {
        try
        {

            Workbook workbook = new HSSFWorkbook();
            FileOutputStream fileOut = new FileOutputStream("c:/rotated.xls");
            Sheet sheet1 = workbook.createSheet();
            Row row1 = sheet1.createRow((short) 0);

            Cell cell1 = row1.createCell(0);

            cell1.setCellValue("Successful rotated text.");

            CellStyle style = workbook.createCellStyle();
            style.setRotation((short) -90);

            cell1.setCellStyle(style);

            workbook.write(fileOut);
            fileOut.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    private void createXlsx()
    {
        try
        {

            Workbook workbook = new XSSFWorkbook();
            FileOutputStream fileOut = new FileOutputStream("c:/rotated.xlsx");
            Sheet sheet1 = workbook.createSheet();
            Row row1 = sheet1.createRow((short) 0);

            Cell cell1 = row1.createCell(0);

            cell1.setCellValue("Unsuccessful rotated text.");

            CellStyle style = workbook.createCellStyle();
            style.setRotation((short) -90);

            cell1.setCellStyle(style);

            workbook.write(fileOut);
            fileOut.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws Exception
    {

        TestExcel testExcel = new TestExcel();

        testExcel.createXls();
        testExcel.createXlsx();

    }
}
Comment 1 Michiel 2015-07-12 18:33:46 UTC
I'm not sure if this is a bug. The HSSFCellStyle.setRotation(...) method takes an number between 90 and -90 degrees, while the XSSFCellStyle.setRotation(...) method takes a number between 0 and 180 degrees. So -90 degrees for HSSF would be the same as 180 degrees for XSSF API.
Comment 2 Dominik Stadler 2015-09-20 11:12:17 UTC
Hmm, that's bad, so we have 
* Only document the difference in the javadoc, however this is not very good as it would break in strange ways when people migrate code from HSSF to XSSF
* Adjust XSSFCellStyle to expect the same value-range as HSSF, this will create backwards-incompatibilities and might break existing code...
* Maybe it is possible to allow both value ranges, i.e. simply map -90 to 180 for XSSF and 180 to -90 for HSSF. This way we should be able to handle this equal for both types without causing incompatibilities
Comment 3 Dominik Stadler 2016-01-03 13:30:57 UTC
In r1722716 we added some compatibility between HSSF and XSSF to map both ranges of values in both cases. We also updated javadoc somewhat to describe the different ranges that are used. The getters still return the "native" values to stay backwards compatible.