Example usage for org.apache.poi.ss.util WorkbookUtil createSafeSheetName

List of usage examples for org.apache.poi.ss.util WorkbookUtil createSafeSheetName

Introduction

In this page you can find the example usage for org.apache.poi.ss.util WorkbookUtil createSafeSheetName.

Prototype

public static String createSafeSheetName(final String nameProposal) 

Source Link

Document

Creates a valid sheet name, which is conform to the rules.

Usage

From source file:vista.ui.VistaGenerarEstadisticas.java

License:Open Source License

/**
 * Este mtodo sirve para crear una nueva hoja de Excel en el libro de
 * trabajo seleccionado, no es trabajo de este mtodo guardar los cambios en
 * el sistema de archivos, slo agrega la hoja de Excel al libro de trabajo
 * en memoria./*from  w w  w .  j av a2  s.c o  m*/
 *
 * @param workbook el objeto Workbook al que se le adjuntar la nueva hoja
 * de Excel
 * @param nombre un String con el nombre que tendr la nueva hoja de Excel
 * @param datos un objeto TablaEstadisticas con los datos y los nombres de
 * columnas que sern agregados a la hoja
 */
private void crearHoja(Workbook workbook, String nombre, TablaEstadisticas datos) {
    //Crear la hoja
    CreationHelper createHelper = workbook.getCreationHelper();
    Sheet nuevaHoja = workbook.createSheet(WorkbookUtil.createSafeSheetName(nombre));

    //Escribir los nombres de las columnas en la hoja
    Row headers = nuevaHoja.createRow(0);

    for (int i = 0; i < datos.getColumnCount(); i++) {
        //Crear headers con estilo
        Cell cell = headers.createCell(i);
        CellStyle style = crearEstiloCelda(workbook, IndexedColors.BLACK, CellStyle.BORDER_THICK,
                CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);

        cell.setCellValue(createHelper.createRichTextString(datos.getColumnName(i)));
        cell.setCellStyle(style);

        //Autoajustar
        nuevaHoja.autoSizeColumn(i);
    }

    //Escribir los datos de la tabla en la hoja
    for (int i = 0; i < datos.getRowCount(); i++) {
        Row fila = nuevaHoja.createRow(i + 1);
        CellStyle style = crearEstiloCelda(workbook, IndexedColors.GREEN, CellStyle.BORDER_THIN, (short) -18,
                (short) -18);

        for (int j = 0; j < datos.getColumnCount(); j++) {
            Cell cell = fila.createCell(j);

            //Escribir de acuerdo al tipo de dato
            if (datos.getColumnClass(j).equals(String.class)) {
                cell.setCellValue(createHelper.createRichTextString(datos.getValueAt(i, j).toString()));

            } else if (datos.getColumnClass(j).equals(Integer.class)) {
                cell.setCellValue((int) datos.getValueAt(i, j));

            } else if (datos.getColumnClass(j).equals(Double.class)) {
                cell.setCellValue((double) datos.getValueAt(i, j));

            } else if (datos.getColumnClass(j).equals(Turno.class)) {
                cell.setCellValue(((Turno) datos.getValueAt(i, j)).toString());

            }

            //Agregar el estilo
            cell.setCellStyle(style);
        }
    }

}