Hello Team,
I am faciing a problem when i am iterating row in excel file. I use a AbstractExcelAsposeUpload class to do all this process
Here is the class definition :
package caf.opm.sec.tda.common;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.aspose.cells.Cell;
import com.aspose.cells.Cells;
import com.aspose.cells.FileFormatType;
import com.aspose.cells.LoadOptions;
import com.aspose.cells.Row;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
/**
*
* @author blrskab
*
*/
public abstract class AbstractExcelAsposeUpload {
private static final Logger TDALOG = Logger.getLogger(AbstractExcelAsposeUpload.class);
private static final String UNCHECKED = "unchecked";
protected InputStream stream;
protected Worksheet worksheet;
@SuppressWarnings(UNCHECKED)
protected Iterator rowIterator;
protected Cells cells;
/**
* @param inputStream
*/
public AbstractExcelAsposeUpload(InputStream inputStream) {
this.stream = inputStream;
}
//default Constructor
public AbstractExcelAsposeUpload(){
}
/**
*
*/
public void close() {
try {
if (getStream() != null) {
getStream().close();
}
} catch (IOException e) {
TDALOG.error(e);
}
}
/**
*
* @param extension
* @return
* @throws TDAException
*/
public Workbook open(String extension) throws TDAException {
Workbook workbook = null;
LoadOptions loadOptions = null;
try {
//workbook.open(getStream());
if(StringUtils.isNotBlank(extension)) {
if("xls".equalsIgnoreCase(extension)) {
loadOptions = new LoadOptions(FileFormatType.EXCEL_97_TO_2003);
} else {
loadOptions = new LoadOptions(FileFormatType.XLSX);
}
workbook = new Workbook(getStream(),loadOptions);
}
} catch (IOException e) {
TDALOG.error(e);
throw new TDAException(e);
} catch (Exception e) {
throw new TDAException(e);
}
return workbook;
}
/**
* @param rowNumber
* @return
*/
public Row readNextRow(int rowNumber) {
return cells.getRows().get(rowNumber);
}
/**
* @return
*/
@SuppressWarnings(UNCHECKED)
public Iterator getRowIterator() {
return rowIterator;
}
/**
* @param rowIterator
*/
@SuppressWarnings(UNCHECKED)
public void setRowIterator(Iterator rowIterator) {
this.rowIterator = rowIterator;
}
/**
* @return
*/
public InputStream getStream() {
return stream;
}
/**
* @param stream
*/
public void setStream(InputStream stream) {
this.stream = stream;
}
/**
* @return
*/
public Worksheet getWorksheet() {
return worksheet;
}
/**
* @param worksheet
*/
public void setWorksheet(Worksheet worksheet) {
this.worksheet = worksheet;
}
/**
* @return
*/
public boolean hasNext() {
return getRowIterator().hasNext();
}
/**
* @return
*/
public Cells getCells() {
return cells;
}
/**
* @param cells
*/
public void setCells(Cells cells) {
this.cells = cells;
}
/**
* @param row
* @return
*/
@SuppressWarnings(UNCHECKED)
public boolean isRowEmpty(Row row) {
Iterator itr = row.iterator();
boolean foundNonEmpty = false;
while (itr.hasNext() && !foundNonEmpty) {
Cell cell = (Cell) itr.next();
if (cell.getStringValue() != null && !cell.getStringValue().trim().equals("")) {
// Non empty cell found in the row - break the loop and return
foundNonEmpty = true;
}
}
return !foundNonEmpty;
}
/**
* @return
*/
public int getNumberOfFilledRows() {
int rowNumber = 0;
while (getRowIterator().hasNext()) {
if (isRowEmpty(cells.getRows().get(rowNumber))) {
break;
}
rowNumber++;
}
return rowNumber;
}
}
-------------------------------------------------------------------
I extenda above class in excel reader as below :
public class AmortScheduleExcelReader extends AbstractExcelAsposeUpload {
public AmortScheduleExcelReader(InputStream inputStream) {
super(inputStream); }
public void readAmortScheduleSheet() {
int noOfFilledRows = getNumberOfFilledRows(); }
}
------------------------------------------------------
here getNumberOfFilledRows() method is behaving very strange. It is not processing ocmplete rows in an excel sheet. I have attached excel sheet which i use.
I use aspose cell 8.2.0 and java 7
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.2.0</version>
</dependency>
Please give me solution why it is behaving differently.