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

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

Introduction

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

Prototype

int addMergedRegion(CellRangeAddress region);

Source Link

Document

Adds a merged region of cells (hence those cells form one)

Usage

From source file:vistas.reportes.procesos.rptVacacionesExcel.java

public void crearExcel(List<Empleado> empleados, Date fechaInicio, Date fechaFin)
        throws FileNotFoundException, IOException {
    Workbook wb;//  w w w. ja  v  a  2  s  .  c  o m
    wb = new XSSFWorkbook();
    Map<String, CellStyle> styles = createStyles(wb);

    Sheet sheet = wb.createSheet("Vacaciones");
    PrintSetup printSetup = sheet.getPrintSetup();
    printSetup.setLandscape(true);
    sheet.setFitToPage(true);
    sheet.setHorizontallyCenter(true);

    //title row
    Row titleRow = sheet.createRow(0);
    titleRow.setHeightInPoints(45);
    Cell titleCell = titleRow.createCell(0);
    SimpleDateFormat formateador = new SimpleDateFormat("dd/MM/yyyy");
    titleCell.setCellValue(
            "VACACIONES MINEDU " + formateador.format(fechaInicio) + " - " + formateador.format(fechaFin));
    titleCell.setCellStyle(styles.get("title"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$D$1"));

    //header row
    Row headerRow = sheet.createRow(1);
    headerRow.setHeightInPoints(40);
    Cell headerCell;
    for (int i = 0; i < titles.length; i++) {
        headerCell = headerRow.createCell(i);
        headerCell.setCellValue(titles[i]);
        headerCell.setCellStyle(styles.get("header"));
    }

    int rownum = 2;
    for (int i = 0; i < empleados.size(); i++) {
        Row row = sheet.createRow(rownum++);
        for (int j = 0; j < titles.length; j++) {
            Cell cell = row.createCell(j);
            cell.setCellStyle(styles.get("cell"));

        }
    }
    for (int i = 0; i < empleados.size(); i++) {
        Row row = sheet.getRow(2 + i);
        row.getCell(0).setCellValue(empleados.get(i).getNroDocumento());
        row.getCell(1).setCellValue(empleados.get(i).getNombre() + " " + empleados.get(i).getApellidoPaterno());
        row.getCell(2).setCellValue("Departamento");
        Vacacion vacaciones = vc.buscarXDia(empleados.get(i).getNroDocumento(), fechaInicio);
        if (vacaciones != null) {
            row.getCell(3).setCellValue(formateador.format(vacaciones.getFechaInicio()) + " al "
                    + formateador.format(vacaciones.getFechaFin()));
        } else {
            row.getCell(3).setCellValue("No tiene vacaciones");
        }

    }

    sheet.setColumnWidth(0, 10 * 256);
    sheet.setColumnWidth(1, 30 * 256);
    sheet.setColumnWidth(2, 30 * 256);
    sheet.setColumnWidth(3, 25 * 256);
    //Write the output to a file
    String file = "rptVacaciones.xls";
    if (wb instanceof XSSFWorkbook)
        file += "x";
    FileOutputStream out = new FileOutputStream(file);
    wb.write(out);
    out.close();
    try {
        File path = new File(file);
        Desktop.getDesktop().open(path);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}