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
Related
I have an .xls file which I call it model. Now my probleme is that I want to create over 50 .xls file that have that same format of the model. So properly my question is : how do I create a worksheet (excel file) based on an excel template using java? Is there is any way to do it without copying the style between sheets ??
This example does not use a pre defined template but you can go check out my code and morph it into something worth for you.
https://github.com/ernst223/spread-sheet-exporter
So you can maybe code the style in my class and then use my class for all of your excel files
SpreadSheetExporter spreadSheetExporter = new SpreadSheetExporter(List<Object>, "Filename");
File fileExcel = spreadSheetExporter.getExcel();
as you didn't make any apparent effort before asking i will do the same
excel api
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 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...
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();
First of all thanks for the previous help.
What I want to do is the following.
Is to collect information through a small program that can capture in Java and writes it to a file which I have done.
Then to have this information passed to a preformatted excel spread sheet.
In the spread sheet it reads this data that is written and displays it in different tables and graphs.
Is this possible to automatically code or do I have to write to a csv file, from the csv file manually copy to an excel template?
If anybody has come across a solution to my problem I would appreciate a steer in the right direction
Hey try ths code in servlet or in JSP.
This will convert simple excel sheet on which you can do further operations..
response.setContentType("application/vnd.ms-excel");
PrintWriter out = response.getWriter();
try {
out.println("\tId \tName");
out.println("\t1\tabc");
out.println("\t2\txyz");
out.println("\t3\tpqr");
} finally {
out.close();
}
here ..
response.setContentType("application/vnd.ms-excel");
specify the excel sheet to be generated..
all "\t" separates new cell in the excel sheet.
On your requirement basis you can also change this code with database or any...
It's a duplicate of How to read and write excel file in java
you can use this API: http://poi.apache.org/
Or if you write one text file with tab separation between fields and save with (.XLS) extension, excel should read it. But off course the better solution is to use Apache POI HSSF.