Example usage for org.apache.poi.xssf.usermodel XSSFSheet getRow

List of usage examples for org.apache.poi.xssf.usermodel XSSFSheet getRow

Introduction

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

Prototype

@Override
public XSSFRow getRow(int rownum) 

Source Link

Document

Returns the logical row ( 0-based).

Usage

From source file:parser.CloudDSFParser.java

License:Apache License

/**
 * Retrieves influencing relations between tasks and decisions.
 *///from ww  w. j a v a 2 s  .c o  m
private void setInfluencingTasks() {
    XSSFSheet sheet = workbook.getSheet("Task Level");
    // Column A has name of start Task
    int startTaskColumn = 0;
    // Row 1 has names of endDecision
    Row endDecisionRow = sheet.getRow(1);
    // Iterate over all rows
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        XSSFRow row = (XSSFRow) rows.next();
        Iterator<Cell> cells = row.cellIterator();
        while (cells.hasNext()) {
            XSSFCell cell = (XSSFCell) cells.next();
            if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
                // Depending on the relation type source and target are set
                // accordingly
                String relationName = cell.getStringCellValue();
                String sourceDesc = row.getCell(startTaskColumn).getStringCellValue();
                String targetDesc = endDecisionRow.getCell(cell.getColumnIndex()).getStringCellValue();
                switch (relationName) {
                case "Affecting":
                    cdsf.setTaskRelation(sourceDesc, targetDesc, "oneWay");
                    break;
                case "Both":
                    cdsf.setTaskRelation(sourceDesc, targetDesc, "twoWay");
                    break;
                case "Affected":
                    cdsf.setTaskRelation(sourceDesc, targetDesc, "backwards");
                    break;
                // no default
                }
            }
        }
    }
}

From source file:parser.CloudDSFParser.java

License:Apache License

/**
 * Retrieve defined tasks./*from  w  w  w . j av a 2s  .  c o m*/
 */
private void setTasks() {
    XSSFSheet sheet = workbook.getSheet("Task Level");
    // start with fixed task id
    int taskId = 901;
    // iterate over all rows skipping headline
    for (int j = 2; j <= sheet.getLastRowNum(); j++) {
        // row 2 gets selected
        Row row = sheet.getRow(j);
        // select cell A
        Cell cell = row.getCell(0);
        // create new task
        Task task = new Task(taskId, cell.getStringCellValue());
        taskId++;
        cdsf.addTask(task);
    }
}

From source file:parser.CloudDSFPlusParser.java

License:Apache License

/**
 * Retrieves the knowledge base for the CloudDSFPlus from the sheet and the relations.
 *
 * @return CloudDSFPlus object//from  w w  w  .  j a v  a2s. c o  m
 */
public CloudDSF readExcel() {
    // Get desired sheet from the workbook
    XSSFSheet sheet = workbook.getSheet("Knowledge Base");

    // setup variable
    String decisionName = "";
    String decisionPointName = "";

    DecisionPoint decisionPoint;
    Decision decision;
    Outcome outcome;

    int decisionPointId = 0;
    int decisionId = 0;
    int outcomeId = 0;

    // iterate over all rows skipping headline
    for (int j = 1; j <= sheet.getLastRowNum(); j++) {
        // row 2 gets selected
        Row row = sheet.getRow(j);
        // select cell A
        Cell cell = row.getCell(dpCol);
        // if not empty than new decision Point
        if (cell.getStringCellValue().equals("") == false) {
            decisionPointId++;
            decisionId = decisionPointId * 100 + 1;
            outcomeId = decisionId * 100 + 1;
            decisionPointName = cell.getStringCellValue();
            // create new DecisionPoint
            decisionPoint = generateDecisionPoint(cell, decisionPointId, row);
            // create new Decision
            Cell decisionCell = row.getCell(decCol);
            decisionName = decisionCell.getStringCellValue();
            decision = generateDecision(decisionCell, decisionId, decisionPointId, row);
            // create new outcome
            Cell outcomeCell = row.getCell(outCol);
            outcome = generateOutcome(outcomeCell, decisionId, decisionPointId, outcomeId, row);
            // add outcome to decision
            decision.addOutcome(outcome);
            // add decision to decisionPoint
            decisionPoint.addDecision(decision);
            // add decisionPoint to cloudDSFPlus
            cdsf.addDecisionPoint(decisionPoint);
        } else {
            // Select Cell B
            Cell decisionCell = row.getCell(decCol);
            // if text than new decision
            if (decisionCell.getStringCellValue().equals("") == false) {
                decisionId++;
                outcomeId = decisionId * 100 + 1;
                // create new decision
                decisionName = decisionCell.getStringCellValue();
                decision = generateDecision(decisionCell, decisionId, decisionPointId, row);
                // create new outcome
                Cell outcomeCell = row.getCell(outCol);
                outcome = generateOutcome(outcomeCell, decisionId, decisionPointId, outcomeId, row);
                // add outcome to decision
                decision.addOutcome(outcome);
                // add decision to current decision point
                cdsf.getDecisionPoint(decisionPointName).addDecision(decision);
            } else {
                // if no text in dp or d than new outcome
                outcomeId++;
                // create new outcome
                Cell outcomeCell = row.getCell(outCol);
                outcome = generateOutcome(outcomeCell, decisionId, decisionPointId, outcomeId, row);
                // add outcome to current decision in current decision point
                cdsf.getDecisionPoint(decisionPointName).getDecision(decisionName).addOutcome(outcome);
            }
        }
    }
    // retrive relations
    setInfluencingRelations();
    setRequiringRelations();
    setInfluencingOutcomes();
    // sorting
    cdsf.sortEntities();
    cdsf.sortLists();
    return cdsf;

}

From source file:parser.CloudDSFPlusParser.java

License:Apache License

/**
 * Retrieves influencing relations between decisions.
 * // ww  w .  j a va  2s .  c om
 * @return
 */
private void setInfluencingRelations() {
    XSSFSheet sheet = workbook.getSheet("Decision Level");
    // Column B has name of start Decision
    int startDecisionColumn = 1;
    // Row 1 has names of endDecision
    Row endDecisionRow = sheet.getRow(1);
    // Iterate over all rows starting at 3
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        XSSFRow row = (XSSFRow) rows.next();
        // select cell C
        Iterator<Cell> cells = row.cellIterator();
        // Iterate of all cells in row
        while (cells.hasNext()) {
            XSSFCell cell = (XSSFCell) cells.next();
            String relationType = cell.getStringCellValue();
            if (relationType.equals("Influencing") || relationType.equals("Affecting")
                    || relationType.equals("Binding")) {
                // if type of relationship matches predefined values get names of the two participating
                // decisions
                String startDecision = row.getCell(startDecisionColumn).getStringCellValue();
                String endDecision = endDecisionRow.getCell(cell.getColumnIndex()).getStringCellValue();
                // add decision relation to cloudDSFPlus
                cdsf.setDecisionRelation(startDecision, endDecision, relationType, null);
            }
        }
    }
}

From source file:parser.CloudDSFPlusParser.java

License:Apache License

/**
 * Retrieves requiring relations between decisions.
 * //  w  w  w  .j a  va2s . c  o m
 * @return
 */
private void setRequiringRelations() {
    XSSFSheet sheet = workbook.getSheet("Required Level");
    // Column B has name of start Decision
    int startDecisionColumn = 1;
    // Row 1 has names of endDecision
    Row endDecisionRow = sheet.getRow(1);
    // Iterate over all rows starting at 3
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        XSSFRow row = (XSSFRow) rows.next();
        Iterator<Cell> cells = row.cellIterator();
        while (cells.hasNext()) {
            XSSFCell cell = (XSSFCell) cells.next();
            String relationType = cell.getStringCellValue();
            if (relationType.equals("Requiring")) {
                // if requiring relationship is denoted get names of both decisions
                String startDecision = row.getCell(startDecisionColumn).getStringCellValue();
                String endDecision = endDecisionRow.getCell(cell.getColumnIndex()).getStringCellValue();
                // add requiring relation to cloudDSFPlus
                cdsf.setDecisionRelation(startDecision, endDecision, relationType, null);
            }
        }
    }
}

From source file:parser.CloudDSFPlusParser.java

License:Apache License

/**
 * Retrieves relations between outcomes.
 * /* www .  j  a  v a  2s  .c o m*/
 * @return
 */
private void setInfluencingOutcomes() {
    XSSFSheet sheet = workbook.getSheet("Outcome Level");
    // Column B has name of start Decision
    int startOutcomeColumn = 1;
    // Row 1 has names of endDecision
    Row endOutcomeRow = sheet.getRow(0);
    // Iterate over all rows
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        XSSFRow row = (XSSFRow) rows.next();
        Iterator<Cell> cells = row.cellIterator();
        // Iterate over all cells
        while (cells.hasNext()) {
            XSSFCell cell = (XSSFCell) cells.next();
            String relationType = cell.getStringCellValue();
            if (relationType.equals("in") || relationType.equals("ex") || relationType.equals("a")
                    || relationType.equals("eb") || relationType.equals("aff")) {
                // if relationship is denoted get names of both outcomes
                String startOutcome = row.getCell(startOutcomeColumn).getStringCellValue();
                String endOutcome = endOutcomeRow.getCell(cell.getColumnIndex()).getStringCellValue();
                // add new outcome relation to cloudDSFPlus
                cdsf.setOutcomeRelation(startOutcome, endOutcome, relationType, null, null);
            }
        }
    }
}

From source file:pt.webdetails.cda.exporter.PivotXlsExporter.java

License:Open Source License

private void writePivotColumns(MetadataTableModel table, XSSFSheet sheet, PivotTableData pivotTableData,
        String[] pivotGroupColumns) {
    // create first header row
    CellStyle headerCellStyle = sheet.getRow(0).getCell(0).getCellStyle();
    Row header = sheet.createRow(0);//from  www  .  ja  va2 s .  c  o  m
    boolean processingPivotColumns = false;
    int columnsToCreateIndex = 0;
    while (true) {
        if (!processingPivotColumns) {
            Cell cell = header.createCell(columnsToCreateIndex);
            cell.setCellStyle(headerCellStyle);
            cell.setCellValue("");
            if (pivotTableData.lastFixedColumnIndex == columnsToCreateIndex) {
                processingPivotColumns = true;
            } else {
                ++columnsToCreateIndex;
            }
        } else {
            // create one column group for each columnGroupTitle

            for (String groupTitle : pivotTableData.groupTitleSet) {
                boolean writeTitle = true;
                for (String columnTitle : pivotGroupColumns) {
                    Cell cell = header.createCell(++columnsToCreateIndex);
                    cell.setCellStyle(headerCellStyle);
                    if (writeTitle) {
                        cell.setCellValue(groupTitle);
                        writeTitle = false;
                    }
                }
            }
            break;
        }
    }
    // create second header row
    header = sheet.createRow(1);
    processingPivotColumns = false;
    columnsToCreateIndex = 0;
    while (true) {
        if (!processingPivotColumns) {
            Cell cell = header.createCell(columnsToCreateIndex);
            cell.setCellStyle(headerCellStyle);
            cell.setCellValue(table.getColumnName(columnsToCreateIndex));
            if (pivotTableData.lastFixedColumnIndex == columnsToCreateIndex) {
                processingPivotColumns = true;
            } else {
                ++columnsToCreateIndex;
            }
        } else {
            // create one column group for each columnGroupTitle
            for (String groupTitle : pivotTableData.groupTitleSet) {
                for (String columnTitle : pivotGroupColumns) {
                    Cell cell = header.createCell(++columnsToCreateIndex);
                    cell.setCellStyle(headerCellStyle);
                    cell.setCellValue(columnTitle);
                }
            }
            break;
        }
    }
    // finish creating header rows
    sheet.createFreezePane(0, 2);
    pivotTableData.pivotTableColumnNumber = columnsToCreateIndex + 1;
    logger.debug("columns lastFixedColumnIndex=" + pivotTableData.lastFixedColumnIndex + " columnNumber="
            + pivotTableData.pivotTableColumnNumber);
}

From source file:pt.webdetails.cda.exporter.PivotXlsExporter.java

License:Open Source License

private void writePivotRows(XSSFSheet sheet, PivotTableData pivotTableData, String[] pivotGroupColumns) {
    // create sheet content
    CellStyle rowCellStyle = sheet.getRow(2).getCell(0).getCellStyle();
    int sheetRowIdx = 2;
    for (String rowGroupSelector : pivotTableData.rowGroupSelectors) {
        Row row = sheet.createRow(sheetRowIdx++);
        int sheetRowColumnIdx = 0;
        for (String rowGroupDataValue : pivotTableData.rowGroupData.get(rowGroupSelector)) {
            Cell cell = row.createCell(sheetRowColumnIdx++);
            cell.setCellStyle(rowCellStyle);
            cell.setCellValue(rowGroupDataValue);
        }//  w w  w  .j  a  va  2  s .  co m
        Map<String, List<String>> currentRow = pivotTableData.pivotData.get(rowGroupSelector);
        for (String columnGroupTitle : pivotTableData.groupTitleSet) {
            List<String> columnGroupData = currentRow.get(columnGroupTitle);
            if (columnGroupData != null) {
                for (String columnData : columnGroupData) {
                    Cell cell = row.createCell(sheetRowColumnIdx++);
                    cell.setCellStyle(rowCellStyle);
                    cell.setCellValue(columnData);
                }
            } else {
                // no data for this column group in this row
                for (String columnTitle : pivotGroupColumns) {
                    Cell cell = row.createCell(sheetRowColumnIdx++);
                    cell.setCellStyle(rowCellStyle);
                    cell.setCellValue("");
                }
            }
        }

    }
}

From source file:punchcardrecords.ui.PunchCardRecordsMainFrame.java

License:Open Source License

/**
 * ?excel(2007+)/*from  w w w .  java  2 s.  co  m*/
 * @param excelFile ??Excel
 * @param single ??
 */
private Map<String, double[]> parseExcel42007(File excelFile, boolean single) {
    Map<String, double[]> result = new HashMap<String, double[]>();
    try {
        // ?,?,
        File copyExcelFile = null;
        XSSFWorkbook copyWorkBook = null;
        if (single) {// ??
            addMessage("");
            copyExcelFile = new File(
                    excelFile.getAbsolutePath().substring(0, excelFile.getAbsolutePath().lastIndexOf("\\"))
                            + "/.xlsx");
            FileUtils.copyFile(excelFile, copyExcelFile);
            // 
            copyWorkBook = new XSSFWorkbook(new FileInputStream(copyExcelFile));
        }

        // ?
        XSSFWorkbook workBook = new XSSFWorkbook(new FileInputStream(excelFile));
        XSSFSheet sheet = workBook.getSheetAt(0);
        int rows = sheet.getLastRowNum();
        if (rows >= 6) { // 6,???
            int month = -1; // ?
            int year = -1;// ?
            if (single) {// ??
                // ?3,?
                String dateStr = sheet.getRow(2).getCell(2).getStringCellValue();
                if (StringUtils.isNotBlank(dateStr)) {
                    addMessage("??:" + dateStr);
                    String[] dates = dateStr.split("~");
                    month = Integer.parseInt(dates[0].split("\\/")[1]);// ??
                    year = Integer.parseInt(dates[0].split("\\/")[0]);// ??
                } else {
                    addMessage(
                            "??,??,?");
                }
                // ?,??
                // ,??
                int maxValue = (rows - 6) / 2;
                progressBar.setMaximum(maxValue);
            }
            int days = sheet.getRow(3).getLastCellNum();

            // ?
            SimpleDateFormat punchFormat = new SimpleDateFormat("HH:mm");

            if (single) {// ??
                // ?,,,?
                String[] title = { "", "", "?" };
                if (null != copyWorkBook) {
                    for (int i = 0; i < title.length; i++) {
                        copyWorkBook.getSheetAt(0).getRow(4).createCell(days + i).setCellValue(title[i]);
                        XSSFCellStyle cellStyle = (XSSFCellStyle) copyWorkBook.getSheetAt(0).getRow(4)
                                .getCell(0).getCellStyle().clone();
                        cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
                        cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
                        copyWorkBook.getSheetAt(0).getRow(4).getCell(days + i).setCellStyle(cellStyle);
                        copyWorkBook.getSheetAt(0).autoSizeColumn((short) (days + i));
                    }
                }
            }

            for (int i = 4; i < rows; i = i + 2) { // 
                //,?,?+2
                String userName = sheet.getRow(i).getCell(10).getStringCellValue();// ??
                String userNum = sheet.getRow(i).getCell(2).getStringCellValue();// ?
                if (single) {// ??
                    addMessage("?:" + userName + "<?:" + userNum + ">");
                    // ??
                    addBar(1);
                }

                // ??,i+1
                XSSFRow recordRow = sheet.getRow(i + 1);

                // 
                double punchDays = 0;
                // (?),?
                double punchHours = 0, avgHours = 0;
                // ???
                for (int j = 0; j < days; j++) {// ???

                    if (single) {// ??
                        // ?,
                        // ?,??,??
                        if (month != -1 && year != -1) {
                            // ???
                            if (isWeekEnd(year, month, j + 1)) {
                                // ,
                                if (null != copyWorkBook) {
                                    XSSFCellStyle weekend = (XSSFCellStyle) copyWorkBook.getSheetAt(0)
                                            .getRow(i + 1).getCell(j).getCellStyle().clone();
                                    weekend.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
                                    weekend.setFillForegroundColor(
                                            new XSSFColor(new java.awt.Color(21, 225, 216)));
                                    //weekend.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
                                    copyWorkBook.getSheetAt(0).getRow(i + 1).getCell(j).setCellStyle(weekend);
                                }
                            }
                        }
                    }

                    // ???
                    String record = recordRow.getCell(j).getStringCellValue();// ?
                    if (StringUtils.isNotBlank(record)) {// ??,??
                        String[] records = record.split("\n");
                        // ???,,?
                        if (records.length >= 2) {
                            try {
                                // ?start,?end,?ls,??le
                                Date end = punchFormat.parse(records[records.length - 1]),
                                        start = punchFormat.parse(records[0]);
                                Date ls = punchFormat.parse("11:40"), le = punchFormat.parse("13:00");

                                if (start.after(ls) && end.before(le)) {
                                    // ??,??
                                    if (single) {// ??
                                        // ?,??,??
                                        if (null != copyWorkBook) {
                                            XSSFCellStyle excepitonStyle = (XSSFCellStyle) copyWorkBook
                                                    .getSheetAt(0).getRow(i + 1).getCell(j).getCellStyle()
                                                    .clone();
                                            excepitonStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
                                            if (month != -1 && year != -1) {
                                                // ???
                                                if (isWeekEnd(year, month, j + 1)) {
                                                    // ,
                                                    excepitonStyle.setFillForegroundColor(
                                                            IndexedColors.PINK.getIndex());
                                                } else {
                                                    excepitonStyle.setFillForegroundColor(
                                                            IndexedColors.RED.getIndex());
                                                }
                                            }
                                            copyWorkBook.getSheetAt(0).getRow(i + 1).getCell(j)
                                                    .setCellStyle(excepitonStyle);
                                        }
                                    }
                                } else {//???
                                    punchDays = punchDays + 1;
                                    // ?
                                    long ms = end.getTime() - start.getTime();//????

                                    // ??,???,?
                                    long mins = 75 * 60 * 1000;//?75

                                    // ??,???
                                    if (start.before(ls) && end.before(le)) {
                                        // ????
                                        mins = end.getTime() - ls.getTime();
                                    }

                                    // ??,???
                                    if (start.after(ls) && end.after(le)) {
                                        // ???,?:??-?
                                        if (start.before(le)) {
                                            mins = le.getTime() - start.getTime();
                                        } else if (start.after(ls)) { // ???,?0
                                            mins = 0;
                                        }
                                    }

                                    ms = ms - mins;// ??

                                    punchHours = punchHours + (double) ms / (3600 * 1000); // (?)
                                }
                            } catch (ParseException ex) {
                                Logger.getLogger(PunchCardRecordsMainFrame.class.getName()).log(Level.SEVERE,
                                        null, ex);
                            }
                        } else {// ?,
                            if (single) {// ??
                                // ?,??,??
                                if (null != copyWorkBook) {
                                    XSSFCellStyle excepitonStyle = (XSSFCellStyle) copyWorkBook.getSheetAt(0)
                                            .getRow(i + 1).getCell(j).getCellStyle().clone();
                                    excepitonStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
                                    if (month != -1 && year != -1) {
                                        // ???
                                        if (isWeekEnd(year, month, j + 1)) {
                                            // ,
                                            excepitonStyle
                                                    .setFillForegroundColor(IndexedColors.PINK.getIndex());
                                        } else {
                                            excepitonStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
                                        }
                                    }
                                    copyWorkBook.getSheetAt(0).getRow(i + 1).getCell(j)
                                            .setCellStyle(excepitonStyle);
                                }
                            }
                        }
                    }
                }
                // ?
                if (punchDays > 0) {
                    // ????
                    punchHours = new BigDecimal(punchHours).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
                    avgHours = new BigDecimal(punchHours / punchDays).setScale(1, BigDecimal.ROUND_HALF_UP)
                            .doubleValue();
                }

                double[] values = { punchDays, punchHours, avgHours };
                result.put(userNum + ":" + userName, values);

                if (single) {// ??
                    addMessage(":" + userName + "<?:" + userNum + ">??,:"
                            + "D:" + punchDays + ",H:" + punchHours + ",AH:" + avgHours);
                    if (null != copyWorkBook) {
                        for (int v = 0; v < values.length; v++) {
                            copyWorkBook.getSheetAt(0).getRow(i + 1).createCell(days + v)
                                    .setCellValue(values[v]);
                            XSSFCellStyle cellStyle = (XSSFCellStyle) copyWorkBook.getSheetAt(0).getRow(i + 1)
                                    .getCell(0).getCellStyle().clone();
                            cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
                            cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
                            copyWorkBook.getSheetAt(0).getRow(i + 1).getCell(days + v).setCellStyle(cellStyle);
                        }
                    }
                }
            }

            if (single) {// ??
                // ??
                // ,?
                addMessage("?,??");
                if (null != copyWorkBook) {
                    FileOutputStream out = new FileOutputStream(copyExcelFile);
                    copyWorkBook.write(out);
                    out.close();
                }

                // ???,??
                JFileChooser fileSaveChooser = new JFileChooser();

                fileSaveChooser.setDialogTitle("?");
                fileSaveChooser.setSelectedFile(new File(
                        excelFile.getAbsolutePath().substring(0, excelFile.getAbsolutePath().lastIndexOf("."))
                                + "-.xlsx"));
                String[] saveType = { "xlsx" };
                fileSaveChooser.setAcceptAllFileFilterUsed(false);
                fileSaveChooser.setFileFilter(new FileNameExtensionFilter("*.xlsx", saveType));
                int saveResult = fileSaveChooser.showSaveDialog(this);
                if (saveResult == JFileChooser.APPROVE_OPTION) {
                    File saveFile = fileSaveChooser.getSelectedFile();

                    // ???
                    String saveFilePath = saveFile.getAbsolutePath();
                    addMessage("?,??->" + saveFilePath);
                    FileUtils.copyFile(copyExcelFile, saveFile);

                    Object[] options = { "", "",
                            ",?" };
                    int response = JOptionPane.showOptionDialog(this,
                            "??,???", "?",
                            JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
                    if (0 == response) {// 
                        // ??
                        addMessage(",??");
                        Desktop.getDesktop().open(saveFile);
                    } else if (1 == response) {// 
                        addMessage(",??");
                        String[] cmd = new String[5];
                        cmd[0] = "cmd";
                        cmd[1] = "/c";
                        cmd[2] = "start";
                        cmd[3] = " ";
                        cmd[4] = saveFile.getAbsolutePath().substring(0,
                                saveFile.getAbsolutePath().lastIndexOf("\\"));
                        Runtime.getRuntime().exec(cmd);
                    } else {
                        alert("??,?()");
                    }
                } else {
                    // ??,?
                    clearMessage();
                    fileName.setText("");
                    // ???
                    addMessage("??");
                }

                // ???
                if (null != copyExcelFile) {
                    copyExcelFile.delete();
                }
            }

        } else {
            // excel???,???????
            alert("????!");
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(PunchCardRecordsMainFrame.class.getName()).log(Level.SEVERE, null, ex);
        alert(",??");
    } catch (IOException | OfficeXmlFileException ex) {
        Logger.getLogger(PunchCardRecordsMainFrame.class.getName()).log(Level.SEVERE, null, ex);
        alert(":" + ex.getMessage());
    }
    return result;
}

From source file:rapture.dp.invocable.workflow.ProcessFile.java

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override/* w ww . j  a  v  a 2s  . co m*/
public String invoke(CallingContext ctx) {
    final int BATCH_LOAD_SIZE = 50; // TODO: move to config
    OPCPackage pkg;
    XSSFWorkbook wb;
    List uris = new ArrayList<>();
    // stores all documents for insertion
    List<List<String>> allDocs = new ArrayList<List<String>>();

    String file = Kernel.getDecision().getContextValue(ctx, getWorkerURI(), "filetoupload");
    String blobUri = Kernel.getDecision().getContextValue(ctx, getWorkerURI(), "blobUri");
    String folderName = Kernel.getDecision().getContextValue(ctx, getWorkerURI(), "folderName");

    String repo = "document://data/" + folderName;
    String docUri = repo + "#id";

    try {
        InputStream is = new ByteArrayInputStream(Kernel.getBlob().getBlob(ctx, blobUri).getContent());
        pkg = OPCPackage.open(is);
        wb = new XSSFWorkbook(pkg);
        XSSFSheet sheet = wb.getSheetAt(0);

        log.info("Loading " + sheet.getPhysicalNumberOfRows() + " rows from " + file + ". Batch size is "
                + BATCH_LOAD_SIZE);

        int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
        int remainder = physicalNumberOfRows % BATCH_LOAD_SIZE;
        int div = physicalNumberOfRows / BATCH_LOAD_SIZE;

        // this only needs to be done once as the uris dont change
        for (int g = 1; g <= BATCH_LOAD_SIZE; g++) {
            uris.add(docUri);
        }
        log.info("created uris list " + uris.size());

        int j = 0;
        int count = 0;
        long startLoadTime = System.currentTimeMillis();
        for (int i = 1; i <= div; i++) {
            List docs = new ArrayList<>();
            // Create a list of documents with size of BATCH_LOAD_SIZE
            for (j = count; j < (BATCH_LOAD_SIZE * i); j++) {
                Row row = sheet.getRow(j);
                Map<String, Object> map = ImmutableMap.of("Row", row.getRowNum(), "DataPeriod",
                        row.getCell(0).toString(), "Industry", row.getCell(3).toString(), "Price",
                        row.getCell(7).toString());
                docs.add(JacksonUtil.jsonFromObject(map));
            }
            allDocs.add(docs);
            count = j;
        }
        long endLoadTime = System.currentTimeMillis();

        ExecutorService executorService = Executors.newCachedThreadPool();
        long startWriteTime = System.currentTimeMillis();
        for (List<String> docList : allDocs) {
            executorService.execute(new InsertData(ctx, docList, uris));
        }
        executorService.shutdown();

        try {
            // TODO: hardcoded timeout.ComparableFutures?
            // Helpful:
            // http://stackoverflow.com/questions/1250643/how-to-wait-for-all-threads-to-finish-using-executorservice
            executorService.awaitTermination(60000L, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            log.error(e.getStackTrace().toString(), e);
            return "error";
        }
        long endWriteTime = System.currentTimeMillis();
        log.info("Completed parallel load.");

        // handle the remaining rows
        if (remainder > 0) {
            long remStartTime = System.currentTimeMillis();
            for (int k = (count); k < (count + remainder); k++) {
                Row row = sheet.getRow(k);
                Map<String, Object> map = ImmutableMap.of("Row", row.getRowNum(), "DataPeriod",
                        row.getCell(0).toString(), "Industry", row.getCell(3).toString(), "Price",
                        row.getCell(7).toString());
                Kernel.getDoc().putDoc(ctx, docUri, JacksonUtil.jsonFromObject(map));
            }
            long remEndTime = System.currentTimeMillis();
            log.info("Remainders took " + (remEndTime - remStartTime) + "ms");
        }

        log.info("Populated uri " + repo + ". Took " + (endLoadTime - startLoadTime) + "ms. to load data. Took "
                + (endWriteTime - startWriteTime) + "ms. to write data.");
        pkg.close();

        Map<String, RaptureFolderInfo> listDocsByUriPrefix = Kernel.getDoc().listDocsByUriPrefix(ctx, repo, 1);
        log.info("Count from repo is " + listDocsByUriPrefix.size());

        if (listDocsByUriPrefix.size() == sheet.getPhysicalNumberOfRows()) {
            return "ok";
        } else {
            return "error"; // TODO: add error step
        }
    } catch (InvalidFormatException | IOException | RaptureException e) {
        log.error("ProcessFile error", e);
        return "error";
    }
}