Example usage for org.apache.poi.ss.usermodel CellStyle setAlignment

List of usage examples for org.apache.poi.ss.usermodel CellStyle setAlignment

Introduction

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

Prototype

void setAlignment(HorizontalAlignment align);

Source Link

Document

set the type of horizontal alignment for the cell

Usage

From source file:util.ExcelConverter.java

public static File createXlsx(String[] header, String[][] data, String path) {

    try {// ww  w.ja  v  a 2 s  .  c om
        XSSFWorkbook xwb = new XSSFWorkbook();
        XSSFSheet sheet = xwb.createSheet();

        CellStyle cellStyle = xwb.createCellStyle();
        cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
        cellStyle.setAlignment(CellStyle.VERTICAL_TOP);
        cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        cellStyle.setWrapText(false);

        Font bold = xwb.createFont();
        bold.setBoldweight(Font.BOLDWEIGHT_BOLD);
        bold.setFontHeightInPoints((short) 10);

        CellStyle cellStyleHeader = xwb.createCellStyle();
        cellStyleHeader.setAlignment(CellStyle.ALIGN_LEFT);
        cellStyleHeader.setAlignment(CellStyle.VERTICAL_TOP);
        cellStyleHeader.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setBorderTop(XSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setBorderRight(XSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setFont(bold);
        cellStyleHeader.setWrapText(false);

        XSSFRow row;
        Cell cell;

        //header
        row = sheet.createRow(0);
        for (int i = 0; i < header.length; i++) {
            cell = row.createCell(i);
            cell.setCellStyle(cellStyleHeader);
            cell.setCellValue(header[i]);
        }

        int colCount = header.length;
        int no = 1;

        for (String[] obj : data) {
            row = sheet.createRow(no);
            for (int i = 0; i < colCount; i++) {
                cell = row.createCell(i);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(obj[i]);
            }
            no++;
        }

        for (int i = 0; i < header.length; i++) {
            sheet.autoSizeColumn(i);
        }

        File newFile = new File(path);
        try (FileOutputStream fileOut = new FileOutputStream(path)) {
            xwb.write(fileOut);
        }

        return newFile;
    } catch (IOException e) {
        return null;
    }
}

From source file:util.ExcelConverter.java

public static File createXls(String[] header, String[][] data, String path) {

    try {//  www  . j  ava2  s .c  o m
        HSSFWorkbook xwb = new HSSFWorkbook();
        HSSFSheet sheet = xwb.createSheet();

        CellStyle cellStyle = xwb.createCellStyle();
        cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
        cellStyle.setAlignment(CellStyle.VERTICAL_TOP);
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyle.setWrapText(false);

        Font bold = xwb.createFont();
        bold.setBoldweight(Font.BOLDWEIGHT_BOLD);
        bold.setFontHeightInPoints((short) 10);

        CellStyle cellStyleHeader = xwb.createCellStyle();
        cellStyleHeader.setAlignment(CellStyle.ALIGN_LEFT);
        cellStyleHeader.setAlignment(CellStyle.VERTICAL_TOP);
        cellStyleHeader.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setFont(bold);
        cellStyleHeader.setWrapText(false);

        HSSFRow row;
        Cell cell;

        //header
        row = sheet.createRow(0);
        for (int i = 0; i < header.length; i++) {
            cell = row.createCell(i);
            cell.setCellStyle(cellStyleHeader);
            cell.setCellValue(header[i]);
        }

        int colCount = header.length;
        int no = 1;

        for (String[] obj : data) {
            row = sheet.createRow(no);
            for (int i = 0; i < colCount; i++) {
                cell = row.createCell(i);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(obj[i]);
            }
            no++;
        }

        for (int i = 0; i < header.length; i++) {
            sheet.autoSizeColumn(i);
        }

        File newFile = new File(path);
        try (FileOutputStream fileOut = new FileOutputStream(newFile)) {
            xwb.write(fileOut);
        }

        return newFile;
    } catch (IOException e) {
        return null;
    }
}

From source file:Utilities.ExportToXLSX.java

private void createHeaderRow(Boolean isNewLeave, String fileName) throws IOException {
    // WB = new XSSFWorkbook(); 
    FileOutputStream output;//from   w  ww. ja  v  a 2 s  .  c  om
    //Sheet sheet;

    int titleLen;
    String[] header; // the header of the spreadsheet
    if (isNewLeave) {
        //            titleLen = GlobalVar.NEW_LEAVE_TITLES.length; 
        //            sheet = wb.createSheet("New Leave Form");          
        //            header = GlobalVar.NEW_LEAVE_TITLES;
        titleLen = GlobalVar.LEAVE_TITLES_V1.length;
        sheet = wb.createSheet("New Leave Form");
        header = GlobalVar.LEAVE_TITLES_V1;
    } else {
        //            titleLen = GlobalVar.SIGNED_LEAVE_TITLES.length; 
        //            sheet = wb.createSheet("SIGNED Leave Form");
        //            header = GlobalVar.SIGNED_LEAVE_TITLES;
        titleLen = GlobalVar.LEAVE_TITLES_V1.length;
        sheet = wb.createSheet("SIGNED Leave Form");
        header = GlobalVar.LEAVE_TITLES_V1;
    }

    //set column widths, the width is measured in units of 1/256th of a character width
    for (int i = 0; i < titleLen; i++) {
        sheet.setColumnWidth(i, 256 * GlobalVar.COL_WIDTH);
    }

    // create style for the first row
    CellStyle style;
    Font headerFont = wb.createFont();
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style = createStandardStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFont(headerFont);

    Row row = sheet.createRow(0); // first row

    //set value for the first row
    for (int i = 0; i < header.length; i++) {
        Cell cell = row.createCell(i);
        cell.setCellValue(header[i]);
        cell.setCellStyle(style);
    }

    //freeze the first row
    sheet.createFreezePane(0, 1);

    try {
        output = new FileOutputStream(fileName);
        wb.write(output);
        output.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(ExportToXLSX.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null, "Failed: Excel worksheet is used by another user!");
    }

}

From source file:Utilities.ExportToXLSX.java

private CellStyle createDateStyle() {
    DataFormat df = wb.createDataFormat();

    CellStyle style = createStandardStyle();
    style.setDataFormat(df.getFormat("yyMMdd"));
    style.setAlignment(CellStyle.ALIGN_LEFT);
    return style;
}

From source file:Utilities.ExportToXLSX.java

private CellStyle createSSNStyle() {
    Font font3 = wb.createFont();
    DataFormat df = wb.createDataFormat();
    CellStyle style = wb.createCellStyle();

    style = createStandardStyle();//from ww w  .  j  ava  2s  .com

    style.setDataFormat(df.getFormat("0000"));
    style.setAlignment(CellStyle.ALIGN_LEFT);
    //style.setFont(font3);
    style.setWrapText(true);
    return style;
}

From source file:Utilities.ExportToXLSX.java

private CellStyle createFormulaStyle() {
    Font font3 = wb.createFont();
    DataFormat df = wb.createDataFormat();
    CellStyle style = wb.createCellStyle();
    //        font3.setFontHeightInPoints((short)14);
    //        font3.setColor(IndexedColors.DARK_BLUE.getIndex());
    //        font3.setBoldweight(Font.BOLDWEIGHT_BOLD);       
    style = createStandardStyle();//w ww . j ava 2  s  .co  m

    style.setAlignment(CellStyle.ALIGN_LEFT);
    //style.setFont(font3);
    style.setWrapText(true);
    return style;
}

From source file:utilities.XlsxGenerator.java

private CellStyle createStandardStyle() {
    CellStyle style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    Font font = wb.createFont();//from www. ja va  2  s. c  om
    font.setFontName("Arial");
    font.setFontHeightInPoints((short) 8);
    style.setFont(font);
    //        style.setBorderRight(CellStyle.BORDER_THIN);
    //        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
    //        style.setBorderBottom(CellStyle.BORDER_THIN);
    //        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
    //        style.setBorderLeft(CellStyle.BORDER_THIN);
    //        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
    //        style.setBorderTop(CellStyle.BORDER_THIN);
    //        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
    style.setWrapText(true);
    return style;
}

From source file:utilities.XlsxGenerator.java

private CellStyle createSSNStyle() {
    Font font3 = wb.createFont();
    DataFormat df = wb.createDataFormat();
    CellStyle style = wb.createCellStyle();

    style = createStandardStyle();/*from ww w  . j a  v  a2 s.c o  m*/
    style.setDataFormat(df.getFormat("000-00-0000"));
    style.setAlignment(CellStyle.ALIGN_LEFT);
    //style.setFont(font3);
    style.setWrapText(true);
    return style;
}

From source file:utilities.XlsxGenerator.java

private CellStyle createHeaderStyle() {
    CellStyle style;
    Font headerFont = wb.createFont();
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style = createStandardStyle();/*  w w  w .j  a v  a2  s  .co m*/
    style.setAlignment(CellStyle.ALIGN_CENTER);
    //style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
    //style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFont(headerFont);
    return style;
}

From source file:Utility.TestReports.java

public void setHeaderCellStyle(org.apache.poi.hssf.usermodel.HSSFSheet sheet,
        org.apache.poi.ss.usermodel.Cell cell, org.apache.poi.hssf.usermodel.HSSFWorkbook wb) {
    CellStyle s = null;
    s = sheet.getWorkbook().createCellStyle();
    cell.setCellStyle(s);// w  w w  .ja  v  a2s .  c  o m

    HSSFFont font = wb.createFont();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    s.setFont(font);
    s.setAlignment(CellStyle.ALIGN_GENERAL);
    s.setBorderBottom(CellStyle.BORDER_THIN);
    s.setBorderLeft(CellStyle.BORDER_THIN);
    s.setBorderRight(CellStyle.BORDER_THIN);
    s.setBorderTop(CellStyle.BORDER_THIN);
    s.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    s.setAlignment(CellStyle.ALIGN_LEFT);
    s.setFont(font);

}