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

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

Introduction

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

Prototype

void setColumnWidth(int columnIndex, int width);

Source Link

Document

Set the width (in units of 1/256th of a character width)

The maximum column width for an individual cell is 255 characters.

Usage

From source file:utilities.XLSCustomReportsManager.java

License:Open Source License

private void createHeader(ArrayList<Column> cols, Sheet sheet, Map<String, CellStyle> styles) {
    // Set column widths
    for (int i = 0; i < cols.size(); i++) {
        sheet.setColumnWidth(i, cols.get(i).getWidth());
    }/* w w  w .  ja va  2 s  .com*/

    // Create survey sheet header row
    Row headerRow = sheet.createRow(0);
    CellStyle headerStyle = styles.get("header");
    for (int i = 0; i < cols.size(); i++) {
        Column col = cols.get(i);

        Cell cell = headerRow.createCell(i);
        cell.setCellStyle(headerStyle);
        cell.setCellValue(col.name);
    }
}

From source file:utilities.XLSReportsManager.java

License:Open Source License

private void createHeader(ArrayList<Column> cols, Sheet sheet, Map<String, CellStyle> styles) {

    // Set column widths
    for (int i = 0; i < cols.size(); i++) {
        sheet.setColumnWidth(i, cols.get(i).getWidth());
    }/*  w w  w.  j  a  v a2s.  c o m*/

    Row headerRow = sheet.createRow(0);
    CellStyle headerStyle = styles.get("header");
    for (int i = 0; i < cols.size(); i++) {
        Column col = cols.get(i);

        Cell cell = headerRow.createCell(i);
        cell.setCellStyle(headerStyle);
        cell.setCellValue(col.humanName);
    }
}

From source file:utilities.XLSTaskManager.java

License:Open Source License

private void createHeader(ArrayList<Column> cols, Sheet sheet, Map<String, CellStyle> styles) {
    // Set column widths
    for (int i = 0; i < cols.size(); i++) {
        sheet.setColumnWidth(i, cols.get(i).getWidth());
    }//from  w  ww . j  a va2 s  . c o m

    Row headerRow = sheet.createRow(0);
    for (int i = 0; i < cols.size(); i++) {
        Column col = cols.get(i);

        CellStyle headerStyle = null;
        if (col.isAssignment) {
            headerStyle = styles.get("header_assignments");
        } else {
            headerStyle = styles.get("header_tasks");
        }
        Cell cell = headerRow.createCell(i);
        cell.setCellStyle(headerStyle);
        cell.setCellValue(col.human_name);
    }
}

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

public static void main(String[] args) throws Exception {
    Workbook wb;/*www .ja v a 2 s. 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("Timesheet");
    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);
    titleCell.setCellValue("Weekly Timesheet");
    titleCell.setCellStyle(styles.get("title"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$L$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 < 10; i++) {
        Row row = sheet.createRow(rownum++);
        for (int j = 0; j < titles.length; j++) {
            Cell cell = row.createCell(j);
            if (j == 9) {
                //the 10th cell contains sum over week days, e.g. SUM(C3:I3)
                String ref = "C" + rownum + ":I" + rownum;
                cell.setCellFormula("SUM(" + ref + ")");
                cell.setCellStyle(styles.get("formula"));
            } else if (j == 11) {
                cell.setCellFormula("J" + rownum + "-K" + rownum);
                cell.setCellStyle(styles.get("formula"));
            } else {
                cell.setCellStyle(styles.get("cell"));
            }
        }
    }

    //row with totals below
    Row sumRow = sheet.createRow(rownum++);
    sumRow.setHeightInPoints(35);
    Cell cell;
    cell = sumRow.createCell(0);
    cell.setCellStyle(styles.get("formula"));
    cell = sumRow.createCell(1);
    cell.setCellValue("Total Hrs:");
    cell.setCellStyle(styles.get("formula"));

    for (int j = 2; j < 12; j++) {
        cell = sumRow.createCell(j);
        String ref = (char) ('A' + j) + "3:" + (char) ('A' + j) + "12";
        cell.setCellFormula("SUM(" + ref + ")");
        if (j >= 9)
            cell.setCellStyle(styles.get("formula_2"));
        else
            cell.setCellStyle(styles.get("formula"));
    }
    rownum++;
    sumRow = sheet.createRow(rownum++);
    sumRow.setHeightInPoints(25);
    cell = sumRow.createCell(0);
    cell.setCellValue("Total Regular Hours");
    cell.setCellStyle(styles.get("formula"));
    cell = sumRow.createCell(1);
    cell.setCellFormula("L13");
    cell.setCellStyle(styles.get("formula_2"));
    sumRow = sheet.createRow(rownum++);
    sumRow.setHeightInPoints(25);
    cell = sumRow.createCell(0);
    cell.setCellValue("Total Overtime Hours");
    cell.setCellStyle(styles.get("formula"));
    cell = sumRow.createCell(1);
    cell.setCellFormula("K13");
    cell.setCellStyle(styles.get("formula_2"));

    //set sample data
    for (int i = 0; i < sample_data.length; i++) {
        Row row = sheet.getRow(2 + i);
        for (int j = 0; j < sample_data[i].length; j++) {
            if (sample_data[i][j] == null)
                continue;

            if (sample_data[i][j] instanceof String) {
                row.getCell(j).setCellValue((String) sample_data[i][j]);
            } else {
                row.getCell(j).setCellValue((Double) sample_data[i][j]);
            }
        }
    }

    //finally set column widths, the width is measured in units of 1/256th of a character width
    sheet.setColumnWidth(0, 30 * 256); //30 characters wide
    for (int i = 2; i < 9; i++) {
        sheet.setColumnWidth(i, 6 * 256); //6 characters wide
    }
    sheet.setColumnWidth(10, 10 * 256); //10 characters wide

    // Write the output to a file
    String file = "timesheet.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();
    }
}

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

public void crearExcel(List<Empleado> empleados, Date fechaInicio, Date fechaFin)
        throws FileNotFoundException, IOException {
    Workbook wb;//  ww w  .  j a  va2  s  .  c om
    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();
    }
}