Reputation: 371
I have a problem of character encoding while using JExcel.
My app creates an excel document from a template and fills it with with data from a database (filled with current and previous sessions user-input) before sending it to the user.
In the final document, non-ASCII characters FROM THE TEMPLATE such as é
, è
, à
, or °
are not rendered properly (in the generated document, they appear properly in the template) and are instead replaced by �
while those from the database are properly encoded.
I use UTF-8 for user input (and output to the viewing layer) as well as database storage.
I use this code in the class that generates the file:
private void createFile(Arguments...)
throws IOException, BiffException, RowsExceededException, WriteException
{
File XLSFile = new File(MyPath);
WorkbookSettings XLSSettings = new WorkbookSettings()
XLSSettings.setEncoding(Constants.TEMPLATE_ENCODING)
// Constants.java is a class containing only app-wide constants declared as public static final
Workbook template = Workbook.getWorkbook(
new File(Constants.TEMPLATE_PATH));
WritableWorkbook userDocument =
Workbook.createWorkBook(XLSFile,template,XLSSettings);
template.close();
WritableSheet sheet = userDocument.getSheet(0);
...
Code that fills my workbook and sheet by creating new Labels and
adding them to my WritableSheet with sheet.add(Label)
...
userDocument.write();
userDocument.close();
}
Constants.TEMPLATE_ENCODING
has been set to "Cp1252"
as was suggested in this question: Encoding problem in JExcel but to no avail however.
Trying to change it to "UTF-8"
produced no visible change either.
The application works otherwise just fine at every level. I figured it might be a problem of setting the proper encoding when opening and copying the template and tried to change this line
Workbook template = Workbook.getWorkbook(new File(Constants.TEMPLATE_PATH);
to
Workbook template = Workbook.getWorkbook(new File(Constants.TEMPLATE_PATH, XLSSettings);
but it produces an ArrayOutOfBoundException in java.lang.System.arraycopy
propagating from this line userDocument.write();
via
java.lang.ArrayIndexOutOfBoundsException
java.lang.System.arraycopy(Native Method)
jxl.biff.StringHelper.getBytes(StringHelper.java:127)
jxl.write.biff.WriteAccessRecord.<init>(WriteAccessRecord.java:59)
jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:726)
com.mypackage.MyClass.createFile(MyClass.java:337)
Anyone ever encountered the problem and know how to fix it ?
Upvotes: 2
Views: 3217
Reputation: 81
I was facing this problem too. The solution, for me, was very easy. I just had to put my WorkbookSettings just in the TEMPLATE wb and not in the new file.
//Load template workbook with settings
WorkbookSettings ws = new WorkbookSettings();
ws.setEncoding("Cp1252");
Workbook templateWorkbook = Workbook.getWorkbook(this.context.getAssets().open("template.xls"), ws);
//Create new workbook from templateWorkbook without settings
this.workbook = Workbook.createWorkbook(new File(this.location), templateWorkbook);
Found at: Android and JXL : ArrayIndexOutOfBoundException when create WritableWorkbook
Regards
Upvotes: 7