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:dk.cubing.liveresults.uploader.parser.WcaParser.java

License:Open Source License

@Override
protected boolean isValidSpreadsheet(Workbook workBook) throws IllegalStateException {
    boolean isValid = false;

    // get version information from registration sheet
    Sheet regSheet = workBook.getSheet(SHEET_TYPE_REGISTRATION);
    log.debug("Validating: {}", regSheet.getSheetName());
    isValid = isValidRegistrationSheet(regSheet);

    // continue validating result sheets
    if (isValid) {
        for (int i = 0; i < workBook.getNumberOfSheets(); i++) {
            Sheet sheet = workBook.getSheetAt(i);
            if (sheet != null && !SHEET_TYPE_REGISTRATION.equals(sheet.getSheetName())
                    && !SHEET_TYPE_DUMMY.equals(sheet.getSheetName())) {
                log.debug("Validating: {}", sheet.getSheetName());
                isValid = isValidResultSheet(sheet);
                if (!isValid) {
                    log.warn("[{}] Invalid result sheet", sheet.getSheetName());
                    break;
                }//from   w w  w .jav a 2 s .co m
            }
        }
    } else {
        log.warn("[{}] Invalid registration sheet", regSheet.getSheetName());
    }

    return isValid;
}

From source file:dk.cubing.liveresults.uploader.parser.WcaParser.java

License:Open Source License

@Override
protected Competition parseCompetitionDetails(Workbook workBook, Competition competition)
        throws IllegalStateException {
    if (competition.getName() == null) { // only used when automatic upload are disabled
        Sheet sheet = workBook.getSheet(SHEET_TYPE_REGISTRATION);
        if (isValidRegistrationSheet(sheet)) {
            Row row = sheet.getRow(0);/*from w w w.  ja v  a  2  s.com*/
            if (row != null) {
                Cell cell = row.getCell(0);
                if (cell != null && cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    competition.setName(cell.getStringCellValue());
                }
            }
        }
    }
    return competition;
}

From source file:dk.cubing.liveresults.uploader.parser.WcaParser.java

License:Open Source License

@Override
protected List<Competitor> parseCompetitors(Workbook workBook) throws IllegalStateException {
    List<Competitor> competitors = new CopyOnWriteArrayList<Competitor>();
    Sheet sheet = workBook.getSheet(SHEET_TYPE_REGISTRATION);
    if (isValidRegistrationSheet(sheet)) {
        parseEventNames(sheet);//from www.j av a  2s  .  c  om
        Row firstRow = sheet.getRow(3); // first row with competitor data
        if (firstRow != null) {
            Cell cell = firstRow.getCell(1); // first cell with competitor data
            if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) { // only parse sheet with content
                log.debug("Parsing: {}", sheet.getSheetName());
                for (Row row : sheet) {
                    if (row.getRowNum() > 2) {
                        Competitor competitor = parseCompetitorRow(row);
                        if (competitor != null) {
                            if (competitor.getRegisteredEvents().hasSignedUp()) {
                                competitors.add(competitor);
                            } else {
                                log.warn("[{}] No events registered for: {}", sheet.getSheetName(),
                                        competitor.getFirstname());
                            }
                        }
                    }
                }
            }
        }
    }
    return competitors;
}

From source file:dk.cubing.wcaspreadsheet.action.ScoresheetAction.java

License:Open Source License

/**
 * @param workBook// w  w w . j  a  va 2s.co m
 * @param format
 * @param timeFormat
 * @return
 */
private Sheet getResultSheet(Workbook workBook, String format, String timeFormat) {
    Sheet sheet = null;
    if (Event.Format.AVERAGE.getValue().equals(format)) {
        if (Event.TimeFormat.SECONDS.getValue().equals(timeFormat)) {
            sheet = workBook.getSheet(SHEET_TYPE_AVERAGE5S);
        } else if (Event.TimeFormat.MINUTES.getValue().equals(timeFormat)) {
            sheet = workBook.getSheet(SHEET_TYPE_AVERAGE5M);
        } else if (Event.TimeFormat.NUMBER.getValue().equals(timeFormat)) {
            log.error("Unsupported format: Fewest moves uses Best of and not Average.");
        } else if (Event.TimeFormat.MULTI_BLD.getValue().equals(timeFormat)) {
            log.error("Unsupported format: Multi BLD uses Best of and not Average.");
        } else {
            log.error("Unknown format: {}, time format: {}", format, timeFormat);
        }
    } else if (Event.Format.MEAN.getValue().equals(format)) {
        if (Event.TimeFormat.SECONDS.getValue().equals(timeFormat)) {
            sheet = workBook.getSheet(SHEET_TYPE_MEAN3S);
        } else if (Event.TimeFormat.MINUTES.getValue().equals(timeFormat)) {
            sheet = workBook.getSheet(SHEET_TYPE_MEAN3M);
        } else if (Event.TimeFormat.NUMBER.getValue().equals(timeFormat)) {
            log.error("Unsupported format: Fewest moves uses Best of and not Mean.");
        } else if (Event.TimeFormat.MULTI_BLD.getValue().equals(timeFormat)) {
            log.error("Unsupported format: Multi BLD uses Best of and not Mean.");
        } else {
            log.error("Unknown format: {}, time format: {}", format, timeFormat);
        }
    } else if (Event.Format.BEST_OF_1.getValue().equals(format)) {
        if (Event.TimeFormat.SECONDS.getValue().equals(timeFormat)) {
            sheet = workBook.getSheet(SHEET_TYPE_BEST1S);
        } else if (Event.TimeFormat.MINUTES.getValue().equals(timeFormat)) {
            sheet = workBook.getSheet(SHEET_TYPE_BEST1M);
        } else if (Event.TimeFormat.NUMBER.getValue().equals(timeFormat)) {
            sheet = workBook.getSheet(SHEET_TYPE_BEST1N);
        } else if (Event.TimeFormat.MULTI_BLD.getValue().equals(timeFormat)) {
            sheet = workBook.getSheet(SHEET_TYPE_MULTIBF1);
        } else {
            log.error("Unknown format: {}, time format: {}", format, timeFormat);
        }
    } else if (Event.Format.BEST_OF_2.getValue().equals(format)) {
        if (Event.TimeFormat.SECONDS.getValue().equals(timeFormat)) {
            sheet = workBook.getSheet(SHEET_TYPE_BEST2S);
        } else if (Event.TimeFormat.MINUTES.getValue().equals(timeFormat)) {
            sheet = workBook.getSheet(SHEET_TYPE_BEST2M);
        } else if (Event.TimeFormat.NUMBER.getValue().equals(timeFormat)) {
            log.error("Unsupported format: Fewest moves uses Best of 1 and not Best of 2.");
        } else if (Event.TimeFormat.MULTI_BLD.getValue().equals(timeFormat)) {
            sheet = workBook.getSheet(SHEET_TYPE_MULTIBF2);
        } else {
            log.error("Unknown format: {}, time format: {}", format, timeFormat);
        }
    } else if (Event.Format.BEST_OF_3.getValue().equals(format)) {
        if (Event.TimeFormat.SECONDS.getValue().equals(timeFormat)) {
            sheet = workBook.getSheet(SHEET_TYPE_BEST3S);
        } else if (Event.TimeFormat.MINUTES.getValue().equals(timeFormat)) {
            sheet = workBook.getSheet(SHEET_TYPE_BEST3M);
        } else if (Event.TimeFormat.NUMBER.getValue().equals(timeFormat)) {
            log.error("Unsupported format: Fewest moves uses Best of 1 and not Best of 3.");
        } else if (Event.TimeFormat.MULTI_BLD.getValue().equals(timeFormat)) {
            log.error("Unsupported format: Multi BLD uses Best of 1 or Best of 2 and not Best of 3.");
        } else {
            log.error("Unknown format: {}, time format: {}", format, timeFormat);
        }
    }
    return sheet;
}

From source file:edu.vt.owml.saurav.raininterpolation.debug.NewMain.java

License:Open Source License

/**
 * @param args the command line arguments
 *//*from w w w .jav a2s .  c o m*/
public static void main(String[] args) {
    try {

        Workbook wb;
        wb = WorkbookFactory.create(NewMain.class.getResourceAsStream("/unit_test.xlsx"));

        // retrieve the named range
        String cellname = "stations";
        int namedCellIdx = wb.getNameIndex(cellname);
        Name aNamedCell = wb.getNameAt(namedCellIdx);
        // retrieve the cell at the named range and test its contents
        AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula());
        CellReference[] crefs = (CellReference[]) aref.getAllReferencedCells();
        int index = 0;
        int columns = 2;
        double[][] stations = new double[(int) crefs.length / columns][2];
        for (CellReference cref : crefs) {
            Sheet s = wb.getSheet(cref.getSheetName());
            Row r = s.getRow(cref.getRow());
            Cell c = r.getCell(cref.getCol());
            System.out.println(c.getNumericCellValue());
            //2 col array
            stations[(int) (index / columns)][index % columns] = c.getNumericCellValue();
            index++;
        }
        printArray(stations);

        //rain
        cellname = "gridpts";
        namedCellIdx = wb.getNameIndex(cellname);
        aNamedCell = wb.getNameAt(namedCellIdx);
        // retrieve the cell at the named range and test its contents
        aref = new AreaReference(aNamedCell.getRefersToFormula());
        crefs = (CellReference[]) aref.getAllReferencedCells();
        index = 0;
        columns = 2;
        double[][] locations = new double[(int) crefs.length / columns][2];
        for (CellReference cref : crefs) {
            Sheet s = wb.getSheet(cref.getSheetName());
            Row r = s.getRow(cref.getRow());
            Cell c = r.getCell(cref.getCol());
            System.out.println(c.getNumericCellValue());
            //2 col array
            locations[(int) (index / columns)][index % columns] = c.getNumericCellValue();
            index++;
        }
        printArray(locations);

        //rain
        cellname = "rainVal";
        namedCellIdx = wb.getNameIndex(cellname);
        aNamedCell = wb.getNameAt(namedCellIdx);
        // retrieve the cell at the named range and test its contents
        aref = new AreaReference(aNamedCell.getRefersToFormula());
        crefs = (CellReference[]) aref.getAllReferencedCells();
        index = 0;
        double[] rainValues = new double[crefs.length];
        for (CellReference cref : crefs) {
            Sheet s = wb.getSheet(cref.getSheetName());
            Row r = s.getRow(cref.getRow());
            Cell c = r.getCell(cref.getCol());
            System.out.println(c.getNumericCellValue());
            //2 col array
            rainValues[index] = c.getNumericCellValue();
            index++;
        }
        printArray(rainValues);

        //vals
        cellname = "estimates";
        namedCellIdx = wb.getNameIndex(cellname);
        aNamedCell = wb.getNameAt(namedCellIdx);
        // retrieve the cell at the named range and test its contents
        aref = new AreaReference(aNamedCell.getRefersToFormula());
        crefs = (CellReference[]) aref.getAllReferencedCells();
        index = 0;
        double[] vals = new double[crefs.length];
        for (CellReference cref : crefs) {
            Sheet s = wb.getSheet(cref.getSheetName());
            Row r = s.getRow(cref.getRow());
            Cell c = r.getCell(cref.getCol());
            System.out.println(c.getNumericCellValue());
            //2 col array
            vals[index] = c.getNumericCellValue();
            index++;
        }
        printArray(vals);

        //distances
        cellname = "distances";
        namedCellIdx = wb.getNameIndex(cellname);
        aNamedCell = wb.getNameAt(namedCellIdx);
        // retrieve the cell at the named range and test its contents
        aref = new AreaReference(aNamedCell.getRefersToFormula());
        crefs = (CellReference[]) aref.getAllReferencedCells();
        index = 0;
        columns = stations.length;
        double[] d = new double[stations.length];
        List<double[]> distances = new ArrayList();
        for (CellReference cref : crefs) {

            Sheet s = wb.getSheet(cref.getSheetName());
            Row r = s.getRow(cref.getRow());
            Cell c = r.getCell(cref.getCol());
            System.out.println(c.getNumericCellValue());
            d[index % columns] = c.getNumericCellValue();
            if (index % columns == columns - 1) {
                distances.add(d);
                d = new double[stations.length];
            }
            index++;

        }
        printArray(distances);

        IDWInterpolator idw = new IDWInterpolator();
        // printArray(idw.getDistances(stations, locations));

    } catch (FileNotFoundException ex) {
        Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException | InvalidFormatException ex) {
        Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:egovframework.rte.fdl.excel.EgovExcelServiceTest.java

License:Apache License

/**
 * [Flow #-4]   ?  :  ? ? ?(Header, Footer)? 
 *//* w  w  w  .ja  va2 s .c  o m*/
@Test
public void testModifyDocAttribute() throws Exception {

    try {
        LOGGER.debug("testModifyDocAttribute start....");

        StringBuffer sb = new StringBuffer();
        sb.append(fileLocation).append("/").append("testModifyDocAttribute.xls");

        if (EgovFileUtil.isExistsFile(sb.toString())) {
            EgovFileUtil.delete(new File(sb.toString()));

            LOGGER.debug("Delete file....{}", sb.toString());
        }

        Workbook wbTmp = new HSSFWorkbook();
        wbTmp.createSheet();

        //  ? ?
        excelService.createWorkbook(wbTmp, sb.toString());

        //  ? 
        Workbook wb = excelService.loadWorkbook(sb.toString());
        LOGGER.debug("testModifyCellContents after loadWorkbook....");

        Sheet sheet = wb.createSheet("doc test sheet");

        Row row = sheet.createRow(1);
        Cell cell = row.createCell(1);
        cell.setCellValue(new HSSFRichTextString("Header/Footer Test"));

        // Header
        Header header = sheet.getHeader();
        header.setCenter("Center Header");
        header.setLeft("Left Header");
        header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") + HSSFHeader.fontSize((short) 16)
                + "Right Stencil-Normal Italic font and size 16");

        // Footer
        Footer footer = sheet.getFooter();
        footer.setCenter(HSSFHeader.font("Fixedsys", "Normal") + HSSFHeader.fontSize((short) 12) + "- 1 -");
        LOGGER.debug("Style is ...{}", HSSFHeader.font("Fixedsys", "Normal"),
                HSSFHeader.fontSize((short) 12) + "- 1 -");
        footer.setLeft("Left Footer");
        footer.setRight("Right Footer");

        //  ? 
        FileOutputStream out = new FileOutputStream(sb.toString());
        wb.write(out);
        out.close();

        assertTrue(EgovFileUtil.isExistsFile(sb.toString()));

        //////////////////////////////////////////////////////////////////////////
        // ?
        Workbook wbT = excelService.loadWorkbook(sb.toString());
        Sheet sheetT = wbT.getSheet("doc test sheet");

        Header headerT = sheetT.getHeader();
        assertEquals("Center Header", headerT.getCenter());
        assertEquals("Left Header", headerT.getLeft());
        assertEquals(HSSFHeader.font("Stencil-Normal", "Italic") + HSSFHeader.fontSize((short) 16)
                + "Right Stencil-Normal Italic font and size 16", headerT.getRight());

        Footer footerT = sheetT.getFooter();
        assertEquals("Right Footer", footerT.getRight());
        assertEquals("Left Footer", footerT.getLeft());
        assertEquals(HSSFHeader.font("Fixedsys", "Normal") + HSSFHeader.fontSize((short) 12) + "- 1 -",
                footerT.getCenter());

    } catch (Exception e) {
        LOGGER.error(e.toString());
        throw new Exception(e);
    } finally {
        LOGGER.debug("testModifyDocAttribute end....");
    }
}

From source file:egovframework.rte.fdl.excel.EgovExcelServiceTest.java

License:Apache License

/**
 * [Flow #-5]    :  ?? ?  ? ?  /*from  w  ww .j a v  a  2 s  .c o  m*/
 */
@Test
public void testGetCellContents() throws Exception {

    try {
        LOGGER.debug("testGetCellContents start....");

        StringBuffer sb = new StringBuffer();
        sb.append(fileLocation).append("/").append("testGetCellContents.xls");

        if (EgovFileUtil.isExistsFile(sb.toString())) {
            EgovFileUtil.delete(new File(sb.toString()));

            LOGGER.debug("Delete file....{}", sb.toString());
        }

        Workbook wbTmp = new HSSFWorkbook();
        wbTmp.createSheet();

        //  ? ?
        excelService.createWorkbook(wbTmp, sb.toString());

        //  ? 
        Workbook wb = excelService.loadWorkbook(sb.toString());
        LOGGER.debug("testGetCellContents after loadWorkbook....");

        Sheet sheet = wb.createSheet("cell test sheet");

        CellStyle cs = wb.createCellStyle();
        cs = wb.createCellStyle();
        cs.setWrapText(true);

        for (int i = 0; i < 100; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 5; j++) {
                Cell cell = row.createCell(j);
                cell.setCellValue(new HSSFRichTextString("row " + i + ", cell " + j));
                cell.setCellStyle(cs);
            }
        }

        //  ? 
        FileOutputStream out = new FileOutputStream(sb.toString());
        wb.write(out);
        out.close();

        //////////////////////////////////////////////////////////////////////////
        // ?
        Workbook wbT = excelService.loadWorkbook(sb.toString());
        Sheet sheetT = wbT.getSheet("cell test sheet");

        for (int i = 0; i < 100; i++) {
            Row row1 = sheetT.getRow(i);
            for (int j = 0; j < 5; j++) {
                Cell cell1 = row1.getCell(j);
                LOGGER.debug("row {}, cell {} : {}", i, j, cell1.getRichStringCellValue());
                assertEquals("row " + i + ", cell " + j, cell1.getRichStringCellValue().toString());
            }
        }

    } catch (Exception e) {
        LOGGER.error(e.toString());
        throw new Exception(e);
    } finally {
        LOGGER.debug("testGetCellContents end....");
    }
}

From source file:egovframework.rte.fdl.excel.EgovExcelServiceTest.java

License:Apache License

/**
 * [Flow #-6]  ?  :  ? ?(?, ? )? /*from  w w w. j av a 2 s. c  o  m*/
 */
@Test
public void testModifyCellAttribute() throws Exception {

    try {
        LOGGER.debug("testModifyCellAttribute start....");

        StringBuffer sb = new StringBuffer();
        sb.append(fileLocation).append("/").append("testModifyCellAttribute.xls");

        if (EgovFileUtil.isExistsFile(sb.toString())) {
            EgovFileUtil.delete(new File(sb.toString()));

            LOGGER.debug("Delete file....{}", sb.toString());
        }

        Workbook wbTmp = new HSSFWorkbook();
        wbTmp.createSheet();

        //  ? ?
        excelService.createWorkbook(wbTmp, sb.toString());

        //  ? 
        Workbook wb = excelService.loadWorkbook(sb.toString());
        LOGGER.debug("testModifyCellAttribute after loadWorkbook....");

        Sheet sheet = wb.createSheet("cell test sheet2");
        //           sheet.setColumnWidth((short) 3, (short) 200);   // column Width

        CellStyle cs = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontHeight((short) 16);
        font.setBoldweight((short) 3);
        font.setFontName("fixedsys");

        cs.setFont(font);
        cs.setAlignment(CellStyle.ALIGN_RIGHT); // cell 
        cs.setWrapText(true);

        for (int i = 0; i < 100; i++) {
            Row row = sheet.createRow(i);
            //              row.setHeight((short)300); // row? height 

            for (int j = 0; j < 5; j++) {
                Cell cell = row.createCell(j);
                cell.setCellValue(new HSSFRichTextString("row " + i + ", cell " + j));
                cell.setCellStyle(cs);
            }
        }

        //  ? 
        FileOutputStream out = new FileOutputStream(sb.toString());
        wb.write(out);
        out.close();

        //////////////////////////////////////////////////////////////////////////
        // ?
        Workbook wbT = excelService.loadWorkbook(sb.toString());
        Sheet sheetT = wbT.getSheet("cell test sheet2");
        LOGGER.debug("getNumCellStyles : {}", wbT.getNumCellStyles());

        CellStyle cs1 = wbT.getCellStyleAt((short) (wbT.getNumCellStyles() - 1));

        Font fontT = ((HSSFCellStyle) cs1).getFont(wbT);
        LOGGER.debug("font getFontHeight : {}", fontT.getFontHeight());
        LOGGER.debug("font getBoldweight : {}", fontT.getBoldweight());
        LOGGER.debug("font getFontName : {}", fontT.getFontName());
        LOGGER.debug("getAlignment : {}", cs1.getAlignment());
        LOGGER.debug("getWrapText : {}", cs1.getWrapText());

        for (int i = 0; i < 100; i++) {
            Row row1 = sheetT.getRow(i);
            for (int j = 0; j < 5; j++) {
                Cell cell1 = row1.getCell(j);
                LOGGER.debug("row {}, cell {} : {}", i, j, cell1.getRichStringCellValue());
                assertEquals(16, fontT.getFontHeight());
                assertEquals(3, fontT.getBoldweight());
                assertEquals(CellStyle.ALIGN_RIGHT, cs1.getAlignment());
                assertTrue(cs1.getWrapText());
            }
        }

    } catch (Exception e) {
        LOGGER.error(e.toString());
        throw new Exception(e);
    } finally {
        LOGGER.debug("testModifyCellAttribute end....");
    }
}

From source file:egovframework.rte.fdl.excel.EgovExcelXSSFServiceTest.java

License:Apache License

/**
 * [Flow #-4]   ?  :  ? ? ?(Header, Footer)? 
 *///from  w  w w .  java2s . c om
@Test
public void testModifyDocAttribute() throws Exception {

    try {
        LOGGER.debug("testModifyDocAttribute start....");

        StringBuffer sb = new StringBuffer();
        sb.append(fileLocation).append("/").append("testModifyDocAttribute.xlsx");

        if (EgovFileUtil.isExistsFile(sb.toString())) {
            EgovFileUtil.delete(new File(sb.toString()));

            LOGGER.debug("Delete file....{}", sb.toString());
        }

        Workbook wbTmp = new XSSFWorkbook();
        wbTmp.createSheet();

        //  ? ?
        excelService.createWorkbook(wbTmp, sb.toString());

        //  ? 
        Workbook wb = excelService.loadWorkbook(sb.toString(), new XSSFWorkbook());
        LOGGER.debug("testModifyCellContents after loadWorkbook....");

        Sheet sheet = wb.createSheet("doc test sheet");

        Row row = sheet.createRow(1);
        Cell cell = row.createCell(1);
        cell.setCellValue(new XSSFRichTextString("Header/Footer Test"));

        // Header
        Header header = sheet.getHeader();
        header.setCenter("Center Header");
        header.setLeft("Left Header");
        header.setRight(XSSFOddHeader.stripFields("&IRight Stencil-Normal Italic font and size 16"));

        // Footer
        Footer footer = (XSSFOddFooter) sheet.getFooter();
        footer.setCenter(XSSFOddHeader.stripFields("Fixedsys"));
        LOGGER.debug("Style is ... {}", XSSFOddHeader.stripFields("Fixedsys"));
        footer.setLeft("Left Footer");
        footer.setRight("Right Footer");

        //  ? 
        FileOutputStream out = new FileOutputStream(sb.toString());
        wb.write(out);
        out.close();

        assertTrue(EgovFileUtil.isExistsFile(sb.toString()));

        //////////////////////////////////////////////////////////////////////////
        // ?
        Workbook wbT = excelService.loadWorkbook(sb.toString(), new XSSFWorkbook());
        Sheet sheetT = wbT.getSheet("doc test sheet");

        Header headerT = sheetT.getHeader();
        assertEquals("Center Header", headerT.getCenter());
        assertEquals("Left Header", headerT.getLeft());
        assertEquals(XSSFOddHeader.stripFields("Right Stencil-Normal Italic font and size 16"),
                headerT.getRight());

        Footer footerT = sheetT.getFooter();
        assertEquals("Right Footer", footerT.getRight());
        assertEquals("Left Footer", footerT.getLeft());
        assertEquals(XSSFOddHeader.stripFields("Fixedsys"), footerT.getCenter());

    } catch (Exception e) {
        LOGGER.error(e.toString());
        throw new Exception(e);
    } finally {
        LOGGER.debug("testModifyDocAttribute end....");
    }
}

From source file:egovframework.rte.fdl.excel.EgovExcelXSSFServiceTest.java

License:Apache License

/**
 * [Flow #-5]    :  ?? ?  ? ?  /* w w w  . ja  va  2  s  .  c om*/
 */
@Test
public void testGetCellContents() throws Exception {

    try {
        LOGGER.debug("testGetCellContents start....");

        StringBuffer sb = new StringBuffer();
        sb.append(fileLocation).append("/").append("testGetCellContents.xlsx");

        if (EgovFileUtil.isExistsFile(sb.toString())) {
            EgovFileUtil.delete(new File(sb.toString()));

            LOGGER.debug("Delete file....{}", sb.toString());
        }

        Workbook wbTmp = new XSSFWorkbook();
        wbTmp.createSheet();

        //  ? ?
        excelService.createWorkbook(wbTmp, sb.toString());

        //  ? 
        Workbook wb = excelService.loadWorkbook(sb.toString(), new XSSFWorkbook());
        LOGGER.debug("testGetCellContents after loadWorkbook....");

        Sheet sheet = wb.createSheet("cell test sheet");

        CellStyle cs = wb.createCellStyle();
        cs = wb.createCellStyle();
        cs.setWrapText(true);

        for (int i = 0; i < 100; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 5; j++) {
                Cell cell = row.createCell(j);
                cell.setCellValue(new XSSFRichTextString("row " + i + ", cell " + j));
                cell.setCellStyle(cs);
            }
        }

        //  ? 
        FileOutputStream out = new FileOutputStream(sb.toString());
        wb.write(out);
        out.close();

        //////////////////////////////////////////////////////////////////////////
        // ?
        Workbook wbT = excelService.loadWorkbook(sb.toString(), new XSSFWorkbook());
        Sheet sheetT = wbT.getSheet("cell test sheet");

        for (int i = 0; i < 100; i++) {
            Row row1 = sheetT.getRow(i);
            for (int j = 0; j < 5; j++) {
                Cell cell1 = row1.getCell(j);
                LOGGER.debug("row {}, cell : {}", i, j, cell1.getRichStringCellValue());
                assertEquals("row " + i + ", cell " + j, cell1.getRichStringCellValue().toString());
            }
        }

    } catch (Exception e) {
        LOGGER.error(e.toString());
        throw new Exception(e);
    } finally {
        LOGGER.debug("testGetCellContents end....");
    }
}