Java code not printing all rows from excel file - java

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

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

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

Apache POI blank values

I am using Apache POI to import data from excel file to database.(newbie to APACHE POI)
In which I am allowing user to select columns from excel sheet and Map those columns to the Database columns. After mapping the columns, when I try to insert the records from Excel to Database then:
If Columns with NO blank values in them are Mapped then Proper data is inserted into the database
If columns are Mapped with BLANK values in them, then if a Excel Cell has blank value then previous value of that column is assigned.
Source Code:
FileInputStream file = new FileInputStream(new File("C:/Temp.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file); //Get the workbook instance for XLS file
HSSFSheet sheet = workbook.getSheetAt(0); //Get first sheet from the workbook
Iterator<Row> rowIterator = sheet.iterator(); //Iterate through each rows from first sheet
while (rowIterator.hasNext())
{
HSSFRow hssfRow = (HSSFRow) rowIterator.next();
Iterator<Cell> iterator = hssfRow.cellIterator();
int current = 0, next = 1;
while (iterator.hasNext())
{
HSSFCell hssfCell = (HSSFCell) iterator.next();
current = hssfCell.getColumnIndex();
for(int i=0;i<arrIndex.length;i++) //arrayIndex is array of Excel cell Indexes selected by the user
{
if(arrIndex[i] == hssfCell.getColumnIndex())
{
if(current<next)
{
//System.out.println("Condition Satisfied");
}
else
{
System.out.println( "pstmt.setString("+next+",null);");
pstmt.setString(next,null);
next = next + 1;
}
System.out.println( "pstmt.setString("+next+","+((Object)hssfCell).toString()+");");
pstmt.setString(next,((Object)hssfCell).toString());
next = next + 1;
}
}
}
pstmt.addBatch();
}
I have look for similar questions on SO, but still not able to solve the issue.. So any help will be appreciated.
Thanks in advance..
You've made a very common mistake, which has been covered in rather a lot of past StackOverflow questions
As the Apache POI documentation on cell iterating says
In some cases, when iterating, you need full control over how missing or blank cells are treated, and you need to ensure you visit every cell and not just those defined in the file. (The CellIterator will only return the cells defined in the file, which is largely those with values or stylings, but it depends on Excel).
It sounds like you are in that situation, where you need to care about hitting every row/cell, and not just grabbing all the available cells without worrying about the gaps
You'll want to change you code to look somewhat like the example in the POI docs:
// Decide which rows to process
int rowStart = Math.min(15, sheet.getFirstRowNum());
int rowEnd = Math.max(1400, sheet.getLastRowNum());
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row r = sheet.getRow(rowNum);
int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);
for (int cn = 0; cn < lastColumn; cn++) {
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
// The spreadsheet is empty in this cell
// Mark it as blank in the database if needed
} else {
// Do something useful with the cell's contents
}
}
}

Reading Excel sheet column wise using apache poi

Can I read a excel sheet column wise using apache poi..?(without using row Iterator)
for (Row row : sheet) {
Cell firstCell = row.getCell(0);
// Printing Stuff
}
I know, the above one will do the same. But I need to get first column's data without using Row Iterator.
You can iterate over the sheet without using iterator
Workbook wb = WorkbookFactory.create(new FileInputStream("file.xls"));
Sheet sheet = wb.getSheetAt(0);
for (int j=0; j< sheet.getLastRowNum() + 1; j++) {
Row row = sheet.getRow(j);
Cell cell = row.getCell(0); //get first cell
// Printing Stuff
}

Categories