I am using Apache POI java and want to get the total number of rows which are not empty. I successfully processed a whole row with all its columns. Now I am assuming that I get an excel sheet with multiple rows and not a single row...so how to go about that? I was thinking of getting total number of rows (int n) and then loop until i<=n but not sure.
Suggestions are most welcome :)
Note: Apache POI version is 3.8. I am not dealing with Xlsx format...only xls.
Yes I tried this code but got 20 in return....which is not possible given I have only 5 rows
FileInputStream fileInputStream = new FileInputStream("COD.xls");
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
HSSFSheet worksheet = workbook.getSheet("COD");
HSSFRow row1 = worksheet.getRow(3);
Iterator rows = worksheet.rowIterator();
int noOfRows = 0;
while( rows.hasNext() ) {
HSSFRow row = (HSSFRow) rows.next();
noOfRows++;
}
System.out.println("Number of Rows: " + noOfRows);
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
if ((tempRow = sheet.getRow(i)) != null) {
//Your Code Here
}
}
The problem is that POI considers empty rows as physical rows. This happens at times in Excel and while they are not visible to the eye, the rows certainly exist.
If you were to open you Excel sheet and select everything below your data, then delete it (i know it is empty looking, but do it anyway), POI will return the right number.
You may want to getPhysicalNumberOfRows() other than getLastRowNum()?
You can iterate over the rows which are not empty using this:
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = (Row) rowIterator.next();
// Your code here
}
Thanks
worksheet.getLastRownNum() // *base index 0*
This method will give you the last row number where you might have fill the row, even if you have filled 5 rows, there can be cases that you might have filled some spaces in the remaining 15 rows or at the 21st row because of which it is giving last row number as 20.
There can be the cases that in every 5th row you enters data (starting from 1), then your 5th entry will be in 21st row, so again, if you use this method, you will get 20 in result.
Related
So I want to get data from an excel file using the Apache-POI library, that part is already solved, and then store that data in a matrix with corresponding x and y arrays. In my excel file I have my data stored in the following way:
x1 y1
x2 y2
.
.
.
xn yn
Here is the method that I wrote to first actually get the data from the excel file:
public void getDataFromExcelFile() throws IOException {
FileInputStream inputStream = new FileInputStream(excelFile);
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
while(iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
System.out.print(cell.getNumericCellValue());
System.out.println("TEST");
}
System.out.println();
}
workbook.close();
inputStream.close();
}
excelFile is a member variable of the class I am writing this method for. Anyways, let's say I have a matrix: with r rows and c columns, for each ordered (x,y) pair of data I have in my excel file, I want to store that ordered pair in a matrix. For example if my first ordered pair was (1,1) I would store that at data[0][0] (let's just assume said matrix is called data).
I looked through the apache-poi documentation thinking that maybe I could treat the getNumericCellValue() method as an array where its 0th index is an x value and its first index is a y-value, but that is not the case. I then tried to use the fact that each individual number returned by getNumericCellValue is its own method return so I tried to establish a counter,i, variable that increase in every iteration - if i is odd then store getNumericCellValue's returned value at the x-column of the matrix, if it is even then store it in the y-value - this approach did not work either. I've been going at this for 3 hours at this point so I figured it was time to ask Stackoverflow. Thanks in advance for any responses!
I am using the Apache POI library to read values from an Excel sheet into a Java program.
I iterate through each row of a table to get the values I need.
Within the object Row, there is a TreeMap that contains XSSFCell objects as values.
Normally I get the following TreeMap:
Where key 4 is included. The value often is an empty string as chosen in this picture.
For some reason, for some objects I get the following TreeMap:
Where the key 4 is missing.
Both Row Objects belong to the same table.
This is how I use my object Row:
XSSFSheet mySheet = myWorkBook.getSheet("nameOfSheet");
Iterator<Row> rowIterator = mySheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// here I call my method
}
You can prevent this from causing inconsistency in your application by calling "getCell()" passing in both an index and a MissingCellPolicy, probably Row.RETURN_BLANK_AS_NULL.
The Apache POI guide explains:
In some cases, when iterating, you need full control over how missing
or blank rows and 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).
In cases such as these, you should fetch the first and last column
information for a row, then call getCell(int, MissingCellPolicy) to
fetch the cell. Use a MissingCellPolicy to control how blank or null
cells are handled.
// 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);
if (r == null) {
// This whole row is empty
// Handle it as needed
continue;
}
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
} else {
// Do something useful with the cell's contents
}
}
}
I am getting values from my program and I want to save them in an Excel file, I am using poi library, I should have 9 sheets and in each single sheet I have to obtain a matrix (row* columns) in my case (whatever number of rows * 13 columns),,but I only obtain 1 sheet named by the last name in the array and filled only by the column number 13
Here is the method
public static void writeExcelSheet(String categoryName) throws IOException {
Workbook workbook=new HSSFWorkbook();
Sheet sheet=workbook.createSheet(categoryName);
int allength=arraylistCategory.size();
for(int row=0;row<allength;row++){
for(int column=0;column<13;column++){
Cell cell=sheet.createRow(row).createCell(column);
cell.setCellValue(arraylistCategory.get(row)[column]);
}
}
FileOutputStream outputStream=new FileOutputStream("mel coef excel sheet.xls");
workbook.write(outputStream);
outputStream.close();
arraylistCategory.clear();
}
can you please tell me whats missing or wrong,thanks in advance
I should have 9 sheets
Your method only creates one sheet in the workbook. Assuming you are attempting to call this method 9 times, you are re-creating a new, blank Workbook each time you call this method, overwriting the file each time. This explains why you are only getting the last name in the array.
Instead, create the Workbook once, then pass it into this method so you can create sheets on the same Workbook. Then after the last call to this method, then write it to the FileOutputStream.
and filled only by the column number 13
You have a similar problem here. You are creating the row with createRow(row) for each column. When you do this, you are overwriting whatever row was there with a new, empty Row, erasing all cell values except for the last value. Create the Row outside the inner for loop but inside the outer for loop, and use it inside the inner for loop.
for(int row = 0; row < allength; row++){
Row currentRow = sheet.createRow(row);
for(int column = 0; column < 13; column++){
Cell cell = currentRow.createCell(column);
cell.setCellValue(arraylistCategory.get(row)[column]);
}
}
I am using Aspose to read a CSV file.
I do not beforehand know the number of cells for each row of the file, but I will need to know it for further processing.
Unfortunately, I see no way to find out the number of cells in a CSV row.
Imagine the following row in the CSV file. It contains 7 cells, 4 of which are empty:
1,2,,4,,,
Using
row.iterator();
Aspose will only return 3 cells, as it ignores all empty cells.
As an alternative, I now do the following:
Cell lastCell = row.getLastCell();
int count = 0;
do {
cell = row.getCellOrNull(count);
String cellValue = cell == null ? "" : cell.getStringValueWithoutFormat();
//do something with the cell value...
count++;
} while (cell == null || ! lastCell.equals(cell));
This works better, as it returns the first 4 cells.
However, it still ignores the last 3 cells .
Is there any way to get information about the missing cells?
(It would be sufficient for me if Aspose could return the original Row as a String - I could then count the number of commas and find out the number of cells this way)
You may use Worksheet.getCells().getMaxDisplayRange() method to get the maximum display range.
Please consider this CSV. If you open it in MS-Excel and check the last cell, you will find it is Q2
Book1.csv
2,,,,1,,,,,,,,,,,,,
,,3,,,,
Aspose.Cells returns the same via the following code.
TxtLoadOptions opts = new TxtLoadOptions(LoadFormat.CSV);
Workbook wb = new Workbook("Book1.csv", opts);
Worksheet ws = wb.getWorksheets().get(0);
Range rng = ws.getCells().getMaxDisplayRange();
System.out.println(rng);
Here is the console output of the code.
Console Output
Aspose.Cells.Range [ Sheet1!A1:Q2 ]
Note: I am working as Developer Evangelist at Aspose
When trying to read an Excel sheet I get an exception if some cell is empty:
Cell[] rowCells = sheet.getRow(1);
or
Cell cell = sheet.getCell(0,1);
I always get the same message:
java.lang.ArrayIndexOutOfBoundsException: 1
at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)
at gui.ReadExcel.read(ReadExcel.java:45)
at gui.GUIcontroller.chooseSaveFile(GUIcontroller.java:101)
What is the problem? How can I know if the cell is empty, so I won't copy its value?
You can use the getRows or getColumns method to check the bounds of the sheet. The ArrayIndexOutOfBoundsException occurs because you are trying to access a value, which is beyond the range of the farthest cell which is not empty.
int rows = sheet.getRows();
int columns = sheet.getColumns();
int i = 1;
if(i<rows)
Cell[] rowCells = sheet.getRow(i); //Won't throw an Exception
if(i<rows && j<columns)
Cell cell = sheet.getCell(i,j);
In this case you can't read the cell because, as far as jxl is concerned, it doesn't really exist on the spreadsheet. It has yet to be created so there is really no cell to get. It may sound odd because excel sheets go on for what seems like forever though it doesn't store the data of all these empty cells because the file size would be huge. So when jxl goes to read the data it will simply tell you there is nothing there.
If you want to read the cells and all your cells are grouped together than you could try:
int width = sheet.getColumns();
int height = sheet.getRows();
List<Cell> cells = new ArrayList<Cell>();
for(int i=0; i<width; i++){
for(int j=0; j<height; j++){
cells.add(sheet.getCell(i, j));
}
}
If they're not grouped together and your not sure which cells maybe empty there is still a fairly simple solution
List<Cell> cells = new ArrayList<Cell>();
Cell cell = null;
try{
cell = sheet.getCell(0, 1);
}catch(Exception e){
e.printStackTrace();
}finally{
if(cell != null){
cells.add(cell);
}
}
This way you can safely attempt to read a cell and throw it away if it doesn't contain anything.
I hope this is what you were looking for.