Just as with an array structure in Java, POI numbers columns starting from zero
and not one - or A - as Excel does. Therefore, if you have six columns on the
sheet these will be numbered 0 to 5 and that is why - at least I think that is
why - the getNumberOfColumns() method seems to return an incorrect figure.
PS It helps if you are simply iterating through the columns using a loop to
have this happen IMO.
Modha Khammammettu <[EMAIL PROTECTED]> wrote: Hi All
I am using POI 3.0 rc4
I get wrong column count when I read excel file I created.
I use the following method to get Column count:
public int getNumberOfColumns(int sheetNumber){
HSSFRow row = getRow(sheetNumber,2);
return row.getLastCellNum();
}
I always get 1 column less than what I have. If I have 6 columns I get only 5
as return value.
However, when I open the excel file and make a smal change (any change like
adjusting column width etc) and save it, the same getNumberOfColumns method
returns correct columns.
Is there any solution for this problem. Please let me know.
I create excel file programatically
ExcelFile excel = new ExcelFile();
excel.createWorkBook();
I add rows like this:
public void addRow(int sheetNumber, String content){
HSSFSheet sheet = (HSSFSheet)wb.getSheetAt(sheetNumber);
HSSFRow row = sheet.createRow(currentRowNumberBeingAdded);
currentRowNumberBeingAdded = currentRowNumberBeingAdded + 1;
StringTokenizer st = new StringTokenizer(content,"~");
int cellNumber = 0;
while (st.hasMoreTokens()){
HSSFCell cell = row.createCell((short)cellNumber);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(st.nextToken());
cellNumber = cellNumber + 1;
}
}
I save like this:
excel.saveWorkBook(destn);
I import these:
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
Thanks
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------
Be a better Globetrotter. Get better travel answers from someone who knows.
Yahoo! Answers - Check it out.