Search in sources :

Example 6 with CellReference

use of org.apache.poi.hssf.util.CellReference in project oc-explorer by devgateway.

the class XExcelFileReader method getDataRow.

private String[] getDataRow() throws XMLStreamException {
    List<String> rowValues = new ArrayList<String>();
    while (xmlReader.hasNext()) {
        xmlReader.next();
        if (xmlReader.isStartElement()) {
            if (xmlReader.getLocalName().equals("c")) {
                CellReference cellReference = new CellReference(xmlReader.getAttributeValue(null, "r"));
                // Fill in the possible blank cells!
                while (rowValues.size() < cellReference.getCol()) {
                    rowValues.add("");
                }
                String cellType = xmlReader.getAttributeValue(null, "t");
                rowValues.add(getCellValue(cellType));
            }
        } else if (xmlReader.isEndElement() && xmlReader.getLocalName().equals("row")) {
            break;
        }
    }
    return rowValues.toArray(new String[rowValues.size()]);
}
Also used : ArrayList(java.util.ArrayList) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) CellReference(org.apache.poi.hssf.util.CellReference)

Example 7 with CellReference

use of org.apache.poi.hssf.util.CellReference in project my_curd by qinyou.

the class ExcelHelper method excelFileConvertToList.

/**
 * @param filename
 * @param inputStream
 * @return
 * @throws Exception
 */
public List<List> excelFileConvertToList(String filename, FileInputStream inputStream) throws Exception {
    Workbook wb = null;
    // 此处为兼容2003 与2oo7
    String ext = getExtensionName(filename);
    if (ext.toLowerCase().equals("xlsx")) {
        wb = new XSSFWorkbook(inputStream);
    } else {
        wb = WorkbookFactory.create(inputStream);
    }
    Sheet sheet = wb.getSheetAt(0);
    List<List> rows = new ArrayList<List>();
    for (Row row : sheet) {
        List<Object> cells = new ArrayList<Object>();
        for (Cell cell : row) {
            Object obj = null;
            CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
            switch(cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    obj = cell.getRichStringCellValue().getString();
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        obj = new JDateTime(cell.getDateCellValue());
                    } else {
                        obj = cell.getNumericCellValue();
                    }
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    obj = cell.getBooleanCellValue();
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    obj = cell.getNumericCellValue();
                    break;
                default:
                    obj = null;
            }
            cells.add(obj);
        }
        rows.add(cells);
    }
    return rows;
}
Also used : CellReference(org.apache.poi.hssf.util.CellReference) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) JDateTime(jodd.datetime.JDateTime) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook)

Example 8 with CellReference

use of org.apache.poi.hssf.util.CellReference in project poi by apache.

the class TableRecord method toString.

public String toString() {
    StringBuffer buffer = new StringBuffer();
    buffer.append("[TABLE]\n");
    buffer.append("    .range    = ").append(getRange()).append("\n");
    buffer.append("    .flags    = ").append(HexDump.byteToHex(field_5_flags)).append("\n");
    buffer.append("    .alwaysClc= ").append(isAlwaysCalc()).append("\n");
    buffer.append("    .reserved = ").append(HexDump.intToHex(field_6_res)).append("\n");
    CellReference crRowInput = cr(field_7_rowInputRow, field_8_colInputRow);
    CellReference crColInput = cr(field_9_rowInputCol, field_10_colInputCol);
    buffer.append("    .rowInput = ").append(crRowInput.formatAsString()).append("\n");
    buffer.append("    .colInput = ").append(crColInput.formatAsString()).append("\n");
    buffer.append("[/TABLE]\n");
    return buffer.toString();
}
Also used : CellReference(org.apache.poi.hssf.util.CellReference)

Example 9 with CellReference

use of org.apache.poi.hssf.util.CellReference in project poi by apache.

the class TestExternalNameReference method testReadCalcSheet.

/**
	 * tests <tt>NameXPtg for external cell reference by name</tt> and logic in Workbook below that   
	 */
public void testReadCalcSheet() {
    try {
        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("XRefCalc.xls");
        assertEquals("Sheet1!$A$2", wb.getName("QUANT").getRefersToFormula());
        assertEquals("Sheet1!$B$2", wb.getName("PART").getRefersToFormula());
        assertEquals("x123", wb.getSheet("Sheet1").getRow(1).getCell(1).getStringCellValue());
        assertEquals("Sheet1!$C$2", wb.getName("UNITCOST").getRefersToFormula());
        CellReference cellRef = new CellReference(wb.getName("UNITCOST").getRefersToFormula());
        HSSFCell cell = wb.getSheet(cellRef.getSheetName()).getRow(cellRef.getRow()).getCell((int) cellRef.getCol());
        assertEquals("VLOOKUP(PART,COSTS,2,FALSE)", cell.getCellFormula());
        assertEquals("Sheet1!$D$2", wb.getName("COST").getRefersToFormula());
        cellRef = new CellReference(wb.getName("COST").getRefersToFormula());
        cell = wb.getSheet(cellRef.getSheetName()).getRow(cellRef.getRow()).getCell((int) cellRef.getCol());
        assertEquals("UNITCOST*Quant", cell.getCellFormula());
        assertEquals("Sheet1!$E$2", wb.getName("TOTALCOST").getRefersToFormula());
        cellRef = new CellReference(wb.getName("TOTALCOST").getRefersToFormula());
        cell = wb.getSheet(cellRef.getSheetName()).getRow(cellRef.getRow()).getCell((int) cellRef.getCol());
        assertEquals("Cost*Markup_Cost", cell.getCellFormula());
    } catch (Exception e) {
        fail();
    }
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) CellReference(org.apache.poi.hssf.util.CellReference) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 10 with CellReference

use of org.apache.poi.hssf.util.CellReference in project poi by apache.

the class HSSFPatriarch method preSerialize.

/**
     * check if any shapes contain wrong data
     * At now(13.08.2010) check if patriarch contains 2 or more comments with same coordinates
     */
protected void preSerialize() {
    Map<Integer, NoteRecord> tailRecords = _boundAggregate.getTailRecords();
    /**
         * contains coordinates of comments we iterate over
         */
    Set<String> coordinates = new HashSet<String>(tailRecords.size());
    for (NoteRecord rec : tailRecords.values()) {
        String noteRef = new CellReference(rec.getRow(), rec.getColumn()).formatAsString();
        if (coordinates.contains(noteRef)) {
            throw new IllegalStateException("found multiple cell comments for cell " + noteRef);
        } else {
            coordinates.add(noteRef);
        }
    }
}
Also used : NoteRecord(org.apache.poi.hssf.record.NoteRecord) CellReference(org.apache.poi.hssf.util.CellReference) HashSet(java.util.HashSet)

Aggregations

CellReference (org.apache.poi.hssf.util.CellReference)14 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)3 ArrayList (java.util.ArrayList)2 JDateTime (jodd.datetime.JDateTime)2 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2 CellValue (org.apache.poi.ss.usermodel.CellValue)2 XSSFRichTextString (org.apache.poi.xssf.usermodel.XSSFRichTextString)2 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)1 NoteRecord (org.apache.poi.hssf.record.NoteRecord)1 FormulaRecordAggregate (org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate)1 UnicodeString (org.apache.poi.hssf.record.common.UnicodeString)1 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)1 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)1 Ptg (org.apache.poi.ss.formula.ptg.Ptg)1 Cell (org.apache.poi.ss.usermodel.Cell)1 CellType (org.apache.poi.ss.usermodel.CellType)1 ScorecardError (org.drools.scorecards.ScorecardError)1