Reading Excel sheet column wise using apache poi - java

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
}

Related

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

How to loop over values of a certain column in an excel sheet to get the index of a value using java apache poi

I am still new to Java trying only to loop over an excel column.
The Excel sheet:
I want to have in HashMap or Array so that I can compare these dates later on with another column.
My Code is giving me yet a string as you see in the screenshot:
How can I change this String to a another Data structure to access the "index" of the values. Something like dates.get(0) --> So 01-Jul-2018. I would change the time format later.
The Code section:
XSSFWorkbook workbook = new XSSFWorkbook(fsIP);
XSSFSheet sheet = workbook.getSheetAt(0);
for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row != null) {
Cell cell = row.getCell(0); // getColumn(0)
if (cell != null) {
System.out.print(cell);
}
}
}
What would you like to know further from me?
I would be thankful for every help!

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

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
}
}
}

Categories