Example usage for org.apache.poi.ss.usermodel HorizontalAlignment CENTER

List of usage examples for org.apache.poi.ss.usermodel HorizontalAlignment CENTER

Introduction

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

Prototype

HorizontalAlignment CENTER

To view the source code for org.apache.poi.ss.usermodel HorizontalAlignment CENTER.

Click Source Link

Document

The horizontal alignment is centered, meaning the text is centered across the cell.

Usage

From source file:preprocessing.EnemyInfoGenerator.java

private static void createCellStyles(XSSFWorkbook book) {
    XSSFDataFormat format = book.createDataFormat();
    XSSFFont font = book.createFont();/*from w  ww.ja  va2  s  . c  om*/
    font.setFontHeightInPoints((short) 9);
    font.setFontName("");
    XSSFFont headerFont = book.createFont();
    headerFont.setFontHeightInPoints((short) 9);
    headerFont.setBold(true);
    headerFont.setColor(new XSSFColor(new Color(0, 176, 80)));
    headerFont.setFontName("");

    headerStyle = book.createCellStyle();
    headerStyle.setAlignment(HorizontalAlignment.CENTER);
    headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    //        headerStyle.setBorderRight(CellStyle.BORDER_THIN);
    //        headerStyle.setBorderLeft(CellStyle.BORDER_THIN);
    //        headerStyle.setBorderTop(CellStyle.BORDER_THIN);
    //        headerStyle.setBorderBottom(CellStyle.BORDER_THIN);
    headerStyle.setFont(headerFont);
    headerStyle.setWrapText(true);

    contentStyle = book.createCellStyle();
    contentStyle.setAlignment(HorizontalAlignment.CENTER);
    contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    //        contentStyle.setBorderRight(CellStyle.BORDER_THIN);
    //        contentStyle.setBorderLeft(CellStyle.BORDER_THIN);
    //        contentStyle.setBorderTop(CellStyle.BORDER_THIN);
    //        contentStyle.setBorderBottom(CellStyle.BORDER_THIN);
    contentStyle.setFont(font);

    moneyStyle = book.createCellStyle();
    moneyStyle.setAlignment(HorizontalAlignment.CENTER);
    moneyStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    moneyStyle.setBorderRight(CellStyle.BORDER_THIN);
    moneyStyle.setBorderLeft(CellStyle.BORDER_THIN);
    moneyStyle.setBorderTop(CellStyle.BORDER_THIN);
    moneyStyle.setBorderBottom(CellStyle.BORDER_THIN);
    moneyStyle.setDataFormat(format.getFormat("$#,##0.00_);[Red]($#,##0.00)"));
    moneyStyle.setFont(font);

}

From source file:ro.dabuno.office.integration.BusinessPlan.java

License:Apache License

/**
 * create a library of cell styles//  w  w w. j  ava2  s .co  m
 */
private static Map<String, CellStyle> createStyles(Workbook wb) {
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
    DataFormat df = wb.createDataFormat();

    CellStyle style;
    Font headerFont = wb.createFont();
    headerFont.setBold(true);
    style = createBorderedStyle(wb);
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setFont(headerFont);
    styles.put("header", style);

    style = createBorderedStyle(wb);
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setFont(headerFont);
    style.setDataFormat(df.getFormat("d-mmm"));
    styles.put("header_date", style);

    Font font1 = wb.createFont();
    font1.setBold(true);
    style = createBorderedStyle(wb);
    style.setAlignment(HorizontalAlignment.LEFT);
    style.setFont(font1);
    styles.put("cell_b", style);

    style = createBorderedStyle(wb);
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setFont(font1);
    styles.put("cell_b_centered", style);

    style = createBorderedStyle(wb);
    style.setAlignment(HorizontalAlignment.RIGHT);
    style.setFont(font1);
    style.setDataFormat(df.getFormat("d-mmm"));
    styles.put("cell_b_date", style);

    style = createBorderedStyle(wb);
    style.setAlignment(HorizontalAlignment.RIGHT);
    style.setFont(font1);
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setDataFormat(df.getFormat("d-mmm"));
    styles.put("cell_g", style);

    Font font2 = wb.createFont();
    font2.setColor(IndexedColors.BLUE.getIndex());
    font2.setBold(true);
    style = createBorderedStyle(wb);
    style.setAlignment(HorizontalAlignment.LEFT);
    style.setFont(font2);
    styles.put("cell_bb", style);

    style = createBorderedStyle(wb);
    style.setAlignment(HorizontalAlignment.RIGHT);
    style.setFont(font1);
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setDataFormat(df.getFormat("d-mmm"));
    styles.put("cell_bg", style);

    Font font3 = wb.createFont();
    font3.setFontHeightInPoints((short) 14);
    font3.setColor(IndexedColors.DARK_BLUE.getIndex());
    font3.setBold(true);
    style = createBorderedStyle(wb);
    style.setAlignment(HorizontalAlignment.LEFT);
    style.setFont(font3);
    style.setWrapText(true);
    styles.put("cell_h", style);

    style = createBorderedStyle(wb);
    style.setAlignment(HorizontalAlignment.LEFT);
    style.setWrapText(true);
    styles.put("cell_normal", style);

    style = createBorderedStyle(wb);
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setWrapText(true);
    styles.put("cell_normal_centered", style);

    style = createBorderedStyle(wb);
    style.setAlignment(HorizontalAlignment.RIGHT);
    style.setWrapText(true);
    style.setDataFormat(df.getFormat("d-mmm"));
    styles.put("cell_normal_date", style);

    style = createBorderedStyle(wb);
    style.setAlignment(HorizontalAlignment.LEFT);
    style.setIndention((short) 1);
    style.setWrapText(true);
    styles.put("cell_indented", style);

    style = createBorderedStyle(wb);
    style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    styles.put("cell_blue", style);

    return styles;
}

From source file:ro.dabuno.office.integration.LoadCalculator.java

/**
 * cell styles used for formatting calendar sheets
 *///from ww w. j av a 2  s  . c  om
private static Map<String, CellStyle> createStyles(Workbook wb) {
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();

    CellStyle style;
    Font titleFont = wb.createFont();
    titleFont.setFontHeightInPoints((short) 14);
    titleFont.setFontName("Trebuchet MS");
    style = wb.createCellStyle();
    style.setFont(titleFont);
    style.setBorderBottom(BorderStyle.DOTTED);
    style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    styles.put("title", style);

    Font itemFont = wb.createFont();
    itemFont.setFontHeightInPoints((short) 9);
    itemFont.setFontName("Trebuchet MS");
    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.LEFT);
    style.setFont(itemFont);
    styles.put("item_left", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.RIGHT);
    style.setFont(itemFont);
    styles.put("item_right", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.RIGHT);
    style.setFont(itemFont);
    style.setBorderRight(BorderStyle.DOTTED);
    style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderBottom(BorderStyle.DOTTED);
    style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderLeft(BorderStyle.DOTTED);
    style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderTop(BorderStyle.DOTTED);
    style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setDataFormat(
            wb.createDataFormat().getFormat("_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(@_)"));
    styles.put("input_$", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.RIGHT);
    style.setFont(itemFont);
    style.setBorderRight(BorderStyle.DOTTED);
    style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderBottom(BorderStyle.DOTTED);
    style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderLeft(BorderStyle.DOTTED);
    style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderTop(BorderStyle.DOTTED);
    style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setDataFormat(wb.createDataFormat().getFormat("0.000%"));
    styles.put("input_%", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.RIGHT);
    style.setFont(itemFont);
    style.setBorderRight(BorderStyle.DOTTED);
    style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderBottom(BorderStyle.DOTTED);
    style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderLeft(BorderStyle.DOTTED);
    style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderTop(BorderStyle.DOTTED);
    style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setDataFormat(wb.createDataFormat().getFormat("0"));
    styles.put("input_i", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setFont(itemFont);
    style.setDataFormat(wb.createDataFormat().getFormat("m/d/yy"));
    styles.put("input_d", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.RIGHT);
    style.setFont(itemFont);
    style.setBorderRight(BorderStyle.DOTTED);
    style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderBottom(BorderStyle.DOTTED);
    style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderLeft(BorderStyle.DOTTED);
    style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderTop(BorderStyle.DOTTED);
    style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setDataFormat(wb.createDataFormat().getFormat("$##,##0.00"));
    style.setBorderBottom(BorderStyle.DOTTED);
    style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    styles.put("formula_$", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.RIGHT);
    style.setFont(itemFont);
    style.setBorderRight(BorderStyle.DOTTED);
    style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderBottom(BorderStyle.DOTTED);
    style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderLeft(BorderStyle.DOTTED);
    style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setBorderTop(BorderStyle.DOTTED);
    style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setDataFormat(wb.createDataFormat().getFormat("0"));
    style.setBorderBottom(BorderStyle.DOTTED);
    style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    styles.put("formula_i", style);

    return styles;
}

From source file:se.mithlond.services.content.impl.ejb.report.ExcelReportServiceBean.java

License:Apache License

/**
 * {@inheritDoc}/*from   w  w w. java  2  s. c  o  m*/
 */
@Override
@SuppressWarnings("all")
public CellStyle getCellStyle(final ExcelElement el, final Workbook workbook) {

    // Check sanity
    Validate.notNull(workbook, "workbook");
    Validate.notNull(el, "el");

    // Acquire the el and Font as expected
    final CellStyle toReturn = workbook.createCellStyle();
    final Font theFont = workbook.createFont();

    switch (el) {

    case TITLE:
        theFont.setFontHeightInPoints((short) 18);
        theFont.setBold(true);
        theFont.setColor(IndexedColors.BLUE_GREY.getIndex());

        toReturn.setAlignment(HorizontalAlignment.CENTER);
        toReturn.setVerticalAlignment(VerticalAlignment.CENTER);
        break;

    case HEADER:
        theFont.setFontHeightInPoints((short) 11);
        theFont.setColor(IndexedColors.WHITE.getIndex());

        toReturn.setAlignment(HorizontalAlignment.CENTER);
        toReturn.setVerticalAlignment(VerticalAlignment.CENTER);
        toReturn.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
        toReturn.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        toReturn.setWrapText(true);
        break;

    case CELL:

        toReturn.setAlignment(HorizontalAlignment.LEFT);
        toReturn.setWrapText(true);
        toReturn.setBorderRight(BORDER_THIN);
        toReturn.setRightBorderColor(GREY_25_PERCENT);
        toReturn.setBorderLeft(BORDER_THIN);
        toReturn.setLeftBorderColor(GREY_25_PERCENT);
        toReturn.setBorderTop(BORDER_THIN);
        toReturn.setTopBorderColor(GREY_25_PERCENT);
        toReturn.setBorderBottom(BORDER_THIN);
        toReturn.setBottomBorderColor(GREY_25_PERCENT);
        break;

    case NON_WRAPPING:
        toReturn.setAlignment(HorizontalAlignment.LEFT);
        toReturn.setWrapText(false);
        toReturn.setBorderRight(BORDER_THIN);
        toReturn.setRightBorderColor(GREY_25_PERCENT);
        toReturn.setBorderLeft(BORDER_THIN);
        toReturn.setLeftBorderColor(GREY_25_PERCENT);
        toReturn.setBorderTop(BORDER_THIN);
        toReturn.setTopBorderColor(GREY_25_PERCENT);
        toReturn.setBorderBottom(BORDER_THIN);
        toReturn.setBottomBorderColor(GREY_25_PERCENT);
        break;

    case FORMULA:
        toReturn.setAlignment(HorizontalAlignment.CENTER);
        toReturn.setVerticalAlignment(VerticalAlignment.CENTER);
        toReturn.setFillForegroundColor(GREY_25_PERCENT);
        toReturn.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        toReturn.setDataFormat(workbook.createDataFormat().getFormat("0.00"));
        break;

    case ALT_FORMULA:
        toReturn.setAlignment(HorizontalAlignment.CENTER);
        toReturn.setVerticalAlignment(VerticalAlignment.CENTER);
        toReturn.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
        toReturn.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        toReturn.setDataFormat(workbook.createDataFormat().getFormat("0.00"));
        break;

    default:
        throw new IllegalArgumentException(
                "Style [" + el.name() + "] was not defined. " + "Blame the programmer.");
    }

    // All done.
    toReturn.setFont(theFont);
    return toReturn;
}

From source file:standarapp.algorithm.ReadFileVector.java

public String lectureRegistry(String nameFile, String nameOut, int[] col, double percent, int rowBegin) {
    String answer = "";
    int quantityFound = 0;

    workbook = Lecture.lectureXLSX(nameFile);
    sheet = workbook.getSheetAt(0);//from   w  ww.jav a  2 s .  c  o  m

    for (Row row : sheet) {
        if (row.getRowNum() < rowBegin) {
            continue;
        }

        String[] cellsWI = new String[col.length + 1];
        for (int i = 0; i < col.length; i++) {
            cellsWI[i] = "";
            try {
                Cell cell = row.getCell(col[i]);
                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    cellsWI[i] = deleteTrash(cell.getStringCellValue());
                } else {
                    cellsWI[i] = cell.getDateCellValue().toString();
                    cellsWI[col.length] = String.valueOf(cell.getDateCellValue().getMonth());
                }
                if (i == 2) {
                    cellsWI[i] = cell.getStringCellValue();
                }
            } catch (Exception e) {
            }
        }
        registry.add(cellsWI);
    }

    System.out.println("Total: " + registry.size());
    /*for (int i = 0; i < registry.size(); i++) {
    System.out.println(i +" Municipio: " + registry.get(i)[0] + " | Localidad: " + registry.get(i)[1] + " | Especie: " + registry.get(i)[2] + " | Fecha: " + registry.get(i)[3]);
    }*/

    int rowCount = 0;
    int columnCount = 0;

    CellStyle cs = workbook.createCellStyle();
    //Font font = workbook.createFont();
    cs.setAlignment(HorizontalAlignment.CENTER);
    cs.setVerticalAlignment(VerticalAlignment.CENTER);
    cs.setBorderRight(BorderStyle.THIN);
    cs.setBorderLeft(BorderStyle.THIN);
    cs.setBorderBottom(BorderStyle.THIN);
    cs.setBorderTop(BorderStyle.THIN);

    sheet = workbook.createSheet();
    sheet.setFitToPage(true);
    sheet.setHorizontallyCenter(true);
    sheet.setColumnWidth(0, 20);

    Row row = sheet.createRow(0);
    Cell encabezado = row.createCell(rowCount);
    encabezado.setCellValue("Base de  datos coordenadas");
    encabezado.setCellStyle(cs);

    CellRangeAddress region = new CellRangeAddress(0, 0, 0, 8);
    sheet.addMergedRegion(region);

    row = sheet.createRow(++rowCount);
    Cell cell = row.createCell(columnCount);
    cell.setCellValue("Especie");
    cell.setCellStyle(cs);
    cell = row.createCell(++columnCount);
    cell.setCellValue("Municipio");
    cell.setCellStyle(cs);
    cell = row.createCell(++columnCount);
    cell.setCellValue("Codigo Municipio");
    cell.setCellStyle(cs);
    cell = row.createCell(++columnCount);
    cell.setCellValue("Vereda");
    cell.setCellStyle(cs);
    cell = row.createCell(++columnCount);
    cell.setCellValue("Codigo Vereda");
    cell.setCellStyle(cs);
    cell = row.createCell(++columnCount);
    cell.setCellValue("Mes");
    cell.setCellStyle(cs);
    cell = row.createCell(++columnCount);
    cell.setCellValue("Ao");
    cell.setCellStyle(cs);
    cell = row.createCell(++columnCount);
    cell.setCellValue("Latitud");
    cell.setCellStyle(cs);
    cell = row.createCell(++columnCount);
    cell.setCellValue("Longitud");
    cell.setCellStyle(cs);
    cell = row.createCell(++columnCount);
    cell.setCellValue("Fuente");
    cell.setCellStyle(cs);

    for (int i = 0; i < registry.size(); i++) {
        try {
            String[] registro = registry.get(i);
            columnCount = -1;
            int cod_Mncp = 0;
            row = sheet.createRow(++rowCount);
            double levenstein = 0;
            double localidad_oficial = 0;
            double levensteinActual = 0;

            for (Integer codMunicipio : codigo_Municipio.keySet()) {
                if (registro[0].equals(codigo_Municipio.get(codMunicipio))) {
                    cod_Mncp = codMunicipio;
                    break;
                }

                try {
                    double levenstein_local = FuzzySearch.ratio(registro[0],
                            codigo_Municipio.get(codMunicipio));
                    if (levenstein_local >= levensteinActual) {
                        cod_Mncp = codMunicipio;
                        levensteinActual = levenstein_local;
                    }

                    if (levensteinActual == 100) {
                        break;
                    }
                } catch (Exception e) {
                }
            }

            for (Double cod_Loc : mncp_localidad.get(cod_Mncp).keySet()) {
                String loc = mncp_localidad.get(cod_Mncp).get(cod_Loc);

                if (registro[1].equals(loc)) {
                    localidad_oficial = cod_Loc;
                    levenstein = 101;
                }

                try {
                    double levenstein_local = FuzzySearch.ratio(registro[1], loc);
                    if (levenstein_local >= levenstein) {
                        localidad_oficial = cod_Loc;
                        levenstein = levenstein_local;
                    }

                    if (levenstein == 100) {
                        break;
                    }

                } catch (Exception e) {
                }
            }

            String mncp_oficial = codigo_Municipio.get(cod_Mncp);
            String loc_oficial = codigo_localidad.get(localidad_oficial);
            String especie = registro[2];
            double locX = localidad_x.get(localidad_oficial);
            double locY = localidad_y.get(localidad_oficial);
            int year = 0;
            int month = 0;
            System.out.println();
            try {
                year = Integer.parseInt(registro[3].split(" ")[5]);
                month = Integer.parseInt(registro[registro.length - 1]) + 1;
            } catch (Exception e) {
                year = Integer.parseInt(registro[3].substring(registro[3].length() - 4));
                month = Integer
                        .parseInt(registro[3].substring(registro[3].length() - 7, registro[3].length() - 5));
            }

            quantityFound++;

            cell = row.createCell(++columnCount);
            cell.setCellValue(especie);
            cell.setCellStyle(cs);
            cell = row.createCell(++columnCount);
            cell.setCellValue(mncp_oficial);
            cell.setCellStyle(cs);
            cell = row.createCell(++columnCount);
            cell.setCellValue(cod_Mncp);
            cell.setCellStyle(cs);
            cell = row.createCell(++columnCount);
            cell.setCellValue(loc_oficial);
            cell.setCellStyle(cs);
            cell = row.createCell(++columnCount);
            cell.setCellValue(localidad_oficial);
            cell.setCellStyle(cs);
            cell = row.createCell(++columnCount);
            cell.setCellValue(month);
            cell.setCellStyle(cs);
            cell = row.createCell(++columnCount);
            cell.setCellValue(year);
            cell.setCellStyle(cs);
            cell = row.createCell(++columnCount);
            cell.setCellValue(locY);
            cell.setCellStyle(cs);
            cell = row.createCell(++columnCount);
            cell.setCellValue(locX);
            cell.setCellStyle(cs);
        } catch (Exception e) {
            continue;
        }
    }

    sheet.setColumnWidth(0, 5800);
    sheet.setColumnWidth(1, 5800);
    sheet.setColumnWidth(2, 3000);
    sheet.setColumnWidth(3, 5800);
    sheet.setColumnWidth(4, 3000);
    sheet.setColumnWidth(5, 3000);
    sheet.setColumnWidth(6, 3000);
    sheet.setColumnWidth(7, 6400);
    sheet.setColumnWidth(8, 6400);

    answer = "Se generaron " + quantityFound + " vector(es)";
    try (FileOutputStream outputStream = new FileOutputStream(nameOut)) {
        workbook.write(outputStream);
    } catch (IOException ex) {
        quantityFound = 0;
        answer = "Cerrar el archivo de entrada ";
    }
    return answer;
}

From source file:testoffice.ExcelPrinter.java

public ExcelPrinter(Calendar c, Employee[] emp) {

    int monthDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);

    workbook = new HSSFWorkbook();
    sheet = workbook.createSheet("New sheet");
    style = workbook.createCellStyle();/*from ww w  . j a v  a2s.c  o  m*/

    style.setAlignment(HorizontalAlignment.CENTER);

    printTableCap(c, monthDay);
    printEmployees(emp);

    sheet.autoSizeColumn(0);
}

From source file:tr.org.liderahenk.liderconsole.core.utils.SWTResourceManager.java

License:Open Source License

private static XSSFWorkbook createWorkbookFromTable(TableViewer tableViewer, String sheetName) {

    // Create workbook & sheet
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet(sheetName == null ? "Sheet1" : sheetName);

    // Shade the background of the header row
    XSSFCellStyle headerStyle = wb.createCellStyle();
    headerStyle.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    headerStyle.setBorderTop(CellStyle.BORDER_THIN);
    headerStyle.setBorderBottom(CellStyle.BORDER_THIN);
    headerStyle.setBorderLeft(CellStyle.BORDER_THIN);
    headerStyle.setBorderRight(CellStyle.BORDER_THIN);
    headerStyle.setAlignment(HorizontalAlignment.CENTER);

    // Add header row
    Table table = tableViewer.getTable();
    TableColumn[] columns = table.getColumns();
    int rowIndex = 0;
    int cellIndex = 0;
    XSSFRow header = sheet.createRow((short) rowIndex++);
    for (TableColumn column : columns) {
        XSSFCell cell = header.createCell(cellIndex++);
        cell.setCellValue(column.getText());
        cell.setCellStyle(headerStyle);//from w  ww  .j a va  2s . c  o m
    }

    // Add data rows
    TableItem[] items = tableViewer.getTable().getItems();
    for (TableItem item : items) {
        // create a new row
        XSSFRow row = sheet.createRow((short) rowIndex++);
        cellIndex = 0;

        for (int i = 0; i < columns.length; i++) {
            // Create a new cell
            XSSFCell cell = row.createCell(cellIndex++);
            String text = item.getText(i);

            // Set the horizontal alignment (default to RIGHT)
            XSSFCellStyle cellStyle = wb.createCellStyle();
            if (LiderCoreUtils.isInteger(text)) {
                cellStyle.setAlignment(HorizontalAlignment.RIGHT);
            } else if (LiderCoreUtils.isValidDate(text,
                    ConfigProvider.getInstance().get(LiderConstants.CONFIG.DATE_FORMAT))) {
                cellStyle.setAlignment(HorizontalAlignment.CENTER);
            } else {
                cellStyle.setAlignment(HorizontalAlignment.LEFT);
            }
            cell.setCellStyle(cellStyle);

            // Set the cell's value
            cell.setCellValue(text);
        }
    }

    // Auto-fit the columns
    for (int i = 0; i < columns.length; i++) {
        sheet.autoSizeColumn((short) i);
    }

    return wb;
}

From source file:uk.co.certait.htmlexporter.writer.excel.ExcelStyleGenerator.java

License:Apache License

protected void applyHorizontalAlignment(Style style, XSSFCellStyle cellStyle) {
    if (style.isHorizontallyAlignedLeft()) {
        cellStyle.setAlignment(HorizontalAlignment.LEFT);
    } else if (style.isHorizontallyAlignedRight()) {
        cellStyle.setAlignment(HorizontalAlignment.RIGHT);
    } else if (style.isHorizontallyAlignedCenter()) {
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
    }//www .j  a v  a 2  s  .  c  o  m
}