Apache POI hyperlinks with sheets - java

I have a excel file which consists of two sheets
sheet1: consists of error_id and no of errors as column's names(both are integer
values).
sheet2: Error_Id and description are column's here.
When user clicks on error_id column value in sheet one we should automatically go to that particular error_id in sheet2 so user can find description there for that particular error id.
I want to automate it using java and apache POI, please suggest me with code sample's.
Please help me.

Related

Java POI excel paging sheets

I'm working on a excel workbook, using Java POI.
I have several sheets and I need to add Pagination to 4 of them.
The problem is :
How I am suppose to attribute a global pagination to these sheets ?
sheet.getFooter().setRight(HeaderFooter.page()); only works for ONE sheet.
Cordially.
The Excel file format mandates that a footer is always per sheet, even in Excel itself you have to set up the footer for each sheet separately as far as I know. So the Apache POI API reflects this and you need to set the footer for each sheet.
Naturally you can write a helper function where you pass in the Sheet object to not have to code it 4 times here.

How can I refer to Excel columns by their header names rather than indexes/keys in Selenium Webdriver with Java?

I just recently started a job writing automated tests using Selenium WebDriver and Java, but my previous experience was with Telerik Test Studio and C#. The transition is going very well for the most part, but I'm trying to find a way to set up data-driven tests in Selenium WebDriver similarly to how they can be set up using Test Studio's Local Data tab (minus the built-in UI, obviously).
I've set up my framework with Apache POI so that I can use Excel files for my data with each row representing a scenario made of different variables represented by the columns. The problem is that since columns tend to get added or reordered over time, I need a way to refer to any column by the name string in its top row header rather than by its key or index.
Test Studio makes this extremely easy in that once you've copied your spreadsheet to the Local Data tab, you can easily refer to the columns by their header name with something along the lines of...
if (...LocalData.PaymentMethod = "CreditCard")...
...where PaymentMethod is the name of the column and "CreditCard" is a potential value in a cell of that column.
Does anyone know of a way to refer to columns by their header name strings using Java in Selenium WebDriver? Any help would be greatly appreciated. Thanks!
I think you are messing up Selenium with data reading from excel sheet. Selenium can't do it - Apache POI does. So, I'll answer regarding POI usage.
You can retrieve column headers using CellReference utility class and column index:
String columnName = CellReference.convertNumToColString(columnIdex);
When you've found necessary column, you can read its cells:
for(Row r : sheet) {
Cell c = r.getCell(columnIdex);
// do what you need
}

Apache poi not finding a hidden sheet

I am trying an example to learn the Apache POI library. In the example I am using a Test.xls excel sheet which has the 3rd sheet as hidden.
So I am assuming that when I use "workbook.getNumberOfSheets()" the result will be including the hidden sheet, which in my case should be 3.
But, I keep getting 2 as the result. I want to know how can I find the actual number of sheets incase there is/are hidden sheet in the excel.
Since in the actual use case I will not know the excel before hand, I would like to know the total number of sheets.
Any help is appreciated and thank you in advance.

Read empty cell using Apache POI Event model

I have a huge excel file with tons of columns which looks like this :-
Column1 Column2 Column3 Column4 Column5
abc def ghi
mno pqr
......
The output generated by my code when I print all the values in excel is :-
abc;def;ghi;null;null
mno;pqr;null;null;null
So, If we look at the output above we can note that the cells where I left blank values were not picked up by the POI library. Is there a way in which I can get these values as null? Or a way to recognize that the values presented skipped blank cells?
Please note: I am not using the usermodel (org.apache.poi.ss.usermodel) but an Event API to process xls and xlsx files.
I am implementing HSSFListener and overriding its processRecord(Record record) method for xls files. For xlsx files I am using javax.xml.parsers.SAXParser and org.xml.sax.XMLReader.
I am using JDK7 with Apache POI 3.7. Can someone please help?
I have already seen this possible duplicate How to get an Excel Blank Cell Value in Apache POI? But this doesn't answer my question as I am using Event API.
Yes, it can be done, and there are several examples of it which ship with Apache POI. They all relate to Event based xls / xlsx -> CSV, which looks very close to what you're doing. That makes me worry you may be re-inventing the wheel...
For HSSF event model processing, the example you want to look at is XLS2CSVmra. That is powered by MissingRecordAwareHSSFListener
For XSSF event model, the example you need is XLSX2CSV

Using Apache POI can I stop users copying formatting?

I have made an excel sheet which is generated by Java. The cells can only accept certain values, depending on data validation done against lists on a separate sheet.
This all works great, but if a user copies some values from another cell and pastes it into the cell it avoids validation... is there any way to prevent this?
You can do this by setting data format for that cell.
style = wb.createCellStyle();
style.setDataFormat(wb.createDataFormat().getFormat("0.000%"));
which will percentage value.
For documentation goto:
Apache HSSF Doc

Categories