4
votes

I have problems to format a cell with Apache poi 3.9 correctly. I got some english and some arabic Text in my table so I need to set the readingorder to right-to-left for some cells by using

ExtendedFormatRecord.setReadingOrder(2); doc

A HSSFCellStyle is created with

HSSFCellStyle(short index, ExtendedFormatRecord rec, HSSFWorkbook workbook) shown here

The Problem is, that the constructor is protected and the class is final. So I can't extend it. Is there a possibility to set the readingorder to right-to-left for a single cell? I don't need to set the style of the worksheet to rtl. Also this don't solve the Problem.

2
Does setting the read order fix it? i.e. is the problem with setting the order to 2, or with what to do instead? - Gagravarr
The problem is that I need to set the readingOrder to 2 but I think it's not possible. A workaround without changing the value would be fine. - klars
Short term, try something hacky with reflection, just to check it really solves it. If it does, we can help you find a cleaner way - Gagravarr
I can read an existing cellstyle from an excelfile which i created manually in office. This style will work fine then, because the readingorder is set to 2 (checked per debug). Thanks so far! - klars
Can you work out how to do it for .xlsx files too? The "proper" fix will mean doing something to handle both formats - Gagravarr

2 Answers

0
votes

This is how it works with reflection.

Constructor<HSSFCellStyle> con = HSSFCellStyle.class.getDeclaredConstructor(short.class,
    ExtendedFormatRecord.class, HSSFWorkbook.class);

con.setAccessible(true);

ExtendedFormatRecord eFR = new ExtendedFormatRecord();
short ro = 2;
eFR.setReadingOrder(ro);

short s = 0;
HSSFWorkbook generatedWb = new HSSFWorkbook();
HSSFCellStyle myStyle = con.newInstance(s, eFR, generatedWb);
0
votes

You just need to use a newer version of Apache POI!

The latest nightly builds (so future 3.11 beta 3 / 3.11 final) now include the method HSSFCellStyle.setReadingOrder(short) of which you search for