Example usage for org.apache.poi.xssf.usermodel XSSFWorkbook XSSFWorkbook

List of usage examples for org.apache.poi.xssf.usermodel XSSFWorkbook XSSFWorkbook

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFWorkbook XSSFWorkbook.

Prototype

public XSSFWorkbook(PackagePart part) throws IOException 

Source Link

Document

Constructs a XSSFWorkbook object using Package Part.

Usage

From source file:com.dfpray.formatter.CardModel.java

/**
 * Imports a .xlsx file and create a CardModel form it
 * @param path Path to file//from   w  ww  .j  av a 2s . c  om
 * @throws IOException
 */
public void importFromExcel(String path) throws IOException {
    String[] cardInfo = new String[32];
    FileInputStream file = new FileInputStream(new File(path));
    @SuppressWarnings("resource")
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    XSSFSheet sheet = workbook.getSheetAt(0);
    Row row;
    Cell cell;
    Iterator<Cell> cellIterator;
    Iterator<Row> rowIterator;
    int i;

    //Iterate through each rows one by one
    rowIterator = sheet.iterator();

    while (rowIterator.hasNext()) {
        row = rowIterator.next();
        cellIterator = row.cellIterator();

        i = 0;
        while (cellIterator.hasNext()) {
            cell = cellIterator.next();
            switch (cell.getCellType()) {

            case Cell.CELL_TYPE_BLANK:
                cardInfo[i] = " ";
                //System.out.println("Nothing");
                break;

            case Cell.CELL_TYPE_NUMERIC:
                cardInfo[i] = (Double.toString(cell.getNumericCellValue()));
                //System.out.println(Double.toString(cell.getNumericCellValue()));
                break;

            case Cell.CELL_TYPE_STRING:
                cardInfo[i] = cell.getStringCellValue();
                //System.out.println(cell.getStringCellValue());
                break;

            }
            i++;
        }
        //Create card and add it tho this 
        cards.add(new BusinessCard(cardInfo));
    }
    file.close();
}

From source file:com.docdoku.server.esindexer.ESTools.java

License:Open Source License

private static String microsoftExcelDocumentToString(InputStream inputStream)
        throws IOException, OpenXML4JException, XmlException {
    StringBuilder sb = new StringBuilder();
    try (InputStream excelStream = new BufferedInputStream(inputStream)) {
        if (POIFSFileSystem.hasPOIFSHeader(excelStream)) { // Before 2007 format files
            POIFSFileSystem excelFS = new POIFSFileSystem(excelStream);
            ExcelExtractor excelExtractor = new ExcelExtractor(excelFS);
            sb.append(excelExtractor.getText());
        } else { // New format
            XSSFWorkbook workBook = new XSSFWorkbook(excelStream);
            int numberOfSheets = workBook.getNumberOfSheets();
            for (int i = 0; i < numberOfSheets; i++) {
                XSSFSheet sheet = workBook.getSheetAt(0);
                Iterator<Row> rowIterator = sheet.rowIterator();
                while (rowIterator.hasNext()) {
                    XSSFRow row = (XSSFRow) rowIterator.next();
                    Iterator<Cell> cellIterator = row.cellIterator();
                    while (cellIterator.hasNext()) {
                        XSSFCell cell = (XSSFCell) cellIterator.next();
                        sb.append(cell.toString());
                        sb.append(" ");
                    }// w  w w  . j  av a2  s .c om
                    sb.append("\n");
                }
                sb.append("\n");
            }
        }
    }
    return sb.toString();
}

From source file:com.dotosoft.dotoquiz.tools.OldApp.java

License:Apache License

private void processPicasa() {
    if (DATA_TYPE.EXCEL.toString().equals(settings.getDataType())) {
        log.info("process data from excel!");
    } else if (DATA_TYPE.GOOGLESHEET.toString().equals(settings.getDataType())) {
        log.info("process data from googlesheet!");
    }//ww  w.  j  ava  2s  .  co m

    // variable for googlesheet
    GooglesheetClient googlesheetClient = null;
    WorksheetEntry fullSheet = null;

    // variable for excel
    FileInputStream file = null;
    XSSFWorkbook workbook = null;
    XSSFSheet sheet = null;

    // variable for DB
    Session session = null;
    Transaction trx = null;

    APPLICATION_TYPE type = APPLICATION_TYPE.valueOf(settings.getApplicationType());

    try {
        if (APPLICATION_TYPE.DB.toString().equals(settings.getApplicationType())) {
            session = HibernateUtil.getSessionFactory().openSession();
            trx = session.beginTransaction();
        }

        if (DATA_TYPE.EXCEL.toString().equals(settings.getDataType())) {
            file = new FileInputStream(settings.getSyncDataFile());
            workbook = new XSSFWorkbook(file);
        } else if (DATA_TYPE.GOOGLESHEET.toString().equals(settings.getDataType())) {
            googlesheetClient = new GooglesheetClient(auth, settings.getSyncDataFile());
        }

        int index = 0;

        // --------------------------------------------------------------------------
        // Extract Achievement ------------------------------------------------------
        // --------------------------------------------------------------------------

        // parent folder
        if (!APPLICATION_TYPE.SHOW_COLUMN_HEADER.toString().equals(settings.getApplicationType())) {
            DataTopicsParser topicAchievement = new DataTopicsParser(QuizParserConstant.ACHIEVEMENT_NAME,
                    QuizParserConstant.EMPTY_STRING, QuizParserConstant.EMPTY_STRING,
                    QuizParserConstant.EMPTY_STRING, QuizParserConstant.ACHIEVEMENT_NAME,
                    QuizParserConstant.ACHIEVEMENT_DESCRIPTION, QuizParserConstant.ACHIEVEMENT_IMAGE_URL,
                    QuizConstant.NO, new java.util.Date(), QuizConstant.SYSTEM_USER, QuizConstant.NO, type);
            topicAchievement = syncTopicToPicasa(topicAchievement);
            topicMapByTopicId.put(topicAchievement.getId(), topicAchievement);
        }

        List listRow = null;
        for (String achievementTab : settings.getStructure().getTabAchievements().split(";")) {
            String sheetName = "";
            if (DATA_TYPE.EXCEL.toString().equals(settings.getDataType())) {
                sheet = workbook.getSheetAt(Integer.parseInt(achievementTab));
                listRow = Lists.newArrayList(sheet.iterator());
                sheetName = sheet.getSheetName();
            } else if (DATA_TYPE.GOOGLESHEET.toString().equals(settings.getDataType())) {
                fullSheet = (WorksheetEntry) googlesheetClient.getWorksheet(Integer.parseInt(achievementTab));
                listRow = googlesheetClient.getListRows(fullSheet);
                sheetName = fullSheet.getTitle().getPlainText();
            }

            for (Object row : listRow) {
                if (showColumnHeader(row, sheetName))
                    break;

                ParameterAchievementParser achievement = DotoQuizStructure.convertDataToAchievement(row,
                        settings);

                if (achievement != null) {
                    if (type == APPLICATION_TYPE.DB) {
                        session.saveOrUpdate(achievement.toParameterAchievements());
                        log.info("Save or update achievement: " + achievement);
                    } else if (type == APPLICATION_TYPE.SYNC) {
                        achievement = syncAchievementToPicasa(achievement);

                        if (!QuizConstant.YES.equals(achievement.getIsProcessed())) {
                            GooglesheetClient.updateSyncPicasa(settings, QuizParserConstant.PARSE_ACHIEVEMENT,
                                    row, achievement.getPicasaId(), achievement.getImagePicasaUrl(),
                                    QuizConstant.YES);
                        }
                    }
                }
            }
        }

        trx = CommitDB(trx, session, true);

        for (String dataTab : settings.getStructure().getTabTopics().split(";")) {
            // --------------------------------------------------------------------------
            // Extract Topic
            // --------------------------------------------------------------------------
            index = 0;
            String sheetName = "";
            if (DATA_TYPE.EXCEL.toString().equals(settings.getDataType())) {
                sheet = workbook.getSheetAt(Integer.parseInt(dataTab));
                listRow = Lists.newArrayList(sheet.iterator());
                sheetName = sheet.getSheetName();
            } else if (DATA_TYPE.GOOGLESHEET.toString().equals(settings.getDataType())) {
                fullSheet = (WorksheetEntry) googlesheetClient.getWorksheet(Integer.parseInt(dataTab));
                listRow = googlesheetClient.getListRows(fullSheet);
                sheetName = fullSheet.getTitle().getPlainText();
            }

            for (Object row : listRow) {
                if (showColumnHeader(row, sheetName))
                    break;

                DataTopicsParser topic = DotoQuizStructure.convertDataToTopics(row, settings);

                if (topic != null) {
                    if (type == APPLICATION_TYPE.DB) {
                        if (StringUtils.hasValue(topic.getTopicParentId())) {
                            topic.setDatTopics(topicMapByTopicId.get(topic.getTopicParentId()).toDataTopics());
                        }
                        session.saveOrUpdate(topic.toDataTopics());
                        log.info("Save or update topic: " + topic);
                    } else if (type == APPLICATION_TYPE.SYNC) {
                        topic = syncTopicToPicasa(topic);

                        if (!QuizConstant.YES.equals(topic.getIsProcessed())) {
                            GooglesheetClient.updateSyncPicasa(settings, QuizParserConstant.PARSE_TOPIC, row,
                                    topic.getPicasaId(), topic.getImagePicasaUrl(), QuizConstant.YES);
                        }
                    }

                    topicMapByTopicId.put(topic.getId(), topic);
                }
            }
        }

        trx = CommitDB(trx, session, true);

        for (String dataTab : settings.getStructure().getTabQuestions().split(";")) {
            // --------------------------------------------------------------------------
            // Extract QuestionAnswers
            // --------------------------------------------------------------------------
            index = 0;
            String sheetName = "";
            if (DATA_TYPE.EXCEL.toString().equals(settings.getDataType())) {
                sheet = workbook.getSheetAt(Integer.parseInt(dataTab));
                listRow = Lists.newArrayList(sheet.iterator());
                sheetName = sheet.getSheetName();
            } else if (DATA_TYPE.GOOGLESHEET.toString().equals(settings.getDataType())) {
                fullSheet = (WorksheetEntry) googlesheetClient.getWorksheet(Integer.parseInt(dataTab));
                listRow = googlesheetClient.getListRows(fullSheet);
                sheetName = fullSheet.getTitle().getPlainText();
            }

            for (Object row : listRow) {
                if (showColumnHeader(row, sheetName))
                    break;

                DataQuestionsParser questionAnswer = DotoQuizStructure.convertDataToAnswerQuestion(row,
                        settings);

                if (questionAnswer != null) {
                    if (type == APPLICATION_TYPE.DB) {
                        questionAnswer.setMtQuestionType(HibernateUtil.getQuestionTypeByName(session,
                                questionAnswer.getQuestionTypeData()));
                        session.saveOrUpdate(questionAnswer.toDataQuestion());
                        log.info("Save or update QuestionAnswers: " + questionAnswer);
                        for (String topicId : questionAnswer.getTopics()) {
                            DataTopicsParser datTopic = topicMapByTopicId.get(topicId);
                            HibernateUtil.saveOrUpdateTopicQuestionData(session, datTopic.toDataTopics(),
                                    questionAnswer.toDataQuestion());
                        }
                    } else if (type == APPLICATION_TYPE.SYNC) {
                        questionAnswer = syncQuestionAnswersToPicasa(questionAnswer);
                        if (!QuizConstant.YES.equals(questionAnswer.getIsProcessed())) {
                            GooglesheetClient.updateSyncPicasa(settings,
                                    QuizParserConstant.PARSE_QUESTION_ANSWER, row, questionAnswer.getPicasaId(),
                                    questionAnswer.getImagePicasaUrl(), QuizConstant.YES);
                        }
                    }
                }
            }
        }

        trx = CommitDB(trx, session, false);

        if (DATA_TYPE.EXCEL.toString().equals(settings.getDataType())) {
            file.close();
            log.info("Save data to file...");
            FileOutputStream fos = new FileOutputStream(settings.getSyncDataFile());
            workbook.write(fos);
            fos.close();
        }

        log.info("Done");
    } catch (Exception e) {
        trx.rollback();
        session.close();

        e.printStackTrace();
    }

    System.exit(0);
}

From source file:com.dtec.validationgen.service.IoService.java

public XSSFWorkbook readExcel(String fileName) throws IOException {
    FileInputStream file = new FileInputStream(new File(fileName));
    return new XSSFWorkbook(file);
}

From source file:com.ebay.xcelite.Xcelite.java

License:Apache License

public Xcelite(File file) {
    try {/*from ww  w .  ja  v a  2  s  . c  om*/
        this.file = file;
        workbook = new XSSFWorkbook(new FileInputStream(file));
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.ebay.xcelite.Xcelite.java

License:Apache License

public Xcelite(InputStream is) {
    try {//from ww w  .j a  va  2 s . c o m
        this.is = is;
        workbook = new XSSFWorkbook(is);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.envisioncn.it.super_sonic.showcase.evaluation.utils.ExcelUtils.java

License:Open Source License

public static Workbook createWorkbook(String fileName, InputStream in) throws IOException {
    String type = initType(fileName);
    Workbook workbook = null;/*from w ww  .ja  v a2 s. c o  m*/

    if (type.equals(VERSION2003)) {
        workbook = new HSSFWorkbook(in);
    } else if (type.equals(VERSION2007)) {
        workbook = new XSSFWorkbook(in);
    }

    return workbook;
}

From source file:com.esd.cs.common.XExcelSheetParser.java

License:Open Source License

public XExcelSheetParser(File file) {
    try {/*from  w w  w  .ja v  a 2  s  . c om*/
        // ?workbook

        workbook = new XSSFWorkbook(new FileInputStream(file));
        // workbook = new HSSFWorkbook(new FileInputStream(file));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.esd.cs.worker.WorkerUtil.java

License:Open Source License

/**
 * excel:?2003?HSSFWorkbook,2007XSSFWorkbook.
 * 2003??HSSFWorkbook,??office2003 ??//from w ww.  j  a  v  a2 s  . c om
 * 
 * @param file
 * @return
 * @throws IOException
 * @throws FileNotFoundException
 */
public static Object hasParser(File file) throws FileNotFoundException, IOException {
    try {
        // ?workbook
        hWorkbook = new HSSFWorkbook(new FileInputStream(file));
        logger.info("excel 97-2003");
        return hWorkbook;
    } catch (Exception e) {
        xWorkbook = new XSSFWorkbook(new FileInputStream(file));
        logger.info("excel 2007-2010");
        return xWorkbook;

    }
}

From source file:com.Excel.Excel2007.java

private boolean abrirArchivo() {
    FileInputStream file;//w w w. ja v a2 s . co m
    try {
        file = new FileInputStream(new File(pathArchivo + "\\" + nombreArchivo));
        workbook = new XSSFWorkbook(file);
        return true;
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Excel2007.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Excel2007.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}