I have written a code to access .xlsm file but it fails to do so. I have tried the same file using both XSSFWorkBook and the Generic WorkBook which is done using WorkBookFactory.
Scenario 1:
org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(new File(
"/filename.xlsm"));
Error:
The supplied spreadsheet seems to be an Encrypted .xlsx file. It must be decrypted before use by XSSF, it cannot be used by HSSF.
So I left the generic way of reading the file and used XSSF.
Scenario 2:
XSSFWorkbook workBook = new XSSFWorkbook(new FileInputStream(new
File("/fileName.xlsm")));
Error:
org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
Both the scenarios do work on some .xlsm files. Properties and data with working and non working files are the same.
Can someone help in fixing this.?
Promoting a comment to an answer... You need to upgrade your version of Apache POI!
You can view the Apache POI changelog here, and see quite how many changes there have been since 3.9 came out 3 years ago. It's quite a number of pages of fixes! And you need at least one of those...
Related
I am using aspose-cells-8.7.2-java. When I refresh the pivot table and save it, the excel file is getting corrupted. When I try to open the excel file I am getting the alert message as below :
"Excel found unreadable content in 'Book1.xlsx'.Do you want to recover the contents of this workbook?If you trust the source file of this workbook, click yes."
The code is as below :
Workbook wb = new Workbook("Book1.xlsx");
PivotTable pt = wb.getWorksheets().get(1).getPivotTables().get(0);
pt.refreshData();
pt.calculateData();
wb.save("Book1.xlsx");
Any help ?
I found this thread where the same issue is logged as a ticket :
http://www.aspose.com/community/forums/thread/683715/aspose.cells-generates-a-corrupted-xlsx-file-excel-2007-fails-to-open.aspx.
Is this issue solved?
I'm afraid the logged issue is not resolved yet. By the way, do you use similar Excel file or yours template file "Book1.xlsx" is different. Moreover, your issue can be template specific (if you are using different file) and might have different scenarios, so we need your template "Book1.xlsx" file to properly evaluate your issue on our end. We recommend you to kindly create a separate thread in Aspose.Cells forum with your template Excel file, we will evaluate your issue and help you better there.
I am working as Support developer/ Evangelist at Aspose.
I am using package xlsx Version:0.5.7 Date: 2014-08-01. in R version 3.0.1 (2013-05-16) -- "Good Sport" Platform: i386-w64-mingw32/i386 (32-bit).
I have an xlsx file with at least 2 sheets (say A and B). I need to read data from A, edit them and save them in B. This has to be done on a periodical base.
I am able to read data from A with read.xlsx. After editing the data frame I want to save it in an existing sheet B in the same xlsx file.
I try with this line
write.xlsx(down, paste0(root,'/registration reports/registration complete_WK.xlsx'), sheet="data_final", col.names=T, row.names=F, append=T, showNA=F)
but it give me this error:
Error in `.jcall(wb, "Lorg/apache/poi/ss/usermodel/Sheet;", "createSheet", ` :
java.lang.IllegalArgumentException: The workbook already contains a sheet of this name
I need to replace that existing sheet multiple times. How can I do that?
If you want to save your new dataframe in an existing excel file, you first have to load the xlsx-file:
wb <- loadWorkbook(file)
which sheets you have you'll get like this:
sheets <- getSheets(wb)
you can easily remove and add (and thus replace) sheets with:
removeSheet(wb, sheetName="Sheet1")
yourSheet <- createSheet(wb, sheetName="Sheet1")
than you can fill the sheets with dataframes:
addDataFrame(yourDataFrame, yourSheet, <options>)
addDataFrame(anotherDataFrame, yourSheet, startRow=nrow(yourDataFrame)+2)
and last step is saving the whole workbook as .xlsx:
saveWorkbook(wb, file)
btw: the documentation of the xlsx-package is really good and helpful on such questions :)
http://cran.r-project.org/web/packages/xlsx/xlsx.pdf
It may be that the Java installed on your computer is incompatible with the xlsx library. The following thread discusses a similar issue with regard to the same package:
enter link description here
Alternatively, your issue may be solved by a different Excel-related package, such as XLConnect. See this post:
enter link description here
I am trying to read data from a xls which is working fine using
java.io.File f1=new java.io.File("E:/SELENIUM DATA/First_P1/DATA_SHEET.xls");
w = Workbook.getWorkbook(f1);
wworkbook = Workbook.createWorkbook(f1,w);
after some time , if try to open xls , showing file is corrupted. help me.
By looking into your small piece of code it seems you are not following the correct way to read the excel sheet. If you just want to read the excel sheet then there is no need to use createWorkbook(f1,w) or If you want to write something on it then you must be doing something wrong that's why it corrupts your file.
Read/Write Excel sheet
The file should be closed properly once the data read/write from the sheet so that
the mentioned error message will not be prompted.
You can use below commands like
w.write();
w.close();
I'm generating an Excel file in Java with POI 3.2 (Latest version I can use for my client).
Here is my code. As you can see I'm using HSSF because I need to make a XLS file.
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Reporting");
sheet.setColumnWidth(250,250);
HSSFRow Row;
HSSFCell Cell;
//Content part (doesn't matter)
IWDResource resource = WDResourceFactory.createCachedResource(
wb.getBytes(),
"workbook.xls",
WDWebResourceType.XLS);
wdContext.currentContextElement().setXls(resource);
Now after I've downloaded the XLS file, I want to open it but the file seems to be corrupt.
Image on link: http://tinyurl.com/nop52sh
When I'm pressing 2 times on 'Don't send', de excell file opens in correct form.
Any idea why?
Don't call wb.getBytes(), it doesn't do what you want. From the getBytes() javadoc
Method getBytes - get the bytes of just the HSSF portions of the XLS file. Use this to construct a POI POIFSFileSystem yourself.
Instead, if you want the overall xls file as a byte array, do
ByteArrayOutStream baos = new ByteArrayOutStream();
wb.write(baos);
byte[] xlsBytes = baos.toByteArray();
Finally, Apache POI 3.2 is ancient, over 5 years old now! You really ought to upgrade, see the changelog for an idea of all the bugs fixed since then
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);