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 ?
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
Related
I'm using Apache Poi in java to copy an excel sheet to the same workbook This way :
FileInputStream file = new FileInputStream(new File("exemple.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet_copy = workbook.cloneSheet(0);
int num = workbook.getSheetIndex(sheet_copy);
workbook.setSheetName(num, "copy_file");
FileOutputStream outputStream = new FileOutputStream("exemple.xlsx");
workbook.write(outputStream);
the file "sheet_copy" is created as well , but it doesn't have the same scale as the first one ,
The first one has 59% and the second one 100% when i try to save it as pdf it's too much big, how to solve that in code ? i mean that sheet_copy gets the same format as the original one , i don't want to do it manually .
I'm working on java, i have an excel file named "file.xlsx" that i want to convert to pdf using Spire.XLS , this is the code i wrote for that :
//Create a Workbook instance
workbook = new Workbook();
//Load an Excel file
workbook.loadFromFile("file.xlsx");
//Get the first worksheet
sheet = workbook.getWorksheets().get(0);
//Add a new worksheet to the workbook
sheet_copy = workbook.getWorksheets().add("Copy of sheet1");
//Copy the first worksheet to the new worksheet
sheet_copy.copyFrom(sheet);
//Save the result file
workbook.saveToFile("file.xlsx");
workbook.saveToFile("result.pdf", com.spire.xls.FileFormat.PDF);
The pdf file is generated but with a signature : Evaluation Warning : The document was created with Spire.XLS for Java , i'm wondering about how to remove it , any idea ?
Either Request a pro Trial License OR
Per the age old answer use the free version
Free version doesn't have evaluation warning on result file.
Please make sure the version you were using is correct,
https://www.e-iceblue.com/forum/evaluation-warning-the-document-was-created-with-spire-pdf-t4925.html
https://www.e-iceblue.com/Download/xls-for-java-free.html
or for .net users
https://www.e-iceblue.com/Download/download-excel-for-net-free.html
So I'm trying to store a web element such as id in an excel spreadsheet, I then want to call the id from the sheet and place it in a test.
Would this work? The theory is then that others with less tech knowledge can modify tests within my workplace without editing code.
I am using the following code to read from the spreadsheet and place the cell data into a string.
File src=new File("C:\\Users\\Admin\\Documents\\ExcelData\\TestData.xlsx");
FileInputStream fis=new FileInputStream(src);
XSSFWorkbook wb=new XSSFWorkbook(fis);
XSSFSheet sheet1=wb.getSheetAt(0);
String data0=sheet1.getRow(0).getCell(0).getStringCellValue();
The difficulty I am having is when testing for example.
driver.findElement(By.name("q")).sendKeys("Java");
if I replace this with
driver.findElement(By.name(data0).sendKeys("Java");
this will not work.
Yes, you can get the input data from the external sheet as you asked.
This can be done using Apache POI Jars support.
Hope you are coding in Java.
The below link gives you enough code to your requirement. ->
Apache POI Examples
Code to read the data from Excel.
File file= new File(PATH);
FileInputStream inputStream = new FileInputStream(file);
Workbook workbook =new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheet(SHEETNAME);
String[][] number=new String[10][10]; // temp array
for(int i=0;i<rowCount+1;i++)
{
Row row=s.getRow(i);
for(int j=0;j<=row.getLastCellNum();j++)
{
number[i][j]= row.getCell(j).toString(); // works only for cells of string type
}
}
Then you fetch data from number array.
I have this piece of code to read from an xls file:
fileName = "...."
WorkbookSettings settings = new WorkbookSettings();
settings.setEncoding("Cp1252");
System.out.println("BEFORE");
Workbook w = Workbook.getWorkbook(new File(fileName), settings);
Sheet sheet = w.getSheet(1);
System.out.println("AFTER");
This is what I get in the console:
BEFORE
Warning: Text Object on sheet "Detalle" not supported - omitting
jxl.common.AssertionFailed
at jxl.common.Assert.verify(Assert.java:37)
at jxl.read.biff.SheetReader.handleObjectRecord(SheetReader.java:1811)
at jxl.read.biff.SheetReader.read(SheetReader.java:1059)
at jxl.read.biff.SheetImpl.readSheet(SheetImpl.java:716)
at jxl.read.biff.WorkbookParser.getSheet(WorkbookParser.java:257)
at MapMovInfoResource.postService(MapMovInfoResource.java:77)
The problem comes when I try to open the second sheet in that file. When I use the first sheet (w.getSheet(0)), it works fine.
Any ideas on how to solve this?
enter image description here
Its seem you have some value in excel which are not text. Please check excel value like in image, and if and special quotes are available then remove that and try again.
I have an excel sheet with some non-english characters in it and when I try to grab the contents via
sheet.getColumn(column)[row].getContents()
It returns the string with the replacement character \uFFFD instead of the non-english character which I was going to then translate to unicode using StringEscapeUtils.escapeJava.
//"L\u00F6schen" - correct
return StringEscapeUtils.escapeJava("Löschen");
//"L\uFFFDschen" - incorrect
return StringEscapeUtils.escapeJava(sheet.getColumn(column)[row].getContents());
//"L�schen" - incorrect
System.out.print(sheet.getColumn(column)[row].getContents());
This was really frustrating and it seems that jexcelapi is missing a lot of support.
Went with Apache POI instead and it worked great with no issues.
Try to set encoding through WorkbookSettings when initializing Workbook.
For example:
WorkbookSettings settings = new WorkbookSettings();
settings.setEncoding("Your java charset name");
Workbook workbook = Workbook.getWorkbook(source, settings);
Then getContents() method should correct content of cell