Example usage for org.apache.poi.ss.usermodel Sheet createRow

List of usage examples for org.apache.poi.ss.usermodel Sheet createRow

Introduction

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

Prototype

Row createRow(int rownum);

Source Link

Document

Create a new row within the sheet and return the high level representation

Usage

From source file:com.runwaysdk.dataaccess.io.ExcelExportSheet.java

License:Open Source License

/**
 * Prepares a new sheet (which represents a type) in the workbook. Fills in all necessary information for the sheet.
 * /*from w  w w.  j a  va 2 s  .c  o  m*/
 * @return
 */
public Sheet createSheet(Workbook workbook, CellStyle boldStyle) {
    CreationHelper helper = workbook.getCreationHelper();
    String sheetName = this.getFormattedSheetName();

    Sheet sheet = workbook.createSheet(sheetName);
    Drawing drawing = sheet.createDrawingPatriarch();

    Row typeRow = sheet.createRow(0);
    typeRow.setZeroHeight(true);

    Row nameRow = sheet.createRow(1);
    nameRow.setZeroHeight(true);

    Row labelRow = sheet.createRow(2);

    int i = 0;
    for (ExcelColumn column : this.getExpectedColumns()) {
        writeHeader(sheet, drawing, nameRow, labelRow, i++, column, boldStyle);
    }

    for (ExcelColumn column : this.getExtraColumns()) {
        writeHeader(sheet, drawing, nameRow, labelRow, i++, column, boldStyle);
    }

    typeRow.createCell(0).setCellValue(helper.createRichTextString(this.getType()));

    this.writeRows(sheet);

    return sheet;
}

From source file:com.runwaysdk.dataaccess.io.ExcelExportSheet.java

License:Open Source License

private void writeRows(Sheet sheet) {
    List<ExcelColumn> expectedColumns = this.getExpectedColumns();
    List<ExcelColumn> extraColumns = this.getExtraColumns();

    for (int i = 0; i < components.size(); i++) {
        ComponentIF component = components.get(i);
        Row row = sheet.createRow(HEADER_ROW_COUNT + i);

        int size = expectedColumns.size();

        for (int j = 0; j < size; j++) {
            ExcelColumn column = expectedColumns.get(j);
            String value = column.getValue(component);

            Cell cell = row.createCell(j);
            column.setValue(cell, value);
        }/* www  . j a va2s  .c  om*/

        for (int k = 0; k < extraColumns.size(); k++) {
            ExcelColumn column = extraColumns.get(k);
            String value = column.getValue(component);

            Cell cell = row.createCell(size + k);
            column.setValue(cell, value);
        }
    }
}

From source file:com.runwaysdk.dataaccess.io.ExcelImporter.java

License:Open Source License

/**
 * Writes out errors to the correct sheet. Inclusion of the "Column" column is based on the passed parameter. "Row" and "Message" columns are always included.
 * // w w  w.  j a v a  2  s .  co m
 * @param includeColumn
 */
private void writeMessages(boolean includeColumn) {
    int col = 0;

    CreationHelper helper = errorWorkbook.getCreationHelper();
    Sheet sheet = errorWorkbook.getSheet(ERROR_SHEET);
    Row row = sheet.createRow(0);

    row.createCell(col++).setCellValue(helper.createRichTextString("Row"));
    row.createCell(col++).setCellValue(helper.createRichTextString("Sheet"));

    if (includeColumn) {
        row.createCell(col++).setCellValue(helper.createRichTextString("Column"));
    }

    row.createCell(col++).setCellValue(helper.createRichTextString("Error Message"));

    int i = 1;

    for (ImportContext c : contexts) {
        for (ExcelMessage message : c.getErrorMessages()) {
            col = 0;
            row = sheet.createRow(i++);
            row.createCell(col++).setCellValue(message.getRow());
            row.createCell(col++).setCellValue(helper.createRichTextString(c.getSheetName()));
            if (includeColumn) {
                row.createCell(col++).setCellValue(helper.createRichTextString(message.getColumn()));
            }
            row.createCell(col++).setCellValue(helper.createRichTextString(message.getMessage()));
        }
    }

    short c = 0;
    sheet.autoSizeColumn(c++);
    if (includeColumn) {
        sheet.autoSizeColumn(c++);
    }
    sheet.autoSizeColumn(c++);
}

From source file:com.runwaysdk.query.ValueQueryExcelExporter.java

License:Open Source License

/**
 * Prepares a new sheet (which represents a type) in the workbook. Fills in
 * all necessary information for the sheet.
 * //from  ww  w .  ja va 2  s  .  co m
 * @return
 */
private Sheet prepareSheet() {
    OIterator<ValueObject> iterator = this.valueQuery.getIterator();

    Sheet sheet = workbook.createSheet(this.sheetName);
    Row labelRow = sheet.createRow(0);

    List<Selectable> selectableList = this.valueQuery.getSelectableRefs();

    int selectableCount = 0;
    for (Selectable selectable : selectableList) {
        if (this.includeAliases == null || this.includeAliases.size() == 0
                || this.includeAliases.contains(selectable.getUserDefinedAlias())) {
            MdAttributeConcreteDAOIF mdAttribute = selectable.getMdAttributeIF();
            labelRow.createCell(selectableCount).setCellValue(this.workbook.getCreationHelper()
                    .createRichTextString(mdAttribute.getDisplayLabel(Session.getCurrentLocale())));
            selectableCount++;
        }
    }

    int rowCount = 1;
    for (ValueObject valueObject : iterator) {
        Row valueRow = sheet.createRow(rowCount++);

        Map<String, Attribute> attributeMap = valueObject.getAttributeMap();

        selectableCount = 0;

        for (Selectable selectable : selectableList) {
            if (this.includeAliases == null || this.includeAliases.size() == 0
                    || this.includeAliases.contains(selectable.getUserDefinedAlias())) {
                String attributeName = selectable.getResultAttributeName();

                Attribute attribute = attributeMap.get(attributeName);

                String value = attribute.getValue();

                if (attribute instanceof com.runwaysdk.dataaccess.attributes.value.AttributeBoolean) {
                    com.runwaysdk.dataaccess.attributes.value.AttributeBoolean attributeBoolean = (com.runwaysdk.dataaccess.attributes.value.AttributeBoolean) attribute;

                    // exports as 1 and 0 as per #2735
                    String displayLabel;
                    if (value == null || value.trim().length() == 0) {
                        displayLabel = "";
                    } else if (attributeBoolean.getBooleanValue()) {
                        displayLabel = MdAttributeBooleanDAOIF.DB_TRUE;
                    } else {
                        displayLabel = MdAttributeBooleanDAOIF.DB_FALSE;
                    }

                    valueRow.createCell(selectableCount)
                            .setCellValue(this.workbook.getCreationHelper().createRichTextString(displayLabel));
                } else if (attribute instanceof com.runwaysdk.dataaccess.attributes.value.AttributeNumber) {
                    com.runwaysdk.dataaccess.attributes.value.AttributeNumber attributeNumber = (com.runwaysdk.dataaccess.attributes.value.AttributeNumber) attribute;

                    String numberValue = attributeNumber.getValue();

                    if (numberValue != null && !numberValue.equals("")) {
                        // Precondition - assumes value is a valid couble.
                        valueRow.createCell(selectableCount).setCellValue(Double.valueOf(numberValue));
                    }
                } else if (attribute instanceof com.runwaysdk.dataaccess.attributes.value.AttributeDate) {
                    com.runwaysdk.dataaccess.attributes.value.AttributeDate attributeDate = (com.runwaysdk.dataaccess.attributes.value.AttributeDate) attribute;

                    String dateValue = attributeDate.getValue();

                    if (dateValue != null && !dateValue.equals("")) {
                        SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.DATE_FORMAT);

                        Date date = dateFormat.parse(dateValue, new java.text.ParsePosition(0));

                        // Precondition - assumes value is a valid couble.
                        Cell cell = valueRow.createCell(selectableCount);
                        cell.setCellValue(date);
                        cell.setCellStyle(dateStyle);
                    }
                } else if (attribute instanceof com.runwaysdk.dataaccess.attributes.value.AttributeDateTime) {
                    com.runwaysdk.dataaccess.attributes.value.AttributeDateTime attributeDate = (com.runwaysdk.dataaccess.attributes.value.AttributeDateTime) attribute;

                    String dateValue = attributeDate.getValue();

                    if (dateValue != null && !dateValue.equals("")) {
                        SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.DATETIME_FORMAT);

                        Date date = dateFormat.parse(dateValue, new java.text.ParsePosition(0));

                        // Precondition - assumes value is a valid couble.
                        valueRow.createCell(selectableCount).setCellValue(date);
                    }
                } else if (attribute instanceof com.runwaysdk.dataaccess.attributes.value.AttributeTime) {
                    com.runwaysdk.dataaccess.attributes.value.AttributeTime attributeDate = (com.runwaysdk.dataaccess.attributes.value.AttributeTime) attribute;

                    String dateValue = attributeDate.getValue();

                    if (dateValue != null && !dateValue.equals("")) {
                        SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.TIME_FORMAT);

                        Date date = dateFormat.parse(dateValue, new java.text.ParsePosition(0));

                        // Precondition - assumes value is a valid couble.
                        valueRow.createCell(selectableCount).setCellValue(date);
                    }
                } else if (attribute instanceof com.runwaysdk.dataaccess.attributes.value.AttributeChar
                        || attribute instanceof com.runwaysdk.dataaccess.attributes.value.AttributeReference) {
                    valueRow.createCell(selectableCount)
                            .setCellValue(this.workbook.getCreationHelper().createRichTextString(value));
                }

                selectableCount++;
            }
        }
    }

    return sheet;
}

From source file:com.runwaysdk.query.ViewArrayExcelExporter.java

License:Open Source License

/**
 * Prepares a new sheet (which represents a type) in the workbook. Fills in
 * all necessary information for the sheet.
 *
 * @return//from   www  .  j av a2 s  . c o  m
 */
protected Sheet prepareSheet() {
    Sheet sheet = super.createSheet();
    CreationHelper helper = sheet.getWorkbook().getCreationHelper();

    // Row typeRow = sheet.createRow(0);
    // typeRow.createCell(0).setCellValue(new HSSFRichTextString(type));
    Row labelRow = sheet.createRow(0);

    for (int col = 0; col < attributes.size(); col++) {
        String attributeName = attributes.get(col);
        MdAttributeDAOIF mdAttribute = this.getMdAttribute(mdView, attributeName);

        if (mdAttribute != null) {
            labelRow.createCell(col).setCellValue(
                    helper.createRichTextString(mdAttribute.getDisplayLabel(Session.getCurrentLocale())));
        }
    }

    for (int row = 0; row < array.length; row++) {
        View view = array[row];

        Row valueRow = sheet.createRow(row + 1);

        for (int col = 0; col < attributes.size(); col++) {
            String attributeName = attributes.get(col);
            MdAttributeDAOIF mdAttribute = this.getMdAttribute(mdView, attributeName).getMdAttributeConcrete();

            if (mdAttribute != null) {
                String value = view.getValue(attributeName);

                if (mdAttribute instanceof MdAttributeBooleanDAOIF) {
                    MdAttributeBooleanDAOIF mdAttributeBooleanDAOIF = (MdAttributeBooleanDAOIF) mdAttribute;

                    populateBooleanCell(valueRow, col, value, mdAttributeBooleanDAOIF);
                } else if (mdAttribute instanceof MdAttributeNumberDAOIF) {
                    populateNumberCell(valueRow, col, value);
                } else if (mdAttribute instanceof MdAttributeDateDAOIF) {
                    populateDateCell(valueRow, col, value);
                } else if (mdAttribute instanceof MdAttributeDateTimeDAOIF) {
                    populateDateTimeCell(valueRow, col, value);
                } else if (mdAttribute instanceof MdAttributeTimeDAOIF) {
                    populateTimeCell(valueRow, col, value);
                } else if (mdAttribute instanceof MdAttributeCharacterDAOIF
                        || mdAttribute instanceof MdAttributeReferenceDAOIF) {
                    populateCharacterCell(valueRow, col, value);
                }
            }
        }
    }

    return sheet;
}

From source file:com.sany.appbom.service.AppBomServiceImpl.java

License:Open Source License

public void setExcelData(Workbook workbook, boolean is2003, List<AppBom> beans) {
    try {/*from  ww  w . ja  v a  2s . c  o  m*/
        Sheet sheet = null;
        CellStyle cellStyle = null;
        Font font = null;
        if (is2003) {
            sheet = (HSSFSheet) workbook.getSheetAt(0);

        } else {
            sheet = (XSSFSheet) workbook.getSheetAt(0);
            cellStyle = (XSSFCellStyle) workbook.createCellStyle();
        }
        for (int i = 0; i < beans.size(); i++) {
            AppBom bom = beans.get(i);
            Row row = null;
            row = sheet.createRow(i + 2);
            row.setHeight((short) 450);
            row.createCell(0).setCellValue(bom.getBm());
            row.createCell(1).setCellValue(bom.getApp_name_en());
            row.createCell(2).setCellValue(bom.getApply_domain());
            row.createCell(3).setCellValue(bom.getApp_name());
            row.createCell(4).setCellValue(bom.getDescription());
            row.createCell(5).setCellValue(getSoftLevel(bom.getSoft_level()));
            row.createCell(6).setCellValue(bom.getSupplier());
            row.createCell(7).setCellValue(bom.getStart_year());
            row.createCell(8).setCellValue(getState(bom.getState()));
            row.createCell(9).setCellValue(getRdType(bom.getRd_type()));
            row.createCell(10).setCellValue(bom.getVersion_no());
            row.createCell(11).setCellValue(bom.getDomain_url());
            row.createCell(12).setCellValue(bom.getStruct_mode());
            row.createCell(13).setCellValue(bom.getSoft_language());
            row.createCell(14).setCellValue(bom.getDevelop_tool());
            row.createCell(15).setCellValue(bom.getDb_type());
            row.createCell(16).setCellValue(bom.getDepartment_develop());
            row.createCell(17).setCellValue(bom.getProduct_manager());
            row.createCell(18).setCellValue(bom.getDepartment_maintain());
            row.createCell(19).setCellValue(bom.getSys_manager());
            row.createCell(20).setCellValue(getPlanType(bom.getPlan_type()));
            row.createCell(21).setCellValue(bom.getEvolve_strategy());
            row.createCell(22).setCellValue(bom.getEvolve_plan());
            row.createCell(23).setCellValue(bom.getEvolve_depart());
            row.createCell(24).setCellValue(getManageScope(bom.getManage_scope()));
            row.createCell(25).setCellValue(bom.getMain_description());
        }
    } catch (Exception e) {
        e.printStackTrace();

    } finally {

    }
}

From source file:com.seer.datacruncher.profiler.spring.ExporterController.java

License:Open Source License

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String type = CommonUtil.notNullValue(request.getParameter("exportaction"));
    String columns = CommonUtil.notNullValue(request.getParameter("exportcolumns"));
    String data = CommonUtil.notNullValue(request.getParameter("exportdata"));

    if (type.equals("csv")) {
        PrintWriter out = response.getWriter();
        response.setContentType("application/csv");
        response.setHeader("content-disposition", "attachment;filename=analysis_data.csv"); // set the file
        // name to
        // whatever
        // required..
        out.println(columns.replace("&&&&&", ","));
        for (String strData : data.split("@@@@@")) {
            out.println(strData.replace("&&&&&", ","));
        }//w ww .  j a  va  2  s  .  c o  m
        out.flush();
        out.close();
    } else if (type.equals("xml")) {
        PrintWriter out = response.getWriter();
        response.setContentType("text/xml");
        response.setHeader("content-disposition", "attachment;filename=analysis_data.xml"); // set the file
        // name to
        // whatever
        // required..
        try {
            StringBuffer xml = new StringBuffer("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            xml.append("<table><header>");
            String colArr[] = columns.split("&&&&&");
            for (String col : colArr) {
                xml.append("<columnName>" + col + "</columnName>");
            }
            xml.append("</header>");

            for (String strData : data.split("@@@@@")) {
                xml.append("<row>");
                int ind = 0;
                for (String val : strData.split("&&&&&")) {
                    xml.append("<" + colArr[ind] + ">" + val + "</" + colArr[ind] + "/>");
                    ind++;
                }
                xml.append("</row>");
            }
            xml.append("</table>");
            out.print(xml.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        out.flush();
        out.close();
    } else if (type.equals("excel")) {
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=analysis_data.xls");
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("new sheet");
        String colArr[] = columns.split("&&&&&");
        short ind = 0;
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        style.setFont(font);
        Row row = sheet.createRow(0);
        for (String col : colArr) {
            Cell cell = row.createCell(ind);
            cell.setCellValue(col);
            cell.setCellStyle(style);
            ind++;
        }
        ind = 1;
        for (String strData : data.split("@@@@@")) {
            Row valRow = sheet.createRow(ind);
            short cellInd = 0;
            for (String val : strData.split("&&&&&")) {
                valRow.createCell(cellInd).setCellValue(val);
                cellInd++;
            }
            ind++;
        }

        // Write the output to a file
        OutputStream resOout = response.getOutputStream();
        wb.write(resOout);
        resOout.close();

    }

    return null;
}

From source file:com.setu.hsapiassistance.service.ReportService.java

Workbook createReport(List<History> histories) {
    Workbook wb = new HSSFWorkbook();
    //Workbook wb = new XSSFWorkbook();
    CreationHelper createHelper = wb.getCreationHelper();
    Sheet sheet = wb.createSheet("new sheet");

    // Create a row and put some cells in it. Rows are 0 based.
    Row row = sheet.createRow((short) 0);
    // Create a cell and put a value in it.
    Cell contactHeader = row.createCell(0);
    contactHeader.setCellValue("Contact");
    Cell dateHeader = row.createCell(1);
    dateHeader.setCellValue("Date");
    Cell actionHeader = row.createCell(2);
    actionHeader.setCellValue("Action");

    for (int i = 0; i < histories.size(); i++) {
        row = sheet.createRow(i + 1);/* w ww. ja v  a  2 s .co m*/
        Cell contact = row.createCell(0);
        contact.setCellValue(histories.get(i).getEmail());
        Cell date = row.createCell(1);
        date.setCellValue(getFormattedDate(histories.get(i).getDate()));
        Cell action = row.createCell(2);
        action.setCellValue(histories.get(i).getAction());
    }

    return wb;
}

From source file:com.shiyq.poi.HSSFTest.java

private static void setSheet(Sheet sheet, int startRow, int startColumn, List<Map<String, Object>> list,
        String[] head, CellStyle headStyle, String[] code) {
    Row row = sheet.createRow(startRow);
    setHeadRow(row, head, startColumn, headStyle);
    for (int i = 0; i < list.size(); i++) {
        Map<String, Object> map = list.get(i);
        row = sheet.createRow(i + startRow + 1);
        setRow(row, startColumn, map, code);
    }//ww w.  j a  v a 2s  . co  m

}

From source file:com.shiyq.poi.HSSFTest.java

private static void setSheet(Sheet sheet, int startRow, int endRow, int startColumn, int endColumn,
        String describe, CellStyle descStyle) {
    Row row = sheet.getRow(startRow);//  www.j a v  a  2s . co m
    if (Objects.isNull(row)) {
        row = sheet.createRow(startRow);
    }
    Cell cell = row.createCell(startColumn);
    cell.setCellStyle(descStyle);
    cell.setCellValue(describe);
    sheet.addMergedRegion(new CellRangeAddress(startRow, endRow, startColumn, endColumn));
}