Hello!

I have an HSSFSheet and want to display it in a JTable.

For this purpose, I've written a TableModel (see below).

The table shows the HSSFSheet almost correctly. The only problem I have
is the display of sums. The HSSFSheet, which I used for testing,
contains a formulae like "SUM(A1:A20)". These formulae appear in the
table as zeroes (i. e. they are not evaluated).

What have I to do in order to display the sums correctly?

Thanks in advance

dap



Code of the TableModel:

import java.util.Iterator;

import javax.swing.table.DefaultTableModel;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

/**
 * @author Dmitri Pissarenko http://dapissarenko.com
 */
public class HSSFSheetTableModel extends DefaultTableModel {
        private HSSFSheet sheet;

        private Logger logger = Logger.getLogger(getClass());

        public HSSFSheetTableModel() {
                super();

        }

        public int getColumnCount() {
                HSSFRow row = null;
                int columnCount = 0;

                columnCount = 0;
                if (sheet == null) {
                        return 0;
                } else {
                        if (this.sheet.getLastRowNum() > 0) {
                                Iterator iterator = null;

                                iterator = this.sheet.rowIterator();
                                while (iterator.hasNext()) {
                                        row = (HSSFRow) iterator.next();
                                        if (row.getLastCellNum() > columnCount) {
                                                columnCount = row.getLastCellNum() + 1;
                                        }

                                }
                        }
                        return columnCount;
                }

        }

        public String getColumnName(int col) {
                Character character = null;

                character = new Character((char) ('A' + col));
                return character.toString();
        }

        public int getRowCount() {
                if (this.sheet == null) {
                        return 0;
                } else {
                        return this.sheet.getLastRowNum() + 1;
                }

        }

        public Object getValueAt(int rowNum, int col) {
                HSSFRow row = null;
                HSSFCell cell = null;
                Object value = null;

                value = "";
                if (sheet != null) {
                        row = this.sheet.getRow(rowNum);
                        if (row != null) {
                                cell = row.getCell((short) col);
                                if (cell != null) {
                                        if (cell.getCellType() == 
HSSFCell.CELL_TYPE_BLANK) {
                                                value = "";
                                        } else if (cell.getCellType() == 
HSSFCell.CELL_TYPE_BOOLEAN) {
                                                value = "" + 
cell.getBooleanCellValue();
                                        } else if (cell.getCellType() == 
HSSFCell.CELL_TYPE_ERROR) {
                                                value = "ERROR";
                                        } else if (cell.getCellType() == 
HSSFCell.CELL_TYPE_FORMULA) {
                                                value = "" + 
cell.getNumericCellValue();
                                        } else if (cell.getCellType() == 
HSSFCell.CELL_TYPE_NUMERIC) {
                                                value = "" + 
cell.getNumericCellValue();
                                        } else if (cell.getCellType() == 
HSSFCell.CELL_TYPE_STRING) {
                                                value = cell.getStringCellValue();
                                        }
                                }
                        }
                }

                return value;

        }

        public boolean isCellEditable(int arg0, int arg1) {
                return false;
        }

        public HSSFSheet getSheet() {
                return sheet;
        }

        public void setSheet(HSSFSheet sheet) {
                this.sheet = sheet;
        }
}

--
Dmitri Pissarenko
Software Engineer
http://dapissarenko.com


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to