Create an excel worksheet based on a predefined excel template - java

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

Related

Pivot table refresh and save code in aspose cells for java corrupts excel file

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.

Is there a uniform ExcelExtractor class and a factory for both xls and xlsx files?

Is there a common class and an implementation of the ExcelExtractor interface that handles, uniformly, extraction of text from xls and xlsx sources?
Maybe, something in ss package.
I am looking for something that would allow me to do something like, but by getting the right implementation from the factory, based on the file type.
Right now, I am having to explicitly use the org.apache.poi.hssf.extractor.ExcelExtractor
for the xls files and org.apache.poi.xssf.extractor.XSSFExcelExtractor for xlsx.
For example, explicit approach for xls:
InputStream inp = new FileInputStream(path);
HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
ExcelExtractor extractor = new ExcelExtractor(wb);
extractor.setFormulasNotResults(true);
extractor.setIncludeSheetNames(false);
String text = extractor.getText();
I can implement my own Factory, but before I do that I thought to ask to see if there is a common approach that handles both formats (that is what ss package is for).
Two options
First, if you really really want to stick with the old Apache POI text extractors, then use the ExtractorFactory class. That will identify the type, and create an extractor for you
However, the better option - Apache Tika. Tika builds on top of POI (and lots of others), and gives you plain text extraction (+detection +xhtml +more!) from a wide range of file formats. You'd just call Tika, ask for the text, and get it back no matter the type. See Tika examples like this one to get started

Using write.xlsx to replace an existing sheet with R package xlsx

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

reading data from xls, making xls file corrupted in selenium

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();

Converting java file to excel spread sheet

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.

Categories

Resources