POI overriding the old excel sheet with new excel sheet - java

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.

Related

Copying excel sheet in java with Apache POI doesnt conserve the orginal format

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 .

Selenium - store web element id in a spreadsheet, call the id from the spreadsheet in test?

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.

How to write to an existing file using SXSSF?

I have an .xlsx file with multiple sheets containing different data. Of all the sheets one sheet needs to accommodate close to 100,000 rows of data, and the data needs to be written using Java with poi.
This seems quite fast and simple with SXSSFWorkbook, where I can keep only 100 rows in memory, but the disadvantage is that I can only write to a new file (or overwrite existing file).
Also, I am not allowed to 'load' an existing file, i.e
SXSSFWorkbook wb = new SXSSFWorkbook(file_input_stream) is not allowed.
I can use Workbook factory:
Workbook workbook = new SXSSFWorkbook();
workbook = WorkbookFactory.create(file_input_stream);
but when the time comes for me to flush the rows,
((SXSSFSheet)sheet).flushRows(100);
I get the error that type conversion is not allowed from XSSFSheet to SXSSFSheet.
I tried to see if there was any way to copy sheets across different workbooks, but so far it seems it has to be done cell by cell.
Any insights on how to approach this problem?
You are probably having a template to which you want to add large data. You need to use the SXSSFWorkbook(XSSFWorkbook) constructor:
XSSFWorkbook wb = new XSSFWorkbook(new File("template.xlsx"));
SXSSFWorkbook wbss = new SXSSFWorkbook(wb, 100);
Sheet sheet = wbss.createSheet("sheet1");
// now add rows to sheet

How to print excel file from java?

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.

Edit existing excel files using jxl api / Apache POI

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

Categories

Resources