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.
Related
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
if i create a first sheet it is working fine. if i call the same method to create new sheet it will over ride the previous sheet.
After couple of method calls, it is showing only one sheet(last sheet).
In my code every time i am creating sheet only, not workbook every time.
could any body please explain.how to code it.
FileOutputStream fos = new FileOutputStream(outputString)
I am assuming you are using this. When you add the boolean "true" to this syntax like this:
FileOutputStream fos = new FileOutputStream(outputString, true);
Data will get added to the existing data. You should give it a try.
First Question on the community!
Let me explain a little the question.
I have a user interface, that allows me to create an excel file. And I want to add at that ui the a "print Document" button. This button is supposed to allow the user to print that excel document. So what I need is a way to call the windows printing dialog for that file, so the user can print the file.
I have read http://docs.oracle.com/javase/tutorial/2d/printing/printable.html, But I can not find the way of telling the printing dialog what file to print.
Thanks in advance!
I think Apache POI is the best thing for you. Code syntax is like
POIFSFileSystem filesystem = new POIFSFileSystem(new FileInputStream(file));
HSSFWorkbook wrkbook = new HSSFWorkbook(filesystem );
HSSFSheet sheet = wrkbook.getSheetAt(0);
HSSFRow row;
HSSFCell cell;
//Keep on doing your manipulation.
More about Apache POI.
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
I am interested and would like to learn more about java , how to write into existing excel sheets / manipulating the existing data. I was wondering if you could give me an idea on how to edit an existing excel file and save it using the jxl api / Apache POI
or perhaps give me a sample program on how to edit some data in an existing excel file and then save it
Thanks in advance !!
The tutorials here are very helpful and well-written. They use an external JAR developed by the Apache POI project.
Here's an simple example of editing one cell:
InputStream inp = new FileInputStream("wb.xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt([sheet index]);
Row row = sheet.getRow([row index]);
Cell cell = row.getCell([cell index]);
String cellContents = cell.getStringCellValue();
//Modify the cellContents here
// Write the output to a file
cell.setCellValue(cellContents);
FileOutputStream fileOut = new FileOutputStream("wb.xls");
wb.write(fileOut);
fileOut.close();
Hope it helps
One very important tip that I learned the hard way.
Open the OutputStream only after you have completed writing to your excel workbook. Zabbala's example is spot on and shows this correctly. If you open the OutputStream any earlier, your changes would not be written to the file after your program exits and you would be scratching your head as I did.
I refresh the formulas with another tab for this I use the next sentence
HSSFSheet worksheetse = workbook.getSheetAt(0);
worksheetse.setForceFormulaRecalculation(true);
but it's necesary that you apply the method setForceFormulaRecalculation for all the tabs that have the formulas.
Sorry for my English
Hello i have the same problem than neXGen. But strangely if i open the file with openoffice, it works!
Edit: perhaps i found a solution, put this after changing the values:
HSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);