Search in sources :

Example 1 with KSheet

use of org.pentaho.di.core.spreadsheet.KSheet in project pentaho-kettle by pentaho.

the class StaxPoiSheetTest method testReadCells.

@Test
public void testReadCells() throws Exception {
    KSheet sheet = getSampleSheet();
    KCell cell = sheet.getCell(1, 2);
    assertEquals("One", cell.getValue());
    assertEquals(KCellType.LABEL, cell.getType());
    cell = sheet.getCell(2, 2);
    assertEquals(KCellType.DATE, cell.getType());
    assertEquals(new Date(1283817600000L), cell.getValue());
    cell = sheet.getCell(1, 3);
    assertEquals("Two", cell.getValue());
    assertEquals(KCellType.LABEL, cell.getType());
}
Also used : KSheet(org.pentaho.di.core.spreadsheet.KSheet) KCell(org.pentaho.di.core.spreadsheet.KCell) Date(java.util.Date) Test(org.junit.Test)

Example 2 with KSheet

use of org.pentaho.di.core.spreadsheet.KSheet in project pentaho-kettle by pentaho.

the class StaxPoiSheetTest method testReadEmptyRow.

@Test
public void testReadEmptyRow() throws Exception {
    KSheet sheet1 = getSampleSheet();
    KCell[] row = sheet1.getRow(0);
    assertEquals("empty row expected", 0, row.length);
}
Also used : KSheet(org.pentaho.di.core.spreadsheet.KSheet) KCell(org.pentaho.di.core.spreadsheet.KCell) Test(org.junit.Test)

Example 3 with KSheet

use of org.pentaho.di.core.spreadsheet.KSheet in project pentaho-kettle by pentaho.

the class JxlWorkBookIT method testRead.

public void testRead() throws Exception {
    KWorkbook workbook = WorkbookFactory.getWorkbook(SpreadSheetType.JXL, "src/it/resources/sample-file.xls", null);
    int numberOfSheets = workbook.getNumberOfSheets();
    assertEquals(3, numberOfSheets);
    KSheet sheet1 = workbook.getSheet(0);
    assertEquals("Sheet1", sheet1.getName());
    sheet1 = workbook.getSheet("Sheet1");
    assertEquals("Sheet1", sheet1.getName());
    assertEquals(5, sheet1.getRows());
    KCell[] row = sheet1.getRow(2);
    assertEquals(KCellType.LABEL, row[1].getType());
    assertEquals("One", row[1].getValue());
    assertEquals(KCellType.DATE, row[2].getType());
    assertEquals(new Date(1283817600000L), row[2].getValue());
    assertEquals(KCellType.NUMBER, row[3].getType());
    assertEquals(Double.valueOf("75"), row[3].getValue());
    assertEquals(KCellType.BOOLEAN, row[4].getType());
    assertEquals(Boolean.valueOf(true), row[4].getValue());
    assertEquals(KCellType.NUMBER_FORMULA, row[5].getType());
    assertEquals(Double.valueOf("75"), row[5].getValue());
    row = sheet1.getRow(3);
    assertEquals(KCellType.LABEL, row[1].getType());
    assertEquals("Two", row[1].getValue());
    assertEquals(KCellType.DATE, row[2].getType());
    assertEquals(new Date(1283904000000L), row[2].getValue());
    assertEquals(KCellType.NUMBER, row[3].getType());
    assertEquals(Double.valueOf("42"), row[3].getValue());
    assertEquals(KCellType.BOOLEAN, row[4].getType());
    assertEquals(Boolean.valueOf(false), row[4].getValue());
    assertEquals(KCellType.NUMBER_FORMULA, row[5].getType());
    assertEquals(Double.valueOf("117"), row[5].getValue());
    row = sheet1.getRow(4);
    assertEquals(KCellType.LABEL, row[1].getType());
    assertEquals("Three", row[1].getValue());
    assertEquals(KCellType.DATE, row[2].getType());
    assertEquals(new Date(1283990400000L), row[2].getValue());
    assertEquals(KCellType.NUMBER, row[3].getType());
    assertEquals(Double.valueOf("93"), row[3].getValue());
    assertEquals(KCellType.BOOLEAN, row[4].getType());
    assertEquals(Boolean.valueOf(true), row[4].getValue());
    assertEquals(KCellType.NUMBER_FORMULA, row[5].getType());
    assertEquals(Double.valueOf("210"), row[5].getValue());
    try {
        sheet1.getRow(5);
        throw new Exception("No out of bounds exception thrown when expected");
    } catch (ArrayIndexOutOfBoundsException e) {
    // OK!
    }
    workbook.close();
}
Also used : KWorkbook(org.pentaho.di.core.spreadsheet.KWorkbook) KSheet(org.pentaho.di.core.spreadsheet.KSheet) KCell(org.pentaho.di.core.spreadsheet.KCell) Date(java.util.Date)

Example 4 with KSheet

use of org.pentaho.di.core.spreadsheet.KSheet in project pentaho-kettle by pentaho.

the class OdsWorkBookIT method testRead.

public void testRead() throws Exception {
    KWorkbook workbook = WorkbookFactory.getWorkbook(SpreadSheetType.ODS, "src/it/resources/sample-file.ods", null);
    int numberOfSheets = workbook.getNumberOfSheets();
    assertEquals(3, numberOfSheets);
    KSheet sheet1 = workbook.getSheet(0);
    assertEquals("Sheet1", sheet1.getName());
    sheet1 = workbook.getSheet("Sheet1");
    assertEquals("Sheet1", sheet1.getName());
    assertEquals(5, sheet1.getRows());
    KCell[] row = sheet1.getRow(2);
    assertEquals(KCellType.LABEL, row[1].getType());
    assertEquals("One", row[1].getValue());
    assertEquals(KCellType.DATE, row[2].getType());
    assertEquals(new Date(1283817600000L), row[2].getValue());
    assertEquals(KCellType.NUMBER, row[3].getType());
    assertEquals(Double.valueOf("75"), row[3].getValue());
    // Always like that in ODS
    assertEquals(KCellType.BOOLEAN_FORMULA, row[4].getType());
    assertEquals(Boolean.valueOf(true), row[4].getValue());
    assertEquals(KCellType.NUMBER_FORMULA, row[5].getType());
    assertEquals(Double.valueOf("75"), row[5].getValue());
    row = sheet1.getRow(3);
    assertEquals(KCellType.LABEL, row[1].getType());
    assertEquals("Two", row[1].getValue());
    assertEquals(KCellType.DATE, row[2].getType());
    assertEquals(new Date(1283904000000L), row[2].getValue());
    assertEquals(KCellType.NUMBER, row[3].getType());
    assertEquals(Double.valueOf("42"), row[3].getValue());
    // Always like that in ODS
    assertEquals(KCellType.BOOLEAN_FORMULA, row[4].getType());
    assertEquals(Boolean.valueOf(false), row[4].getValue());
    assertEquals(KCellType.NUMBER_FORMULA, row[5].getType());
    assertEquals(Double.valueOf("117"), row[5].getValue());
    row = sheet1.getRow(4);
    assertEquals(KCellType.LABEL, row[1].getType());
    assertEquals("Three", row[1].getValue());
    assertEquals(KCellType.DATE, row[2].getType());
    assertEquals(new Date(1283990400000L), row[2].getValue());
    assertEquals(KCellType.NUMBER, row[3].getType());
    assertEquals(Double.valueOf("93"), row[3].getValue());
    // Always like that in ODS
    assertEquals(KCellType.BOOLEAN_FORMULA, row[4].getType());
    assertEquals(Boolean.valueOf(true), row[4].getValue());
    assertEquals(KCellType.NUMBER_FORMULA, row[5].getType());
    assertEquals(Double.valueOf("210"), row[5].getValue());
    workbook.close();
}
Also used : KWorkbook(org.pentaho.di.core.spreadsheet.KWorkbook) KSheet(org.pentaho.di.core.spreadsheet.KSheet) KCell(org.pentaho.di.core.spreadsheet.KCell) Date(java.util.Date)

Example 5 with KSheet

use of org.pentaho.di.core.spreadsheet.KSheet in project pentaho-kettle by pentaho.

the class StaxWorkBookIT method testEmptySheet.

@Test
public void testEmptySheet() throws Exception {
    KWorkbook workbook = getWorkbook("src/it/resources/sample-file.xlsx", null);
    int numberOfSheets = workbook.getNumberOfSheets();
    assertEquals(3, numberOfSheets);
    // last two sheets are empty, check if no exception opening
    for (int i = 1; i < numberOfSheets; i++) {
        KSheet sheet = workbook.getSheet(i);
        for (int j = 0; j < sheet.getRows(); j++) {
            sheet.getRow(j);
        }
    }
}
Also used : KWorkbook(org.pentaho.di.core.spreadsheet.KWorkbook) KSheet(org.pentaho.di.core.spreadsheet.KSheet) Test(org.junit.Test)

Aggregations

KSheet (org.pentaho.di.core.spreadsheet.KSheet)18 KCell (org.pentaho.di.core.spreadsheet.KCell)16 Test (org.junit.Test)11 KWorkbook (org.pentaho.di.core.spreadsheet.KWorkbook)11 Date (java.util.Date)8 FileObject (org.apache.commons.vfs2.FileObject)2 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)2 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)2 ExcelInputMeta (org.pentaho.di.trans.steps.excelinput.ExcelInputMeta)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 ResultFile (org.pentaho.di.core.ResultFile)1 KettleException (org.pentaho.di.core.exception.KettleException)1 KettleFileException (org.pentaho.di.core.exception.KettleFileException)1 KettlePluginException (org.pentaho.di.core.exception.KettlePluginException)1 KettleStepException (org.pentaho.di.core.exception.KettleStepException)1 FileInputList (org.pentaho.di.core.fileinput.FileInputList)1 PluginInterface (org.pentaho.di.core.plugins.PluginInterface)1