apache poi cellIterator skips blank cells but not in first row - apache-poi

I am creating a java program to read an excel sheet and create a comma separated file. When I run my sample excel file, with blank columns, The first row works perfectly, but the rest of the rows skip the blank cells.
I have read about the code changes required to insert blank cells into the rows, but my question is why does the first row work ????
public ArrayList OpenAndReadExcel(){
FileInputStream file = null;
HSSFWorkbook workBook = null;
ArrayList <String> rows = new ArrayList();
//open the file
try {
file = new FileInputStream(new File("Fruity.xls"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Could not open Input File");
e.printStackTrace();
}
// open the input stream as a workbook
try {
workBook = new HSSFWorkbook(file);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Can't Open HSSF workbook");
e.printStackTrace();
}
// get the sheet
HSSFSheet sheet = workBook.getSheetAt(0);
// add an iterator for every row and column
Iterator<Row> rowIter = sheet.rowIterator();
while (rowIter.hasNext())
{
String rowHolder = "";
HSSFRow row = (HSSFRow) rowIter.next();
Iterator<Cell> cellIter = row.cellIterator();
Boolean first =true;
while ( cellIter.hasNext())
{
if (!first)
rowHolder = rowHolder + ",";
HSSFCell cell = (HSSFCell) cellIter.next();
rowHolder = rowHolder + cell.toString() ;
first = false;
}
rows.add(rowHolder);
}
return rows;
}
public void WriteOutput(ArrayList<String> rows) {
// TODO Auto-generated method stub
PrintStream outFile ;
try {
outFile = new PrintStream("fruity.txt");
for(String row : rows)
{
outFile.println(row);
}
outFile.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-----
my Input in .xls file (Sorry don't know how to insert an excel table here )
Name >>>>>>>>>> Country of Origin >>>>>>>>> State of origin >>>>>>> Grade>>>>>> No of months
Apple >>>>>>>> USA >>>>>>>>>>>>>>>>>>>>>> Washington >>>>>>>>>>>>>> A >>>>>>>>> 6
orange >>>>>> USA >>>>>>>>>>>>>>>>>>>>>> Florida >>>>>>>>>>>>>>>>> A >>>>>>>>> 9
pineapple>>>>> USA >>>>>>>>>>>>>>>>>>>>>> Hawaii >>>>>>>>>>>>>>>>>> B >>>>>>>>> 10
strawberry>>>> USA >>>>>>>>>>>>>>>>>>>>>> New Jersey>>>>>>>>>>>>>> C >>>>>>>>>> 3
my output text file
Name ,Country of Origin,State of origin,,,Grade,No of months
Apple,USA,Washington,A,6.0
orange,USA,Florida,A,9.0
pineapple,USA,Hawaii,B,10.0
strawberry,USA,New Jersey,C,3.0
Notice the two extra commas before the Grade column... This is because I have two blank columns there.<br/>
These extra commas are missing in the rest of the output.
I am using Apache Poi-3.9-20121203.jar

You should have a read through the Iterating Over Rows and Cells documentation on the Apache POI website.
The CellIterator will only return cells that have been defined in the file, which largely means ones with either values or formatting. The excel file format is sparse, and doesn't bother storing cells which have neither values nor formatting.
For your case, you must have formatting applied to the first row, which causes them to show up.
You need to read through the documentation and switch to lookups by index. That will also allow you full control over how blank vs never used cells are handled in your code.

CellIterator does not iterate over cells which do not have any formatting or value applied.
First row must have had at least value or formatting applied.
If you want to read such cells as well you need to address it directly by specifying cell number
row.getCell(cellNumber, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
Using Row.MissingCellPolicy.CREATE_NULL_AS_BLANK returns it as a blank cell.

Related

POI: Using an Excel template that has conditional formatting

I'm new to using Apache Poi.
I had some questions about using an existing xlsx file as a template when it has conditional formatting.
The template file opens with no errors. I can enter data into it manually and the conditional formatting works by highlighting columns and changing fonts for specific cells in the spreadsheet.
I am using Poi to add data to the file.
I open the template add data to the appropriate cells, save the file as a new file.
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(xlsTemplate);
XSSFWorkbook workbook = XSSFWorkbookFactory.createWorkbook(inputStream);
var tmpSheet = workbook.getSheetAt(0);
int rowCount = tmpStartRow;
for(var tmpRow : tmpList) {
Row row = tmpSheet.getRow(rowCount++);
int columnCount = tmpStartCol;
var tmpMap = tmpRow.objectToMap();
int x = columnCount;
for(var entry : tmpMap.entrySet()) {
Cell cell = row.getCell(x);
var value = "";
if(entry.getValue() != null)
value = (String)entry.getValue();
try {
var dblVal = Double.valueOf(value);
cell.setCellValue(dblVal);
}catch(Exception e) {
cell.setCellValue(value);
}
x++;
}
}
var evaluator = workbook.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateAll();
workbook.setForceFormulaRecalculation(true);
inputStream.close();
try (FileOutputStream outputStream = new FileOutputStream(xlsFileName)) {
workbook.write(outputStream);
workbook.close();
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
When I open the new file, Excel does not give any errors.
After reviewing the worksheet, the cells have data in the appropriate fields, but the formatting from the template has not been set.
When I click on "Manage Rules" in the conditional formatting section, I see the rules are still there.
When I click on a cell in the spreadsheet and click on the formula bar and then click away, the conditional formatting gets triggered.
How do I get Poi to handle the conditional formatting for me? Do I need to modify the conditional formatting even though it already exists in the template?
Thanks in advance for your help..
SA

How to make row dynamic in Data Driven Framework

I am working on a Data Driven Project where my test data is from an excel sheet. Am able to fetch data from sheet, but I need to pass only the column value but not Row value in step defn method, so that my test case can complete one set of testing with one row and move to next row.
My code:
public static int getCellData() throws Exception{
int rowData=0;
int NumOfRows=0;
try{
NumOfRows = ExcelWSheet.getPhysicalNumberOfRows();
for(int i=2;i<NumOfRows;i++){
rowData = ExcelWSheet.getRow(i).getPhysicalNumberOfCells();}
}
catch (Exception e){}
return rowData;
}

string automatically converted in spring

I'm working on a project in Spring using SpringMVC. I'm importing data from (.xls) files .
the problem is that:
I'm reading this value "945854955" as a String but saved in DB as "9.45854955E8"
this value "26929" saved as "26929.0"
this value "21/05/1987" saved as "31918.0"
/read Code
// import ...
#RequestMapping(value="/read")
public String Read(Model model,#RequestParam CommonsMultipartFile[] fileUpload)
throws IOException, EncryptedDocumentException, InvalidFormatException {
List<String> liste = new ArrayList();
Employe employe = new Employe();
String modelnom = null;
liste = extraire(modelnom); //See the second code
for (int m=0, i=29;i<liste.size();i=i+29) {
if(i % 29 == 0) {
m++;
}
employe.setNomEmploye(liste.get(29*m+1));
//...
employe.setDateNaissance((String)liste.get(29*m+8).toString()); // here i had the date problem
employe.setDateEntree((String)liste.get(29*m+9).toString()); // here i had the date problem
employe.setDateSortie((String)liste.get(29*m+10).toString()); // here i had the date problem
// ...
employe.setNumCpteBanc(liste.get(29*m+17)); // here i had the first & second case problem
employe.setNumCIMR(liste.get(29*m+19)); // here i had the first & second case problem
employe.setNumMUT(liste.get(29*m+20)); // here i had the first & second case problem
employe.setNumCNSS(liste.get(29*m+21)); // here i had the first & second case problem
boolean bool=true;
List<Employe> employes = dbE.getAll();// liste des employes
for (int n=0;n<employes.size();n++) {
if (employes.get(n).getMatriculeMY() == (int)mat ) {
bool= false;
}
}
if (bool) {
dbE.create(employe);
}
}
return "redirect";
}
extraire code
private List<String> extraire (String nomFichier) throws IOException {
List<String> liste = new ArrayList();
FileInputStream fis = new FileInputStream(new File(nomFichier));
HSSFWorkbook workbook = new HSSFWorkbook(fis);
HSSFSheet spreadsheet = workbook.getSheetAt(0);
Iterator < Row > rowIterator = null;
// recup une ligne
rowIterator = spreadsheet.iterator();
while (rowIterator.hasNext()) {
int i = 0;
row = (HSSFRow) rowIterator.next();
Iterator < Cell > cellIterator = row.cellIterator();
while ( cellIterator.hasNext()) {
Cell cell = cellIterator.next();
i++;
/**
* Pour verifier si une ligne est vide. (for verifing if the line is empty)
*/
if (i % 29 == 0 || i == 1) {
while ( cellIterator.hasNext() && cell.getCellType() == Cell.CELL_TYPE_BLANK) {
cell = cellIterator.next();
}
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
String cellule = String.valueOf(cell.getNumericCellValue());
liste.add(cellule);
break;
case Cell.CELL_TYPE_STRING:
liste.add(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BLANK:
cellule = " ";
liste.add(cellule);
break;
}
}
}
fis.close();
return liste;
}
}
Excel's tries to data type cells and sometimes when you explicitly specify the data type Excel may try and cast the cell. You can try to right click on the cell and select 'Format Cell', then select 'Text' as the type (Category). However, at parse time it may still get hosed up.
Your quickest solution might be to save the file as a CSV and use that. You can still edit it in Excel. Although you will need to do some validation to ensure Excel isn't trying to do the above conversions on CSV save as. There are a lot of good Java CSV parsers out there OpenCSV, Super CSV.
The most time consuming, but probably the most correct way, if you want to continue to use Excel, is build a middle ware layer that parses the row and correctly identifies and formats the cell values. Apache POI and HSSF & XSSF can be used. Be warned that to handle xls and xlsx requires two different sets of libraries and often enough abstraction to handle both.
See https://poi.apache.org/spreadsheet/
As an Example:
protected String getCellValue(final Cell cell){
if (null == cell) { return null; }
// For Excel binaries 97 and below, The method of setting the cell type to CELL_TYPE_STRING converts the
// Formatted to date to a short. To correct this we check that the cell type is numeric and the check that it is
// date formatted. If we don't check that it is Numeric first an IllegalAccessorException is thrown.
if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC && isCellDateFormated(cell) {
// isCellDateFormated is seperate util function to look at the cell value in order to determine if the date is formatted as a double.
// is a date format.
return // do date format procedure.
}
cell.setTypeCell(Cell.CELL_TYPE_STRING);
return cell.toString();
}
Hope this helps.
============Update==================
Instead of calling methods like "getNumericCellValue()" try setting the cell type to String and using toString like the example above. Here is my test code.
Note the xls file has one row and 4 cells in csv: "abba,1,211,q123,11.22"
public void testExtract() throws Exception{
InputStream is = new FileInputStream("/path/to/project/Test/src/test/java/excelTest.xls");
HSSFWorkbook wb = new HSSFWorkbook(is);
HSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> rowIter = sheet.iterator();
while (rowIter.hasNext()){
HSSFRow row = (HSSFRow) rowIter.next();
Iterator<Cell> cellIter = row.cellIterator();
while (cellIter.hasNext()){
Cell cell = cellIter.next();
System.out.println("Raw to string: " + cell.toString());
// Check for data format here. If you set a date cell to string and to string the response the output is funky.
cell.setCellType(Cell.CELL_TYPE_STRING);
System.out.println("Formatted to string: " + cell.toString());
}
}
is.close();
}
Output is
Raw to string: abba
Formatted to string: abba
Raw to string: 1.0
Formatted to string: 1
Raw to string: 211.0
Formatted to string: 211
Raw to string: q1123
Formatted to string: q1123
Raw to string: 11.22
Formatted to string: 11.22

Input/Export data through MS Excel into OATS Scripts

When trying to get the Input the data from the Excel Sheet while working with OATS tool, it always gets into the catch block of the function. The below is the script written. Please help us resolve this issue.
public String getInputfromExcel(int argColumnNumber,int argRowNumber)throws Exception
{
String inputExcelName = dataPath+".xlsx";
String cellContent = "12";
try
{
Workbook workbook = Workbook.getWorkbook(new File(inputExcelName));
Sheet sheet = workbook.getSheet(0);
Cell a1 = sheet.getCell(argColumnNumber, argRowNumber);
cellContent = (a1.getContents()).toString();
System.out.println(cellContent.toString());
workbook.close();
}
catch (Exception e)
{
addReport("Getting Input From Excel", "Fail","Exception while reading value from excel sheet");
}
return cellContent;
}
Axel has brought up the point. On a further note, if I remember correctly, the function sheet.getCell(arg1, arg2) has first argument as rowNumber and 2nd as columnNumber (both the values are 0 based index).
Its old quetion....but just posting answer it might be helpful for someone needy.
In Oracle Application Testing Suite. NO NEED of external JARs to read/write data.
You can enable DataTable module in the tool
Complete explanation given here, http://www.testinghive.com/how-to-read-write-excel-in-oats/
//Define Sheet name to be read, and provide comma seperated to read multiple sheets
String sheetName = "Sheet1";
//Mention excel sheet path
String strFile= "C:\\Demo\\test.xls";
//Defined array list to add Sheets to read
List sheetList = new ArrayList();
sheetList.add(sheetName);
// Iports Sheet1
datatable.importSheets(strFile, sheetList, true, true);
//get rowcount
info("Total rows :"+datatable.getRowCount());
int rowcount=datatable.getRowCount();
//Loop to read all rows
for (int i=0;i<rowcount;i++)
{
//Set current row fromw here you need to start reading, in this case start from first row
datatable.setCurrentRow(sheetName, i);
String strCompany=(String) datatable.getValue(sheetName,i,"Company");
String strEmpFName=(String) datatable.getValue(sheetName,i,"FirstName");
String strEmpLName=(String) datatable.getValue(sheetName,i,"LastName");
String strEmpID=(String) datatable.getValue(sheetName,i,"EmpID");
String strLocation=(String) datatable.getValue(sheetName,i,"Location");
//prints first name and last name
System.out.println("First Name : "+strEmpFName+", Last Name : "+strEmpLName);
//Sets ACTIVE column in excel sheet to Y
String strActive="Y";
datatable.setValue(sheetName, i, datatable.getColumn(sheetName, datatable.getColumnIndex("Active")), strActive);
}
//Updates sheet with updated values ie ACTIVE column sets to Y
datatable.exportToExcel("C:\\Demo\\test1.xlsx");

apache poi how to disable external reference or external links?

I've been looking on the web for 30 minutes now and can't find any explanation about that. Here is my problem :
I wrote an application with poi to parse some data from 200 excel files or so and put some of it into a new file. I do some cell evaluation with FormulaEvaluator to know the content of the cells before choosing to keep them or not.
Now, when i test it on a test file with only values in the cells, the program works perfectly but when i use it on my pile of files I get this error :
"could not resolve external workbook name"
Is there any way to ignore external workbook references or set up the environment so that it wont evaluate formula with external references?
Because the ones I need don't contain references...
Thank you
Can you not just catch the error, and skip over that cell?
You're getting the error because you've asked POI to evaluate a the formula in a cell, and that formula refers to a different file. However, you've not told POI where to find the file that's referenced, so it objects.
If you don't care about cells with external references, just catch the exception and move on to the next cell.
If you do care, you'll need to tell POI where to find your files. You do this with the setupEnvironment(String[],Evaluator[]) method - pass it an array of workbook names, and a matching array of evaluators for those workbooks.
In order for POI to be able to evaluate external references, it needs access to the workbooks in question. As these don't necessarily have the same names on your system as in the workbook, you need to give POI a map of external references to open workbooks, through the setupReferencedWorkbooks(java.util.Map<java.lang.String,FormulaEvaluator> workbooks) method.
I have done please see below code that is working fine at my side
public static void writeWithExternalReference(String cellContent, boolean isRowUpdate, boolean isFormula)
{
try
{
File yourFile = new File("E:\\Book1.xlsx");
yourFile.createNewFile();
FileInputStream myxls = null;
myxls = new FileInputStream(yourFile);
XSSFWorkbook workbook = new XSSFWorkbook(myxls);
FormulaEvaluator mainWorkbookEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
XSSFWorkbook workbook1 = new XSSFWorkbook(new File("E:\\elk\\lookup.xlsx"));
// Track the workbook references
Map<String,FormulaEvaluator> workbooks = new HashMap<String, FormulaEvaluator>();
workbooks.put("Book1.xlsx", mainWorkbookEvaluator);
workbooks.put("elk/lookup.xlsx", workbook1.getCreationHelper().createFormulaEvaluator());
workbook2.getCreationHelper().createFormulaEvaluator());
// Attach them
mainWorkbookEvaluator.setupReferencedWorkbooks(workbooks);
XSSFSheet worksheet = workbook.getSheetAt(0);
XSSFRow row = null;
if (isRowUpdate) {
int lastRow = worksheet.getLastRowNum();
row = worksheet.createRow(++lastRow);
}
else {
row = worksheet.getRow(worksheet.getLastRowNum());
}
if (!isFormula) {
Cell cell = row.createCell(row.getLastCellNum()==-1 ? 0 : row.getLastCellNum());
cell.setCellValue(Double.parseDouble(cellContent));
} else {
XSSFCell cell = row.createCell(row.getLastCellNum()==-1 ? 0 : row.getLastCellNum());
System.out.println(cellContent);
cell.setCellFormula(cellContent);
mainWorkbookEvaluator.evaluateInCell(cell);
cell.setCellFormula(cellContent);
// mainWorkbookEvaluator.evaluateInCell(cell);
//System.out.println(cell.getCellFormula() + " = "+cell.getStringCellValue());
}
workbook1.close();
myxls.close();
FileOutputStream output_file =new FileOutputStream(yourFile,false);
//write changes
workbook.write(output_file);
output_file.close();
} catch (Exception e) {
e.printStackTrace();
}
}

Resources