VLOOKUP_ADD_Array Apache POI - java

I'm using Apache POI to parse an excel document.
It works fine until I reach a cell whom has a VLOOKUP_ADD_ARRAY.
What my code does is:
XSSFWorkbook workbook = new XSSFWorkbook(httpOrder.getFile());
XSSFSheet sheet = workbook.getSheet("MySheet");
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_FORMULA:
CellValue value = workbook.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(cell)
break;
}
}
}
I'm getting the following error: org.apache.poi.ss.formula.FormulaParseException: Name 'VLOOKUP_ADD_ARRAY' is completely unknown in the current workbook
Can anyone help on this?

I assume VLOOKUP_ADD_ARRAY is a user defined function. It would help if you could post what is in the cell the error occurs for.
If it is indeed a user defined function, you have to reimplement it yourself. You can check this page for an example how to reimplement a user defined function.

Related

Java code to Split Workbook into multiple workbooks based on a cell value

I am looking for some help. I have an excel sheet A which has around 700 records. It has 5 columns where one of the column is Lifecycle. I want to split the file into two different files based on the Lifecycle cell value. For example if the cell value of Lifecycle column is X, put the record in A_X.xlsx file else put the records in A_Y.xlsx file.
I am using Apache POI.
Thanks in advance.
You can read from excel file using Workbook
Like this: Workbook workbook = WorkbookFactory.create(new File(EXCEL_FILE_PATH));
Then you need to get your sheet like this: Sheet sheet = workbook.getSheetAt(YOUR_SHEET_POSITION);
But remember that sheets position starts from 0.
Then you can iterate over rows and cells using Iterator, for-each, and Java 8 foreEach with Lamba
Like this:
Using Iterator
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// there you can check if cell.someValue == yourNeed to save on files that you need
if(cell.yourValue == SOMETHING) {
saveOnExcelFile(YOUR_EXCEL_FILE_PATH_ONE);
} else {
saveOnExcelFile(YOUR_EXCEL_FILE_PATH_TWO);
}
}
}
Using for-each
for (Row row: sheet) {
for(Cell cell: row) {
// there you can check if cell.someValue == yourNeed to save on files that you need
if(cell.yourValue == SOMETHING) {
saveOnExcelFile(YOUR_EXCEL_FILE_PATH_ONE);
} else {
saveOnExcelFile(YOUR_EXCEL_FILE_PATH_TWO);
}
}
}
Using Java 8 forEach with lamdas
sheet.forEach(row -> {
row.forEach(cell -> {
// there you can check if cell.someValue == yourNeed to save on files that you need
if(cell.yourValue == SOMETHING) {
saveOnExcelFile(YOUR_EXCEL_FILE_PATH_ONE);
} else {
saveOnExcelFile(YOUR_EXCEL_FILE_PATH_TWO);
}
});
});
And in the end, don't forget to close the workbook using workbook.close()

getCellType() always returns 0 (Numeric)

When I read an xls file with Apache POI, I always receive the value 0 from getCellType(). That is incorrect. I have other types of data inserted in the sheet. And I don't really understand why this is happening.
This is what my logger says every time it parses a cell:
Type = 0
Does anyone know or have a clue why is this happening?
HSSFWorkbook wb = new HSSFWorkbook(inputStream);
HSSFSheet sheet=wb.getSheetAt(0);
HSSFRow row;
HSSFCell cell;
Iterator rows = sheet.rowIterator();
while (rows.hasNext())
{
row=(HSSFRow) rows.next();
Iterator cells = row.cellIterator();
while (cells.hasNext())
{
cell=(HSSFCell) cells.next();
log4Debug.debug(" cellCollumnr = "+cell.getCellNum());
log4Debug.debug(" cellRow = "+row.getRowNum());
log4Debug.debug(" Type = "+cell.getCellType());
}
}

Java code not printing all rows from excel file

I have an excel file with 6663 rows. I want to read all the rows and columns in the excel file and print them out on my console in eclipse. Here is what I have tried to achieve this:
public class ExcelReader {
public static final String SAMPLE_XLSX_FILE_PATH = "K:\\Documents\\Project\\Netword_GUI\\Netword_GUI\\src\\libs\\cc2017.xlsx";
public static void main(String[] args) throws IOException, InvalidFormatException {
// Creating a Workbook from an Excel file (.xls or .xlsx)
Workbook workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH));
// Retrieving the number of sheets in the Workbook
System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
/*
=============================================================
Iterating over all the sheets in the workbook (Multiple ways)
=============================================================
*/
// You can obtain a sheetIterator and iterate over it
Iterator<Sheet> sheetIterator = workbook.sheetIterator();
System.out.println("Retrieving Sheets using Iterator");
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
//System.out.println(sheet.getRow(0));
System.out.println("=> " + sheet.getSheetName());
}
// Getting the Sheet at index zero
Sheet sheet = workbook.getSheetAt(0);
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
// You can obtain a rowIterator and columnIterator and iterate over them
System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// Now let's iterate over the columns of the current row
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue + "\t");
}
System.out.println();
}
if (sheet.getActiveCell() == null) {
// Closing the workbook
workbook.close();
}
}
}
The intention of this code is to display all the rows and columns. At the moment this code just shows roughly about 200 rows but all the columns for these rows are showing as intended. It also seems to be displaying the rows in a random order although, every time I run it the same rows show in the same random order. I would appreciate any solution in order to display all of the 6663 rows in the correct order including the headers (first row). Thank you in advance.
If you are running this inside Eclipse you are probably hitting the limit on the size of the Console output.
You can change the limit in the Preferences in the 'Run/Debug > Console' page. You can change the maximum number of characters or turn off the limiting altogether (but this can lead to out of memory errors).

Java Apache POI - Get Value of Cell to the Right of Cell Matching Regex?

I have an Excel spreadsheet that is full of label cells to the left of corresponding value cells. Like this:
E F
3 Invoice Number "ABC123"
4 Issue Date "2018-04-01"
5 Amount Due 298.43
I cannot rely on the values I want being in the same static cell location with each spreadsheet I process.
I want to use a regex to find the label cell (column E) that corresponds to the value I want (column F), then set the value to a variable.
I am refactoring into Java (using Apache POI library) legacy code originally written using Python. The Python code uses the following syntax to achieve the above goal:
for row in range(startrow, row+1):
if str(rowdata[0].lower().find("invoice number")) > -1:
invoice_number = rowdata[i+1]
I can't find equivalent functionality in the Apache POI documentation.
A pseudocode example of what I'm trying to do:
String invoiceNumber;
Iterator<Row> rowIterator = sheet.iterator;
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> = row.cellIterator();
while(cellIterator.hasNext()) {
if (cell.matches("invoice number") {
invoiceNumber = cell + 1
}
}
}
Any push in the right direction would be greatly appreciated.
String invoiceNumber ;
Iterator<Sheet> sheetIt = wb.sheetIterator();
while (sheetIt.hasNext()) {
Sheet sheet= sheetIt.next();
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIt = row.cellIterator();
while (cellIt.hasNext()) {
Cell cell = cellIt.next();
if(cell.getStringCellValue().equals("Invoice Number")) {
invoiceNumber= row.getCell(cell.getColumnIndex()+1).getStringCellValue();
}
}
}
}

how to read Blank cells AS String from Excel File Using Apache poi

I am new to Apache POI. I want to Read the blank cells as well.
This is what I tried to achieve my objective but it problem is that it doesn't continue reading after getting a blank cell.
here is my logic
HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row;
HSSFCell cell = null;
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext()) {
row = (HSSFRow) rows.next();
if (row.getRowNum() == 0) {
continue;
}
Iterator cells = row.cellIterator();
while(cells.hasNext()){
HSSFCell myCell = (HSSFCell) cells.next();
if(myCell.getCellType()!=Cell.CELL_TYPE_STRING){
myCell.setCellType(Cell.CELL_TYPE_STRING);
}
Any suggestions?
Blank cells are not returned in iterators. Quoting Poi's quick guide:
Note that a rowIterator and cellIterator iterate over rows or cells that have been created, skipping empty rows and cells.
Another option is to iterate through each cell with
row.getCell(i)
and check if null or its type is Cell.CELL_TYPE_BLANK
You can get the last and first row numbers from Sheet's methods
sheet.getFirstRowNum()
sheet.getLastRowNum()
Same for first and last column using row's methods
row.getFirstCellNum()
row.getLastCellNum()

Categories