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.jslsolucoes.tagria.lib.grid.exporter.impl.ExcelExporter.java

License:Apache License

private void header(Sheet sheet, Workbook workbook) {
    Row sheetRow = sheet.createRow(0);
    int cell = 0;
    for (Header header : table.getHeaders()) {
        Cell sheetCel = sheetRow.createCell(cell);
        sheetCel.setCellValue(header.getContent());

        CellStyle cellStyle = workbook.createCellStyle();
        if ("center".equals(header.getAlign()))
            cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        else if ("left".equals(header.getAlign()))
            cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
        else if ("right".equals(header.getAlign()))
            cellStyle.setAlignment(CellStyle.ALIGN_RIGHT);
        sheetCel.setCellStyle(cellStyle);

        cell++;//from  w w  w .j a va2 s .c o m
    }

}

From source file:com.jslsolucoes.tagria.lib.grid.exporter.impl.ExcelExporter.java

License:Apache License

private void body(Sheet sheet, Workbook workbook) {
    int line = 1;
    for (com.jslsolucoes.tagria.lib.grid.exporter.model.Row row : table.getRows()) {
        Row sheetRow = sheet.createRow(line);
        int cell = 0;
        for (Column column : row.getColumns()) {
            Cell sheetCel = sheetRow.createCell(cell);
            sheetCel.setCellValue(column.getContent());

            CellStyle cellStyle = workbook.createCellStyle();
            if ("center".equals(column.getAlign()))
                cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
            else if ("left".equals(column.getAlign()))
                cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
            else if ("right".equals(column.getAlign()))
                cellStyle.setAlignment(CellStyle.ALIGN_RIGHT);
            sheetCel.setCellStyle(cellStyle);
            cell++;/*from ww  w .j  a v a2s . c o  m*/
        }
        line++;
    }

}

From source file:com.jwm123.loggly.reporter.ReportGenerator.java

License:Apache License

public byte[] build(List<Map<String, String>> row) throws IOException {
    List<String> colLabels = new ArrayList<String>();
    Sheet reportSheet = workbook.getSheet("Report");
    if (reportSheet == null) {
        reportSheet = workbook.createSheet("Report");
    }//w  ww  . j  a v a2s  .c o m
    Row firstRow = reportSheet.getRow(0);
    if (firstRow == null) {
        firstRow = reportSheet.createRow(0);
        int cellNum = 0;
        for (Map<String, String> col : row) {
            for (String key : col.keySet()) {
                Cell cell = firstRow.createCell(cellNum++);
                setCellValue(cell, key);
            }
        }
    }
    for (int i = 0; i < firstRow.getLastCellNum(); i++) {
        Cell cell = firstRow.getCell(i);
        if (cell != null) {
            colLabels.add(cell.getStringCellValue());
        }
    }
    Row newRow = reportSheet.createRow(reportSheet.getLastRowNum() + 1);
    int lastIndex = -1;
    for (Map<String, String> col : row) {
        for (String key : col.keySet()) {
            int colNum = -1;
            Cell cell = null;
            if (colLabels.contains(key)) {
                colNum = colLabels.indexOf(key);
                lastIndex = colNum;
            }
            if (colNum == -1) {
                lastIndex++;
                colNum = lastIndex;
                shiftColumns(reportSheet, colNum, key);
                colLabels.add(colNum, key);
            }
            cell = newRow.getCell(colNum);
            if (cell == null) {
                cell = newRow.createCell(colNum);
            }
            setCellValue(cell, col.get(key));
        }
    }
    for (int i = 0; i < firstRow.getLastCellNum(); i++) {
        reportSheet.autoSizeColumn(i);
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    workbook.write(baos);
    return baos.toByteArray();
}

From source file:com.kafeidev.test.BusinessPlan.java

License:Apache License

@Test
public static void main(String[] args) throws Exception {
    Workbook wb;/*  w w  w.ja v  a  2s .  co  m*/

    if (args.length > 0 && args[0].equals("-xls"))
        wb = new HSSFWorkbook();
    else
        wb = new XSSFWorkbook();

    Map<String, CellStyle> styles = createStyles(wb);

    Sheet sheet = wb.createSheet("Business Plan");

    //turn off gridlines
    sheet.setDisplayGridlines(false);
    sheet.setPrintGridlines(false);
    sheet.setFitToPage(true);
    sheet.setHorizontallyCenter(true);
    PrintSetup printSetup = sheet.getPrintSetup();
    printSetup.setLandscape(true);

    //the following three statements are required only for HSSF
    sheet.setAutobreaks(true);
    printSetup.setFitHeight((short) 1);
    printSetup.setFitWidth((short) 1);

    //the header row: centered text in 48pt font
    Row headerRow = sheet.createRow(0);
    headerRow.setHeightInPoints(12.75f);
    for (int i = 0; i < titles.length; i++) {
        Cell cell = headerRow.createCell(i);
        cell.setCellValue(titles[i]);
        cell.setCellStyle(styles.get("header"));
    }
    //columns for 11 weeks starting from 9-Nov
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    //        {
    //           String inputDate = "2010-Nov-04 01:32:27";
    //           Date date = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss", new Locale("en,EN")).parse(inputDate);
    //            String str= new SimpleDateFormat("dd.MMM.yyyy HH:mm:ss").format(date);
    //            System.out.println("data:"+str);
    //            
    //        }
    calendar.setTime(fmt.parse("19-Nov"));
    calendar.set(Calendar.YEAR, year);
    for (int i = 0; i < 11; i++) {
        Cell cell = headerRow.createCell(titles.length + i);
        cell.setCellValue(calendar);
        cell.setCellStyle(styles.get("header_date"));
        calendar.roll(Calendar.WEEK_OF_YEAR, true);
    }
    //freeze the first row
    sheet.createFreezePane(0, 1);

    Row row;
    Cell cell;
    int rownum = 1;
    for (int i = 0; i < data.length; i++, rownum++) {
        row = sheet.createRow(rownum);
        if (data[i] == null)
            continue;

        for (int j = 0; j < data[i].length; j++) {
            cell = row.createCell(j);
            String styleName;
            boolean isHeader = i == 0 || data[i - 1] == null;
            switch (j) {
            case 0:
                if (isHeader) {
                    styleName = "cell_b";
                    cell.setCellValue(Double.parseDouble(data[i][j]));
                } else {
                    styleName = "cell_normal";
                    cell.setCellValue(data[i][j]);
                }
                break;
            case 1:
                if (isHeader) {
                    styleName = i == 0 ? "cell_h" : "cell_bb";
                } else {
                    styleName = "cell_indented";
                }
                cell.setCellValue(data[i][j]);
                break;
            case 2:
                styleName = isHeader ? "cell_b" : "cell_normal";
                cell.setCellValue(data[i][j]);
                break;
            case 3:
                styleName = isHeader ? "cell_b_centered" : "cell_normal_centered";
                cell.setCellValue(Integer.parseInt(data[i][j]));
                break;
            case 4: {
                calendar.setTime(fmt.parse(data[i][j]));
                calendar.set(Calendar.YEAR, year);
                cell.setCellValue(calendar);
                styleName = isHeader ? "cell_b_date" : "cell_normal_date";
                break;
            }
            case 5: {
                int r = rownum + 1;
                String fmla = "IF(AND(D" + r + ",E" + r + "),E" + r + "+D" + r + ",\"\")";
                cell.setCellFormula(fmla);
                styleName = isHeader ? "cell_bg" : "cell_g";
                break;
            }
            default:
                styleName = data[i][j] != null ? "cell_blue" : "cell_normal";
            }

            cell.setCellStyle(styles.get(styleName));
        }
    }

    //group rows for each phase, row numbers are 0-based
    sheet.groupRow(4, 6);
    sheet.groupRow(9, 13);
    sheet.groupRow(16, 18);

    //set column widths, the width is measured in units of 1/256th of a character width
    sheet.setColumnWidth(0, 256 * 6);
    sheet.setColumnWidth(1, 256 * 33);
    sheet.setColumnWidth(2, 256 * 20);
    sheet.setZoom(3, 4);

    // Write the output to a file
    String file = "businessplan.xls";
    if (wb instanceof XSSFWorkbook)
        file += "x";
    FileOutputStream out = new FileOutputStream(file);
    wb.write(out);
    out.close();
}

From source file:com.kovtun.WorkTimeMap.UI.TimeReport.java

private Workbook generateData() {
    Workbook book = new HSSFWorkbook();
    Sheet sheet = book.createSheet();
    int rowCount = 1;
    //? /*from  w  w w  .j  a v a2s.  co m*/
    Row projRow = sheet.createRow(0);
    Cell prjName = projRow.createCell(0);
    prjName.setCellValue(project.getName());

    for (ReportUser user : users) {
        //?
        Row row = sheet.createRow(rowCount);

        Cell nullCell = row.createCell(0);
        nullCell.setCellValue(" ");

        Row nameRow = sheet.createRow(rowCount);

        Cell nameCell = nameRow.createCell(0);
        nameCell.setCellValue(user.getFio());

        Cell userAllTimeCell = nameRow.createCell(1);
        userAllTimeCell.setCellValue((user.getAllTime() / (1000 * 60)) / 60 + " . "
                + (user.getAllTime() / (1000 * 60) % 60 + " ."));

        //
        rowCount++;
        Row aRow = sheet.createRow(rowCount);

        Cell aNumber = aRow.createCell(0);
        aNumber.setCellValue("");

        Cell aName = aRow.createCell(1);
        aName.setCellValue("?");

        Cell aDescription = aRow.createCell(2);
        aDescription.setCellValue("?");

        Cell aComment = aRow.createCell(3);
        aComment.setCellValue("");

        Cell aDate = aRow.createCell(4);
        aDate.setCellValue(" ");

        Cell aTime = aRow.createCell(5);
        aTime.setCellValue("? ?");

        //   
        List<Action> list = user.getActions();
        int k = 1;
        for (Action action : list) {
            rowCount++;
            Row actionRow = sheet.createRow(rowCount);

            Cell actionNumber = actionRow.createCell(0);
            actionNumber.setCellValue(k);
            k++;

            Cell actionName = actionRow.createCell(1);
            actionName.setCellValue(action.getName());

            Cell actionDescription = actionRow.createCell(2);
            actionDescription.setCellValue(action.getDescription());

            Cell actionComment = actionRow.createCell(3);
            actionComment.setCellValue(action.getComment());

            Cell actionDate = actionRow.createCell(4);
            actionDate.setCellValue(action.getStringDate());

            Cell actionTime = actionRow.createCell(5);
            actionTime.setCellValue((action.getStopTime() == null ? "? !"
                    : ((action.getStopTime().getTime() - action.getStartTime().getTime()) / (1000 * 60)) / 60
                            + " . " + ((action.getStopTime().getTime() - action.getStartTime().getTime())
                                    / (1000 * 60) % 60 + " .")));

        }
        rowCount++;

    }

    sheet.autoSizeColumn(0);
    sheet.autoSizeColumn(1);
    sheet.autoSizeColumn(2);
    sheet.autoSizeColumn(3);
    sheet.autoSizeColumn(4);
    sheet.autoSizeColumn(5);

    return book;
}

From source file:com.l3.info.magenda.emplois_du_temps.Jour.java

void writeInSheet(Workbook workbook, Sheet sheet, int x_sem, int y_sem) {
    Row[] row = new Row[1 + this.getNbrLigne()];
    row[0] = sheet.createRow(y_sem);
    for (int i = 1; i < 1 + this.getNbrLigne(); i++) {
        row[i] = sheet.createRow(y_sem + i);
        row[i].setHeight(Workbook.PixelsToTwips(64));
    }/*w  w w. ja va2s.  co  m*/

    int x = x_sem, y = 0;
    Cell cell = row[y].createCell(x);
    cell.setCellValue(this.nom_jour);
    cell.setCellStyle(workbook.getCellStyle("nom_du_jour"));
    x++;
    for (int i = START_HOUR_OF_DAY; i <= END_HOUR_OF_DAY; i++) {
        cell = row[y].createCell(x);
        cell.setCellValue(i + "h");
        cell.setCellStyle(workbook.getCellStyle("case_gauche_jour"));
        cell = row[y].createCell(x + 1);
        cell.setCellStyle(workbook.getCellStyle("case_droite_jour"));
        x += 2;
    }

    for (int i = 1; i <= this.getNbrLigne(); i++) {
        x = x_sem + 1;
        for (int j = (END_HOUR_OF_DAY - START_HOUR_OF_DAY + 1); j > 0; j--) {
            cell = row[i].createCell(x);
            cell.setCellStyle(workbook.getCellStyle("case_gauche_jour"));
            cell = row[i].createCell(x + 1);
            cell.setCellStyle(workbook.getCellStyle("case_droite_jour"));
            x += 2;
        }
    }

    x = x_sem;
    y = y_sem;

    for (int i = 1; i <= this.getNbrLigne(); i++) {
        cell = row[i].createCell(x);
        cell.setCellStyle(workbook.getCellStyle("nom_du_jour"));
    }
    sheet.addMergedRegion(new CellRangeAddress(y + 1, y + getNbrLigne(), x, x));

    x = x_sem + 1;
    y = y_sem + 1;
    for (Horaire h : horaire_du_jour) {
        cell = row[1 + h.getLigne()].createCell((x + h.getColonne()));
        cell.setCellValue(h.toString());
        cell.setCellStyle(workbook.getCellStyle("Style_" + h.getExamen().getDiplome()));
        for (int i = 1; i < h.getPlage_horraire(); i++) {
            cell = row[1 + h.getLigne()].createCell((x + h.getColonne() + i));
            cell.setCellStyle(workbook.getCellStyle("Style_" + h.getExamen().getDiplome()));
        }

        if (h.getPlage_horraire() > 1) {
            sheet.addMergedRegion(new CellRangeAddress(y + h.getLigne(), (y + h.getLigne()),
                    (x + h.getColonne()), (int) (x + h.getColonne() + h.getPlage_horraire() - 1)));
        }
    }

}

From source file:com.l3.info.magenda.emplois_du_temps.Semaine.java

void writeInSheet(Workbook workbook, int week_of_year) {

    XSSFWorkbook xssfWorkbook = workbook.getWorkbook();
    Sheet sheet = xssfWorkbook.createSheet("Sem. " + week_of_year);
    Row row = sheet.createRow((short) 0);

    row.setHeight(Workbook.PixelsToTwips(64));
    Cell cell = row.createCell((short) 0);

    // first row (0-based) - last row  (0-based) - first column (0-based) -last column  (0-based)
    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, (20 - 7) * 2 + 2));

    // Cree une nouvelle police
    Font font = xssfWorkbook.createFont();
    font.setFontHeightInPoints((short) 18);
    font.setFontName("Arial");

    // Fonts are set into a style so create a new one to use.
    XSSFCellStyle style = xssfWorkbook.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setFont(font);/*from   w  ww. ja  v  a2 s .c om*/

    // Create a cell and put a value in it.
    cell.setCellValue("Semaine " + this.num_semaine);
    cell.setCellStyle(style);

    sheet.setDefaultRowHeight((short) 500);

    int x = 0, y = 2;
    for (Jour day : days_of_week) {
        day.writeInSheet(workbook, sheet, x, y);
        y += 2 + day.getNbrLigne();
    }
}

From source file:com.liferay.dynamic.data.lists.internal.exporter.DDLXLSExporter.java

License:Open Source License

protected void createDataRow(int rowIndex, Sheet sheet, DateTimeFormatter dateTimeFormatter, String author,
        String status, Date statusDate, CellStyle style, Map<String, DDMFormField> ddmFormFields,
        Map<String, DDMFormFieldRenderedValue> values) {

    Row row = sheet.createRow(rowIndex);

    int cellIndex = 0;

    Cell cell = null;// ww  w  . j  av  a2  s.  c  om

    for (Map.Entry<String, DDMFormField> entry : ddmFormFields.entrySet()) {
        cell = row.createCell(cellIndex++, CellType.STRING);

        cell.setCellStyle(style);

        if (values.containsKey(entry.getKey())) {
            DDMFormFieldRenderedValue ddmFormFieldRenderedValue = values.get(entry.getKey());

            cell.setCellValue(GetterUtil.getString(ddmFormFieldRenderedValue.getValue()));
        } else {
            cell.setCellValue(StringPool.BLANK);
        }
    }

    cell = row.createCell(cellIndex++, CellType.STRING);

    cell.setCellStyle(style);
    cell.setCellValue(status);

    cell = row.createCell(cellIndex++, CellType.STRING);

    cell.setCellStyle(style);
    cell.setCellValue(formatDate(statusDate, dateTimeFormatter));

    cell = row.createCell(cellIndex++, CellType.STRING);

    cell.setCellStyle(style);
    cell.setCellValue(author);
}

From source file:com.liferay.dynamic.data.lists.internal.exporter.DDLXLSExporter.java

License:Open Source License

protected void createHeaderRow(Collection<DDMFormField> ddmFormFields, Sheet sheet, Workbook workbook) {

    Row row = sheet.createRow(0);

    CellStyle cellStyle = createCellStyle(workbook, true, "Courier New", (short) 14);

    int cellIndex = 0;

    Cell cell = null;/*from  www  . ja va  2s  . c  o  m*/

    Locale locale = getLocale();

    for (DDMFormField ddmFormField : ddmFormFields) {
        LocalizedValue label = ddmFormField.getLabel();

        cell = row.createCell(cellIndex++, CellType.STRING);

        cell.setCellStyle(cellStyle);
        cell.setCellValue(label.getString(locale));
    }

    cell = row.createCell(cellIndex++, CellType.STRING);

    cell.setCellStyle(cellStyle);
    cell.setCellValue(LanguageUtil.get(locale, "status"));

    cell = row.createCell(cellIndex++, CellType.STRING);

    cell.setCellStyle(cellStyle);
    cell.setCellValue(LanguageUtil.get(locale, "modified-date"));

    cell = row.createCell(cellIndex++, CellType.STRING);

    cell.setCellStyle(cellStyle);
    cell.setCellValue(LanguageUtil.get(locale, "author"));
}

From source file:com.linus.excel.poi.AligningCells.java

License:Apache License

public static void main(String[] args) throws IOException {
    Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();

    Sheet sheet = wb.createSheet();
    Row row = sheet.createRow((short) 2);
    row.setHeightInPoints(30);/*from   ww w . j  av  a  2 s .  c  o m*/
    for (int i = 0; i < 8; i++) {
        //column width is set in units of 1/256th of a character width
        sheet.setColumnWidth(i, 256 * 15);
    }

    createCell(wb, row, (short) 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM);
    createCell(wb, row, (short) 1, CellStyle.ALIGN_CENTER_SELECTION, CellStyle.VERTICAL_BOTTOM);
    createCell(wb, row, (short) 2, CellStyle.ALIGN_FILL, CellStyle.VERTICAL_CENTER);
    createCell(wb, row, (short) 3, CellStyle.ALIGN_GENERAL, CellStyle.VERTICAL_CENTER);
    createCell(wb, row, (short) 4, CellStyle.ALIGN_JUSTIFY, CellStyle.VERTICAL_JUSTIFY);
    createCell(wb, row, (short) 5, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_TOP);
    createCell(wb, row, (short) 6, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_TOP);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("excel/ss-example-align.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}