Example usage for org.apache.poi.ss.usermodel Workbook getSheet

List of usage examples for org.apache.poi.ss.usermodel Workbook getSheet

Introduction

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

Prototype

Sheet getSheet(String name);

Source Link

Document

Get sheet with the given name

Usage

From source file:egovframework.rte.fdl.excel.impl.EgovExcelServiceImpl.java

License:Apache License

/**
 * ??  DB? ?.<br/>/*  www.  j a va 2 s. c o m*/
 *  ?      .
 * 
 * @param queryId
 * @param fileIn
 * @param sheetName
 * @param start (default : 0)
 * @param commitCnt (default : 0)
 * @return
 * @throws Exception
 */
public Integer uploadExcel(String queryId, InputStream fileIn, String sheetName, int start, long commitCnt)
        throws BaseException, Exception {
    Workbook wb = loadWorkbook(fileIn);
    Sheet sheet = wb.getSheet(sheetName);

    return uploadExcel(queryId, sheet, start, commitCnt);
}

From source file:info.informationsea.java.excel2csv.ConverterTest.java

License:Open Source License

@Test
public void testDoConvert() throws Exception {
    Path temporaryOutput = Files.createTempFile("excel2csv", ".xlsx");
    Converter.builder().copyAllSheets(true).build()
            .doConvert(Arrays.asList(new File(temporaryDirectory.toFile(), "CO2.csv"),
                    new File(temporaryDirectory.toFile(), "DNase.csv"),
                    new File(temporaryDirectory.toFile(), "iris.csv")), temporaryOutput.toFile());

    Workbook workbook = WorkbookFactory.create(temporaryOutput.toFile());
    for (String one : new String[] { "iris", "CO2", "DNase" }) {
        try (TableReader reader = new ExcelSheetReader(workbook.getSheet(one + ".csv"))) {
            Excel2CSVTest.assertObjects(reference.get(one), reader.readAll());
        }//  w  w w.  j a va 2s  . c  o m
    }
}

From source file:info.informationsea.java.excel2csv.ConverterTest.java

License:Open Source License

@Test
public void testDoConvert2() throws Exception {
    Path temporaryOutput = Files.createTempFile("excel2csv", ".xlsx");
    Converter.builder().copyAllSheets(true).build()
            .doConvert(/*from  www .ja  va2 s .  co m*/
                    Arrays.asList(new File(temporaryDirectory.toFile(), "multisheet.xls"),
                            new File(temporaryDirectory.toFile(), "multisheet.xlsx")),
                    temporaryOutput.toFile());

    Workbook workbook = WorkbookFactory.create(temporaryOutput.toFile());
    for (String one : new String[] { "iris", "CO2", "DNase" }) {
        for (String suffix : new String[] { "", "-1" }) {
            try (TableReader reader = new ExcelSheetReader(workbook.getSheet(one + suffix))) {
                Excel2CSVTest.assertObjects(reference.get(one), reader.readAll());
            }
        }
    }
}

From source file:info.informationsea.java.excel2csv.ConverterTest.java

License:Open Source License

@Test
public void testDoConvert3() throws Exception {
    Path temporaryOutput = Files.createTempFile("excel2csv", ".xlsx");
    Converter.builder().copyAllSheets(true).build().doConvert(
            Collections.singletonList(new File(temporaryDirectory.toFile(), "multisheet.xls")),
            temporaryOutput.toFile());/* w ww  . ja  va 2 s .  com*/

    Workbook workbook = WorkbookFactory.create(temporaryOutput.toFile());
    for (String one : new String[] { "iris", "CO2", "DNase" }) {
        try (TableReader reader = new ExcelSheetReader(workbook.getSheet(one))) {
            Excel2CSVTest.assertObjects(reference.get(one), reader.readAll());
        }
    }
}

From source file:info.informationsea.java.excel2csv.ConverterTest.java

License:Open Source License

@Test
public void testDoConvert4() throws Exception {
    Path temporaryOutput = Files.createTempFile("excel2csv", ".xlsx");
    Converter.builder().copyAllSheets(false).outputSheetName("iris").build().doConvert(
            Collections.singletonList(new File(temporaryDirectory.toFile(), "multisheet.xls")),
            temporaryOutput.toFile());/*from ww w.  ja va2s  .c o m*/

    Workbook workbook = WorkbookFactory.create(temporaryOutput.toFile());
    Assert.assertEquals(1, workbook.getNumberOfSheets());
    for (String one : new String[] { "iris" }) {
        log.info("Sheet name: {}", workbook.getSheetName(0));
        try (TableReader reader = new ExcelSheetReader(workbook.getSheet(one))) {
            Excel2CSVTest.assertObjects(reference.get(one), reader.readAll());
        }
    }
}

From source file:info.informationsea.java.excel2csv.ConverterTest.java

License:Open Source License

@Test
public void testDoConvert5() throws Exception {
    Path temporaryOutput = Files.createTempFile("excel2csv", ".xlsx");
    Converter.builder().copyAllSheets(false).inputSheetName("DNase").outputSheetName("DNase").build().doConvert(
            Collections.singletonList(new File(temporaryDirectory.toFile(), "multisheet.xls")),
            temporaryOutput.toFile());//from   www  . jav a 2 s .  c  om

    Workbook workbook = WorkbookFactory.create(temporaryOutput.toFile());
    Assert.assertEquals(1, workbook.getNumberOfSheets());
    for (String one : new String[] { "DNase" }) {
        try (TableReader reader = new ExcelSheetReader(workbook.getSheet(one))) {
            Excel2CSVTest.assertObjects(reference.get(one), reader.readAll());
        }
    }
}

From source file:info.informationsea.java.excel2csv.ConverterTest.java

License:Open Source License

@Test
public void testDoConvert6() throws Exception {
    Path temporaryOutput = Files.createTempFile("excel2csv", ".xlsx");
    Converter.builder().copyAllSheets(false).inputSheetIndex(2).outputSheetName("DNase").build().doConvert(
            Collections.singletonList(new File(temporaryDirectory.toFile(), "multisheet.xls")),
            temporaryOutput.toFile());/*w  ww  .java  2  s.c o  m*/

    Workbook workbook = WorkbookFactory.create(temporaryOutput.toFile());
    Assert.assertEquals(1, workbook.getNumberOfSheets());
    for (String one : new String[] { "DNase" }) {
        try (TableReader reader = new ExcelSheetReader(workbook.getSheet(one))) {
            Excel2CSVTest.assertObjects(reference.get(one), reader.readAll());
        }
    }
}

From source file:info.informationsea.java.excel2csv.Utilities.java

License:Open Source License

public static TableReader openReader(File inputFile, int sheetIndex, String sheetName) throws IOException {
    if (inputFile == null) {
        return new TableCSVReader(new InputStreamReader(System.in), new TabDelimitedFormat());
    } else {//from  ww  w .  ja va2 s  .co  m
        FileType type = suggestFileTypeFromName(inputFile.getName());
        switch (type) {
        case FILETYPE_XLS:
        case FILETYPE_XLSX: {
            Workbook workbook;
            if (type == FileType.FILETYPE_XLSX)
                workbook = new XSSFWorkbook(new FileInputStream(inputFile));
            else
                workbook = new HSSFWorkbook(new FileInputStream(inputFile));

            if (sheetName != null)
                return new ExcelSheetReader(workbook.getSheet(sheetName));
            else
                return new ExcelSheetReader(workbook.getSheetAt(sheetIndex));
        }
        case FILETYPE_CSV:
            return new TableCSVReader(new FileReader(inputFile), new DefaultFormat());
        case FILETYPE_TAB:
        default:
            return new TableCSVReader(new FileReader(inputFile), new TabDelimitedFormat());
        }
    }
}

From source file:info.informationsea.java.excel2csv.Utilities.java

License:Open Source License

public static Sheet createUniqueNameSheetForWorkbook(Workbook workbook, String sheetName, boolean overwrite) {
    if (overwrite) {
        int index = workbook.getSheetIndex(workbook.getSheet(sheetName));
        if (index >= 0)
            workbook.removeSheetAt(index);
        return workbook.createSheet(sheetName);
    }/*from   w  ww . j  a  v  a  2  s. c  o  m*/

    String realSheetName = sheetName;
    int index = 1;
    Sheet sheet;
    while (true) {
        try {
            sheet = workbook.createSheet(realSheetName);
            break;
        } catch (IllegalArgumentException e) {
            realSheetName = sheetName + "-" + index++;
            if (index > 20) {
                throw e;
            }
        }
    }
    return sheet;
}

From source file:info.informationsea.tableio.excel.test.ExcelWorkbookWriterTest.java

License:Open Source License

@Test
public void testXlsxWorkbookWriter() throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try (ExcelWorkbookWriter workbookWriter = new XlsxWorkbookWriter(outputStream)) {
        commonTestExcelWorkbookWriter(workbookWriter);
    }/*w ww .j a  v  a 2 s.c o  m*/

    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    Workbook workbook = new XSSFWorkbook(inputStream);
    assertData(new ExcelSheetReader(workbook.getSheet("table1")), data[0]);
    assertData(new ExcelSheetReader(workbook.getSheet("table2")), data[1]);
}