Example usage for org.apache.poi.xssf.usermodel XSSFCell setCellStyle

List of usage examples for org.apache.poi.xssf.usermodel XSSFCell setCellStyle

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFCell setCellStyle.

Prototype

@Override
public void setCellStyle(CellStyle style) 

Source Link

Document

Set the style for the cell.

Usage

From source file:Servelt.ExcelWriter.java

private void setDesCell(XSSFCell cell, String name) {
    XSSFCellStyle style = workbook.createCellStyle();
    style.setFillForegroundColor(HSSFColor.GREEN.index);
    style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

    XSSFFont font = workbook.createFont();
    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style.setFont(font);/*w ww.  j a va 2  s . co  m*/

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

From source file:Sheets.GlobalSheet.java

@Override
protected void createDateRows() throws SQLException {
    int currentColN = 11;
    Row row1 = sheet.getRow(21); //row com os "X" dos dias letivos
    Row row2 = sheet.getRow(23); //row com os dias da semana
    Row row3 = sheet.getRow(24); //row com as datas
    XSSFCellStyle style;//from w ww.  ja  va2 s.c  o m

    for (CustomDate cDate : dates) {
        XSSFCell cell1 = (XSSFCell) row1.getCell(currentColN);
        XSSFCell cell2 = (XSSFCell) row2.getCell(currentColN);
        XSSFCell cell3 = (XSSFCell) row3.getCell(currentColN);
        boolean classRegistered = false;
        for (StudentClassDiscipline studentClassDiscipline : studentClassDisciplines) {
            ArrayList<Class> classes = ClassDAO
                    .getClassesByStudentClassDisciplineAndDate(studentClassDiscipline, cDate.getDate());
            if (!classes.isEmpty()) //se pelo menos uma aula ocorreu
            {
                classRegistered = true;
                studentClassDiscipline.getWeekDays().addDate(cDate); //essa turma/disciplina tem aula no dia da semana de cDate
                for (Class classs : classes) {
                    this.totalHours += classs.getHoursPerDay();
                }
            }

        }
        if (classRegistered) {
            this.nClassDays++;
            cell1.setCellValue("X");
        }

        cell2.setCellValue(" " + cDate.getWeekDay());
        cell3.setCellValue(new java.util.Date(cDate.getDate().getTime()));

        style = (XSSFCellStyle) cell1.getCellStyle().clone();
        XSSFColor fillBackgroundColorColor = style.getFillBackgroundColorColor();
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setFillForegroundColor(fillBackgroundColorColor);
        cell1.setCellStyle(style);

        style = (XSSFCellStyle) cell2.getCellStyle().clone();
        style.setFillPattern(FillPatternType.NO_FILL);
        cell2.setCellStyle(style);

        style = (XSSFCellStyle) cell3.getCellStyle().clone();
        style.setFillPattern(FillPatternType.NO_FILL);
        cell3.setCellStyle(style);

        currentColN++;//vai pra prxima coluna
    }
}

From source file:Sheets.PartialSheet.java

/**
 * Cria as linhas das datas/*from  w  w w  .j a va  2 s. c  o  m*/
 * @throws java.sql.SQLException
 */
@Override
protected void createDateRows() throws SQLException {
    int currentColN = 11;
    Row row1 = sheet.getRow(10); //row com os "X" dos dias letivos
    Row row2 = sheet.getRow(12); //row com os dias da semana
    Row row3 = sheet.getRow(13); //row com as datas
    XSSFCellStyle style;

    for (CustomDate cDate : dates) {
        ArrayList<Class> classes = ClassDAO.getClassesByStudentClassDisciplineAndDate(studentClassDiscipline,
                cDate.getDate());
        XSSFCell cell1 = (XSSFCell) row1.getCell(currentColN);
        XSSFCell cell2 = (XSSFCell) row2.getCell(currentColN);
        XSSFCell cell3 = (XSSFCell) row3.getCell(currentColN);

        if (!classes.isEmpty()) {
            Class classs = classes.get(0);//pega somente a primeira aula (no deveria existir mais de uma aula de uma displiplina em um mesmo dia)
            this.totalHours += classs.getHoursPerDay();
            this.nClassDays++;
            cell1.setCellValue("X");
        }

        cell2.setCellValue(" " + cDate.getWeekDay());
        cell3.setCellValue(new java.util.Date(cDate.getDate().getTime()));

        style = (XSSFCellStyle) cell1.getCellStyle().clone();
        XSSFColor fillBackgroundColorColor = style.getFillBackgroundColorColor();
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setFillForegroundColor(fillBackgroundColorColor);
        cell1.setCellStyle(style);

        style = (XSSFCellStyle) cell2.getCellStyle().clone();
        style.setFillPattern(FillPatternType.NO_FILL);
        cell2.setCellStyle(style);

        style = (XSSFCellStyle) cell3.getCellStyle().clone();
        style.setFillPattern(FillPatternType.NO_FILL);
        cell3.setCellStyle(style);

        currentColN++;//vai pra prxima coluna
    }
}

From source file:sv.com.mined.sieni.controller.GestionNotasController.java

public static void copyRow(XSSFSheet worksheetSource, XSSFSheet worksheetDestination, int sourceRowNum,
        int destinationRowNum) {
    // Get the source / new row
    XSSFRow origen = worksheetSource.getRow(sourceRowNum);
    XSSFRow destino = worksheetDestination.createRow(destinationRowNum);

    // Loop through source columns to add to new row
    for (int i = 0; i < origen.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        XSSFCell oldCell = origen.getCell(i);
        XSSFCell newCell = destino.createCell(i);
        // If the old cell is null jump to next cell
        if (oldCell == null) {
            newCell = null;/*from  w w w  .ja va 2  s  .  com*/
            continue;
        }

        //Ajustar tamaos columnas
        worksheetDestination.setColumnWidth(i, worksheetSource.getColumnWidth(i));

        // Copy style from old cell and apply to new cell
        XSSFCellStyle newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
        newCell.setCellStyle(newCellStyle);

        // If there is a cell comment, copy
        if (oldCell.getCellComment() != null) {
            newCell.setCellComment(oldCell.getCellComment());
        }

        // If there is a cell hyperlink, copy
        if (oldCell.getHyperlink() != null) {
            newCell.setHyperlink(oldCell.getHyperlink());
        }

        // Set the cell data type
        newCell.setCellType(oldCell.getCellType());
        // Set the cell data value
        switch (oldCell.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            newCell.setCellValue(oldCell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_ERROR:
            newCell.setCellErrorValue(oldCell.getErrorCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            newCell.setCellFormula(oldCell.getCellFormula());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        }

    }

}

From source file:tan.jam.jsf.Shifting.java

public static void InsertRow(XSSFWorkbook workbook, XSSFSheet worksheet, int sourceRowNum,
        int destinationRowNum) {

    worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
    XSSFRow newRow = worksheet.getRow(destinationRowNum);
    XSSFRow sourceRow = worksheet.getRow(sourceRowNum);

    if (newRow != null) {
        worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
    } else {/*from w  ww .  j  av  a  2 s . co m*/
        newRow = worksheet.createRow(destinationRowNum);
    }

    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        XSSFCell oldCell = sourceRow.getCell(i);
        XSSFCell newCell = newRow.createCell(i);

        if (oldCell == null) {
            newCell = null;
            continue;
        }

        XSSFCellStyle newCellStyle = workbook.createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
        ;
        newCell.setCellStyle(newCellStyle);

        if (oldCell.getCellComment() != null) {
            newCell.setCellComment(oldCell.getCellComment());
        }

        if (oldCell.getHyperlink() != null) {
            newCell.setHyperlink(oldCell.getHyperlink());
        }

        newCell.setCellType(oldCell.getCellType());

        switch (oldCell.getCellType()) {
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK:
            newCell.setCellValue(oldCell.getStringCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_ERROR:
            newCell.setCellErrorValue(oldCell.getErrorCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA:
            newCell.setCellFormula("+" + "F" + destinationRowNum + "*G" + destinationRowNum);
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC:
            //newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING:
            newCell.setCellValue("");
            break;
        }
    }

    for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
        CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
        if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
            CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(),
                    (newRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())),
                    cellRangeAddress.getFirstColumn(), cellRangeAddress.getLastColumn());
            worksheet.addMergedRegion(newCellRangeAddress);
        }
    }
    int inc = destinationRowNum + 1;
    worksheet.getRow(destinationRowNum).getCell(7).setCellFormula("+F" + inc + "*G" + inc);
}

From source file:tan.jam.jsf.Shifting.java

private static void CopyData(XSSFWorkbook workbook, XSSFSheet worksheet, int sourceRowNum,
        int destinationRowNum, int Mov) {
    XSSFRow newRow = worksheet.getRow(destinationRowNum);
    XSSFRow sourceRow = worksheet.getRow(sourceRowNum);

    for (int i = sourceRow.getLastCellNum(); i > 8 + Mov; i--) {

        int d = i - 1;
        XSSFCell oldCell = sourceRow.getCell(d);
        XSSFCell newCell = newRow.createCell(i);

        if (oldCell == null) {
            newCell = null;//  ww  w. ja v  a2s  . co m
            continue;
        }

        XSSFCellStyle newCellStyle = workbook.createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
        ;
        newCell.setCellStyle(newCellStyle);

        if (oldCell.getCellComment() != null) {
            newCell.setCellComment(oldCell.getCellComment());
        }

        if (oldCell.getHyperlink() != null) {
            newCell.setHyperlink(oldCell.getHyperlink());
        }

        newCell.setCellType(oldCell.getCellType());

        switch (oldCell.getCellType()) {
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK:
            newCell.setCellValue(oldCell.getStringCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_ERROR:
            newCell.setCellErrorValue(oldCell.getErrorCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA:
            newCell.setCellFormula(oldCell.getCellFormula());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        }
    }
}

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   ww w.ja v a2  s.  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:vd10_workbook.AbilityManagement.java

public void createWorkSheet(XSSFWorkbook workbook) {
    XSSFSheet sheet = workbook.createSheet("kha_nang");
    int startRow = 0;
    XSSFRow row = sheet.createRow((short) startRow);

    //== THE TITLE ==//
    //SET HEIGHT OF ROW 2 (in excel)
    row.setHeight((short) 500);
    XSSFCell cell = (XSSFCell) row.createCell((short) 0);
    cell.setCellValue("Kh nng lm vic");

    //MEARGING CELLS 
    //this statement for merging cells
    CellRangeAddress cellRangeAddress = new CellRangeAddress(startRow, //first row (0-based)
            startRow, //last row (0-based)
            0, //first column (0-based)
            2 //last column (0-based)
    );/*from  w  w  w .  jav a2s  . c  o  m*/
    sheet.addMergedRegion(cellRangeAddress);

    // Center Align Cell Contents 
    XSSFCellStyle align = workbook.createCellStyle();
    align.setAlignment(XSSFCellStyle.ALIGN_CENTER);
    align.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
    cell.setCellStyle(align);

    //set border
    this.setRegionBoder(cellRangeAddress, workbook, sheet);

    //==THE LABELS ==//
    //STT
    row = sheet.createRow((short) startRow + 1);
    row.setHeight((short) 400);
    cell = (XSSFCell) row.createCell((short) 0);

    cell.setCellValue("STT");
    this.setThickBorder(cell, workbook);
    this.setBackGroundColor(cell, workbook);

    //ID_NHAN_VIEN
    sheet.setColumnWidth(1, 5000);
    cell = (XSSFCell) row.createCell((short) 1);
    cell.setCellValue("ID Nhn vin");
    this.setThickBorder(cell, workbook);
    this.setBackGroundColor(cell, workbook);

    //ID_NGOAI_NGU
    sheet.setColumnWidth(2, 5000);
    cell = (XSSFCell) row.createCell((short) 2);
    cell.setCellValue("ID Ngoi ng");
    this.setThickBorder(cell, workbook);
    this.setBackGroundColor(cell, workbook);

    //fill out the rows
    for (int i = 0; i < this.list.size(); i++) {
        row = sheet.createRow((short) startRow + 2 + i);
        cell = (XSSFCell) row.createCell((short) 0);
        this.setThinBorder(cell, workbook);
        cell.setCellValue(i + 1);

        cell = (XSSFCell) row.createCell((short) 1);
        this.setThinBorder(cell, workbook);
        cell.setCellValue(this.list.get(i).getEmployeeID());

        cell = (XSSFCell) row.createCell((short) 2);
        this.setThinBorder(cell, workbook);
        cell.setCellValue(this.list.get(i).getLanguageID());

    }
}

From source file:vd10_workbook.AbilityManagement.java

public static void setThickBorder(XSSFCell cell, XSSFWorkbook workbook) {
    XSSFCellStyle border = workbook.createCellStyle();
    border.setBorderTop(XSSFCellStyle.BORDER_MEDIUM);
    border.setBottomBorderColor(IndexedColors.BLACK.getIndex());
    border.setBorderBottom(XSSFCellStyle.BORDER_MEDIUM);
    border.setRightBorderColor(IndexedColors.BLACK.getIndex());
    border.setBorderLeft(XSSFCellStyle.BORDER_MEDIUM);
    border.setTopBorderColor(IndexedColors.BLACK.getIndex());
    border.setBorderRight(XSSFCellStyle.BORDER_MEDIUM);
    border.setLeftBorderColor(IndexedColors.BLACK.getIndex());
    cell.setCellStyle(border);
}

From source file:vd10_workbook.AbilityManagement.java

public static void setThinBorder(XSSFCell cell, XSSFWorkbook workbook) {
    XSSFCellStyle border = workbook.createCellStyle();
    border.setBorderBottom(XSSFCellStyle.BORDER_THIN);
    border.setRightBorderColor(IndexedColors.BLACK.getIndex());
    border.setBorderLeft(XSSFCellStyle.BORDER_THIN);
    border.setTopBorderColor(IndexedColors.BLACK.getIndex());
    border.setBorderRight(XSSFCellStyle.BORDER_THIN);
    border.setLeftBorderColor(IndexedColors.BLACK.getIndex());
    cell.setCellStyle(border);
}