Example usage for org.apache.poi.ss.usermodel WorkbookFactory create

List of usage examples for org.apache.poi.ss.usermodel WorkbookFactory create

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel WorkbookFactory create.

Prototype

public static Workbook create(File file) throws IOException, EncryptedDocumentException 

Source Link

Document

Creates the appropriate HSSFWorkbook / XSSFWorkbook from the given File, which must exist and be readable.

Usage

From source file:info.informationsea.venn.CombinationImporterExporter.java

License:Open Source License

public static CombinationSolver<String, String> importCombination(File file) throws Exception {
    if (file.getName().endsWith(".csv")) {
        try (Reader reader = new FileReader(file); TableCSVReader csvReader = new TableCSVReader(reader)) {
            csvReader.setUseHeader(true);
            return importCombination(csvReader);
        }/*www .  j  a  v  a2  s.  c  o m*/
    } else if (file.getName().endsWith(".xls") || file.getName().endsWith(".xlsx")) {
        Workbook workbook = WorkbookFactory.create(file);
        Sheet sheet = workbook.getSheetAt(0);
        try (ExcelSheetReader sheetReader = new ExcelSheetReader(sheet)) {
            sheetReader.setUseHeader(true);
            return importCombination(sheetReader);
        }
    } else {
        throw new IllegalArgumentException("Unsupported file");
    }
}

From source file:info.informationsea.venn.CombinationImporterExporterTest.java

License:Open Source License

public void testExcel(String filename) throws Exception {
    CombinationImporterExporter.export(new File(DIST_DIR, filename), combinationSolver,
            new VennFigureParameters<String>(combinationSolver).getKeys());

    Workbook workbook = WorkbookFactory.create(new File(DIST_DIR, filename));
    Sheet sheet = workbook.getSheetAt(0);

    ExcelSheetReader reader = new ExcelSheetReader(sheet);
    reader.setUseHeader(true);//from w ww  .  j a  va2s .  co  m

    CombinationSolver<String, String> solver = CombinationImporterExporter.importCombination(reader);
    Assert.assertEquals(combinationSolver.getValues(), solver.getValues());

    sheet = workbook.getSheetAt(1);
    reader = new ExcelSheetReader(sheet);
    reader.setUseHeader(true);

    Map<Set<String>, Set<String>> combinations = new HashMap<>();
    for (TableRecord record : reader) {
        String[] key = record.get(0).toString().split(", ");
        Set<String> values = new HashSet<>();
        for (int i = 2; i < record.size(); i++) {
            values.add(record.get(i).toString());
        }

        Assert.assertEquals(values.size(), Integer.parseInt(record.get(1).toString()));

        combinations.put(asSet(key), values);
    }

    Assert.assertEquals(combinations, combinationSolver.getCombinationResult());
    Assert.assertEquals(solver.getCombinationResult(), combinationSolver.getCombinationResult());
}

From source file:IO.FILES.java

public void overWrite(Persona p, String texto) throws Exception {
    if (p == null)
        return;/*from  w w w .  j av a 2  s.  com*/
    Workbook wb = WorkbookFactory.create(new FileInputStream(new File(ruta)));
    Sheet hoja = wb.getSheetAt(0);
    boolean encontrado = false;
    Row row = null;
    Iterator it = hoja.rowIterator();
    while (it.hasNext()) {
        row = (Row) it.next();
        if (row.getCell(0).getStringCellValue().equalsIgnoreCase(p.getId())) {
            encontrado = true;
            break;
        }
    }

    if (!encontrado)
        return;
    int aux = row.getRowNum();
    hoja.removeRow(row);
    row = hoja.createRow(aux);
    CentroEducativo centro = p.getCentro();
    Redaccion R = p.getRedaccion();
    for (int i = 0; i < cols.length; i++) {
        Cell cell = row.createCell(i);
        switch (i) {
        case 0:
            cell.setCellValue(p.getId());
            break;
        case 1:
            cell.setCellValue(p.getName());
            break;
        case 2:
            cell.setCellValue(p.getLastName());
            break;
        case 3:
            cell.setCellValue(p.getLastName2());
            break;
        case 4:
            cell.setCellValue(p.getNivel());
            break;
        case 5:
            cell.setCellValue(p.getEdad());
            break;
        case 6:
            cell.setCellValue(p.isMale() ? "M" : "F");
            break;
        case 7:
            cell.setCellValue(centro.getName());
            break;
        case 8:
            cell.setCellValue(centro.getSiglas());
            break;
        case 9:
            cell.setCellValue(centro.isPublic() ? "PUBLICO" : "PRIVADO");
            break;
        case 10:
            cell.setCellValue(p.isCCA() ? "CCA" : "SCA");
            break;
        case 11:
            cell.setCellValue(p.isCCA() ? p.getCCA_DETALLE() : "-");
            break;
        case 12:
            cell.setCellValue(p.isAdecuacion() ? "SI" : "NO");
            break;
        case 13:
            cell.setCellValue(p.isAdecuacion() ? modelo.Info.tipos[p.getTipo()] : "-");
            break;
        case 14:
            cell.setCellValue(R.getUT());
            break;
        case 15:
            cell.setCellValue(R.getCL());
            break;
        case 16:
            cell.setCellValue(R.getPAL());
            break;
        case 17:
            cell.setCellValue(R.getLPUT());
            break;
        case 18:
            cell.setCellValue(R.getLPCL());
            break;
        case 19:
            cell.setCellValue(R.getINSUB());
        }
    }
    REDACCIONES.overWrite(wb, p, texto);
    save(wb);
}

From source file:IO.FILES.java

public void write(Persona p, String texto) throws Exception {
    if (p == null)
        return;//from w  ww.j  a  v  a2 s .  co  m
    Workbook wb = WorkbookFactory.create(new FileInputStream(new File(ruta)));
    Sheet hoja = wb.getSheetAt(0);
    int inx = 0;
    Iterator it = hoja.rowIterator();
    while (it.hasNext()) {
        inx++;
        it.next();
    }
    Row row = hoja.createRow(inx);
    CentroEducativo centro = p.getCentro();
    Redaccion R = p.getRedaccion();
    for (int i = 0; i < cols.length; i++) {
        Cell cell = row.createCell(i);
        switch (i) {
        case 0:
            cell.setCellValue(p.getId());
            break;
        case 1:
            cell.setCellValue(p.getName());
            break;
        case 2:
            cell.setCellValue(p.getLastName());
            break;
        case 3:
            cell.setCellValue(p.getLastName2());
            break;
        case 4:
            cell.setCellValue(p.getNivel());
            break;
        case 5:
            cell.setCellValue(p.getEdad());
            break;
        case 6:
            cell.setCellValue(p.isMale() ? "M" : "F");
            break;
        case 7:
            cell.setCellValue(centro.getName());
            break;
        case 8:
            cell.setCellValue(centro.getSiglas());
            break;
        case 9:
            cell.setCellValue(centro.isPublic() ? "PUBLICO" : "PRIVADO");
            break;
        case 10:
            cell.setCellValue(p.isCCA() ? "CCA" : "SCA");
            break;
        case 11:
            cell.setCellValue(p.isCCA() ? p.getCCA_DETALLE() : "-");
            break;
        case 12:
            cell.setCellValue(p.isAdecuacion() ? "SI" : "NO");
            break;
        case 13:
            cell.setCellValue(p.isAdecuacion() ? modelo.Info.tipos[p.getTipo()] : "-");
            break;
        case 14:
            cell.setCellValue(R.getUT());
            break;
        case 15:
            cell.setCellValue(R.getCL());
            break;
        case 16:
            cell.setCellValue(R.getPAL());
            break;
        case 17:
            cell.setCellValue(R.getLPUT());
            break;
        case 18:
            cell.setCellValue(R.getLPCL());
            break;
        case 19:
            cell.setCellValue(R.getINSUB());
        }
    }
    REDACCIONES.write(wb, p, texto);
    save(wb);
}

From source file:IO.FILES.java

public void remove(String id) throws Exception {
    try {//from   w  ww. j  a va 2s . c o m
        Workbook wb = WorkbookFactory.create(new FileInputStream(new File(ruta)));
        Sheet hoja = wb.getSheetAt(0);
        Iterator rows = hoja.rowIterator();

        int i = 0;
        Persona p = null;
        while (rows.hasNext()) {
            Row row = (Row) rows.next();
            if (row.getCell(0).getStringCellValue().equalsIgnoreCase(id)) {
                removeRow(hoja, row);
                REDACCIONES.remove(id, wb);
                save(wb);
                break;
            }
            i++;
        }
    } catch (IOException | InvalidFormatException | EncryptedDocumentException ex) {
        Logger.getLogger(FILES.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:IO.FILES.java

final void header() {
    try {//  ww  w  . j  av  a 2 s  .  c o  m
        Workbook wb = WorkbookFactory.create(new FileInputStream(new File(ruta)));
        Sheet hoja = wb.getSheetAt(0);
        Row row = hoja.createRow(0);
        int h = 0;
        for (String s : cols) {
            Cell cell = row.createCell(h++);
            cell.setCellValue(s);
        }
        save(wb);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(FILES.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException | InvalidFormatException | EncryptedDocumentException ex) {
        Logger.getLogger(FILES.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:IO.FILES.java

public Persona getById(String id) {
    if (id.equals(""))
        return null;
    Persona p = null;// www  . j  a v  a  2s .co m
    try {
        Workbook wb = WorkbookFactory.create(new FileInputStream(new File(ruta)));
        Sheet hoja = wb.getSheetAt(0);
        Iterator rows = hoja.rowIterator();

        while (rows.hasNext()) {
            Row row = (Row) rows.next();
            if (row.getCell(0).getStringCellValue().equalsIgnoreCase(id)) {
                p = toPersona(row);
            }
        }
    } catch (IOException | InvalidFormatException | EncryptedDocumentException ex) {
        Logger.getLogger(FILES.class.getName()).log(Level.SEVERE, null, ex);
    }
    return p;
}

From source file:IO.FILES.java

public ArrayList<Persona> busquedaConFiltro(Busqueda v) {
    ArrayList<Persona> p = new ArrayList();
    try {//from w w  w.  j  a v a 2 s . c om
        Workbook wb = WorkbookFactory.create(new FileInputStream(new File(ruta)));
        Sheet hoja = wb.getSheetAt(0);
        Iterator rows = hoja.rowIterator();

        Row row = (Row) rows.next();
        while (rows.hasNext()) {
            row = (Row) rows.next();
            Persona p2 = toPersona(row);
            if (is(v, p2))
                p.add(p2);
        }
    } catch (IOException | InvalidFormatException | EncryptedDocumentException ex) {
        Logger.getLogger(FILES.class.getName()).log(Level.SEVERE, null, ex);
    }
    return p;
}

From source file:io.konig.schemagen.sql.SqlTableGeneratorTest.java

License:Apache License

@Test
public void testTextField() throws Exception {
    AwsShapeConfig.init();/* www . j  a  v  a  2  s  .  c  o m*/
    InputStream input = getClass().getClassLoader().getResourceAsStream("sql/SQL-DDL-Text.xlsx");
    Workbook book = WorkbookFactory.create(input);
    Graph graph = new MemoryGraph();
    NamespaceManager nsManager = new MemoryNamespaceManager();
    graph.setNamespaceManager(nsManager);

    WorkbookLoader loader = new WorkbookLoader(nsManager);
    File file = new File("src/test/resources/sql/test-datasource-params-bucket.xlsx");
    loader.load(book, graph, file);
    input.close();
    URI shapeId = uri("http://example.com/shapes/MDM_PRODUCT");

    ShapeManager s = loader.getShapeManager();

    Shape shape = s.getShapeById(shapeId);
    assertTrue(shape != null);
    ShapeWriter shapeWriter = new ShapeWriter();
    try {
        shapeWriter.writeTurtle(nsManager, shape, new File("target/test/sql/MDM_PRODUCT.ttl"));
    } catch (Exception e) {
        throw new KonigException(e);
    }

    load("target/test/sql/MDM_PRODUCT");

    assertTrue(shape != null);

    SqlTable table = generator.generateTable(shape, null);

    assertTrue(table != null);
    assertStringField(table, "PPID", SqlDatatype.VARCHAR, 2000);
    assertStringField(table, "PP_NAME", SqlDatatype.VARCHAR, 255);
    assertStringField(table, "ASSEMBLY_INSTRUCTIONS", SqlDatatype.VARCHAR, 1000);
    assertStringField(table, "LONG_DESCRIPTION", SqlDatatype.TEXT, 0);
}

From source file:io.konig.spreadsheet.WorkbookLoaderTest.java

License:Apache License

@Ignore
public void testJsonPathObjectArray() throws Exception {
    InputStream input = getClass().getClassLoader().getResourceAsStream("json-path-object-array.xlsx");
    Workbook book = WorkbookFactory.create(input);
    Graph graph = new MemoryGraph();
    NamespaceManager nsManager = new MemoryNamespaceManager();
    graph.setNamespaceManager(nsManager);

    WorkbookLoader loader = new WorkbookLoader(nsManager);
    loader.setFailOnErrors(false);//from  w  w  w  .  jav a  2  s . com
    loader.setFailOnWarnings(false);
    loader.load(book, graph);

    URI userShapeId = uri("https://example.com/shapes/USER_STG_Shape");
    URI addressShapeId = uri("https://example.com/shapes/USER_STG_.addressShape");
    URI addressId = uri("https://example.com/ns/alias/address");
    URI postalCodeId = uri("https://example.com/ns/alias/postalCode");

    ShapeManager shapeManager = loader.getShapeManager();

    Shape userShape = shapeManager.getShapeById(userShapeId);
    Shape addressShape = shapeManager.getShapeById(addressShapeId);

    assertTrue(userShape != null);
    PropertyConstraint address = userShape.getPropertyConstraint(addressId);
    assertTrue(address != null);
    assertTrue(address.getMaxCount() == null);

    Shape nestedAddressShape = address.getShape();
    assertTrue(nestedAddressShape == addressShape);

    assertTrue(addressShape != null);
    PropertyConstraint postalCode = addressShape.getPropertyConstraint(postalCodeId);
    assertTrue(postalCode != null);
    assertEquals(new Integer(1), postalCode.getMaxCount());
    assertEquals(XMLSchema.STRING, postalCode.getDatatype());
}