Is it possible to edit existing Excel formulas using POI - java

I have one excel template placed in my application's configuration folder. Now while downloading that excel template, I am trying to edit it's data and existing formulas using POI api. I am able to edit data easily.
How can I edit existing formulas? The formula which I am trying to edit, is attached with more than 1000 cells, so instead editing every cell and updating formula, I am trying to update that formula itself.
I am using following code to get workbook object
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File("ExcelTemplate.xlsx")));
XSSFSheet sheet = workbook.getSheetAt(5);
for(int i = 2; i<174; i++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell(0);
cell.setCellValue("My Data");
}
FileOutputStream output_file =new FileOutputStream(new File("UpdatedExcelTemplate.xlsx"));
workbook.write(output_file);
output_file.close();
Problem which I am facing is as following:
I have one sheet (say sheetA) in which I am inserting values at run time. And I am referring that list of data in another sheet (say sheetB) as drop down for any cells. Now as per data range in sheetA, my formula should get change.

Related

Keeping Pivot Table and Formating intact in JExcel

I am trying to write data to an excel sheet using JExcel. I have an excel file placed on disk and I opened it using JExcel and then created a Writable Workbook.
The Excel workbook contains sheets having pivot table. Idea is to just populate source sheets and then refresh the pivot tables. But after creating Writable Workbook, the formatting and pivot tables got away.
Here is my code snippet:
FileInputStream fileInputStream = new FileInputStream("C:\\Pivot_File_template.xls");
workbook = Workbook.getWorkbook(fileInputStream);
WorkbookSettings workbookSettings = new WorkbookSettings();
writableWorkbook = Workbook.createWorkbook(new File("C:\\Writable_Pivot_File.xls"),workbook,workbookSettings);
The file "Writable_Pivot_File.xls" just lost the orignal formatting and pivot tables. Any suggestions what I can do to maintain all the formatting and pivot tables? I am using "jxl_2.6.9.jar". Thanks in advance.
Just got away with this issue by using Apache POI. JExcel doesnot support xlsx and updating an xls file having pivot table using JExcel will disable the Pivot table and change its formatting.

Apache poi excel link to other page/sheet

I need to generate an excel report using java. So I'm planning to use Apache POI.
But there is one required which I'm not sure if its possible through apache POI.
Requesting you to please help me with this.
Requirement:
Excel doc should have multiple sheets say sheet1 and sheet 2.
sheet1 will be having multiple links.
sheet2 will be having 100's of columns.
Each link in sheet1 should be linked to one of the column of sheet2.
When user clicks on any of these links, it should take user to sheet2 and automatically focus on that column.
For ex. If user clicks on link pointing to 95th column of sheet2, then clicking on that link should open sheet2 and 95th column should be in focus.
Please let me know if its feasible. I searched through net but couldn't find any way of doing it.
Any pointer is really appreciated.
Well, I think you could use Hyperlinks to Sheet cells(if not columns) in the same document.
http://poi.apache.org/spreadsheet/quick-guide.html#Hyperlinks
Following is an sample extracted from above link-
To create links you could do something like:
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Cell cell;
Sheet sheet = wb.createSheet("Hyperlinks");
//URL
cell = sheet.createRow(0).createCell((short)0);
cell.setCellValue("Worksheet Link");
Hyperlink link2 = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
link2.setAddress("'Target Sheet'!A1");
cell.setHyperlink(link2);
cell.setCellStyle(hlink_style);
This will take you to a sheet named 'Target Sheet' and then to cell A1

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 copy one workbook sheet to another workbook sheet using apache POI and java [duplicate]

This question already has answers here:
Copying Excel Worksheets in POI
(6 answers)
Closed 3 years ago.
I have one excel file with single sheet (abstract model). Now I want to copy the sheet to another existing workbook. How can I do this?
For processing regular styles and data we could iterate through each cells.
But if we have formula enabled cell,non editable cells, merged cell then this is the better solution to go for:
I have tried something like copying sheets but it didn't worked.
In addition with that i need to copy a sheet in which there are some non editable cells and formula enabled cells too.
This solution worked for me:
1)copy the entire workbook
2)delete unnecessary sheets
3)add your new sheets to above book
//input source excel file which contains sheets to be copied
file = new FileInputStream("C:\\SamepleTemplate.xlsx");
workbookinput = new XSSFWorkbook(file);
//output new excel file to which we need to copy the above sheets
//this would copy entire workbook from source
XSSFWorkbook workbookoutput=workbookinput;
//delete one or two unnecessary sheets, you can delete them by specifying the sheetnames
workbook.removeSheetAt(workbook.getSheetIndex(workbook.getSheet(" your sheet name ")));
//if you want to delete more sheets you can use a for to delete the sheets
for (int index=0;index<=5;index++)
{
workbook.removeSheetAt(index);
}
//To write your changes to new workbook
FileOutputStream out = new FileOutputStream("C:\\FinalOutput.xlsx");
workbookoutput.write(out);
out.close();

Categories

Resources