Example usage for org.apache.poi.ss.usermodel VerticalAlignment TOP

List of usage examples for org.apache.poi.ss.usermodel VerticalAlignment TOP

Introduction

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

Prototype

VerticalAlignment TOP

To view the source code for org.apache.poi.ss.usermodel VerticalAlignment TOP.

Click Source Link

Document

The vertical alignment is aligned-to-top.

Usage

From source file:achmad.rifai.admin.ui.Saver.java

private void konten(int i, XSSFRow r, String s) {
    org.apache.poi.xssf.usermodel.XSSFCell c = r.createCell(i);
    c.setCellType(CellType.STRING);/*from  w w  w  .j  av a  2s.c o  m*/
    c.setCellValue(s);
    org.apache.poi.xssf.usermodel.XSSFCellStyle cs = c.getCellStyle();
    cs.setFillBackgroundColor(new org.apache.poi.xssf.usermodel.XSSFColor(Color.WHITE));
    cs.setFillForegroundColor(new org.apache.poi.xssf.usermodel.XSSFColor(Color.BLACK));
    cs.setAlignment(HorizontalAlignment.JUSTIFY);
    cs.setVerticalAlignment(VerticalAlignment.TOP);
    cs.setBorderBottom(BorderStyle.DASHED);
    cs.setBorderTop(BorderStyle.DASHED);
    cs.setBorderLeft(BorderStyle.DASHED);
    cs.setBorderRight(BorderStyle.DASHED);
}

From source file:com.github.igor_kudryashov.utils.excel.ExcelWriter.java

License:Apache License

/**
 * Returns a style of cell/*from w  w  w .java2  s .  co m*/
 *
 * @param rownum
 *            the number of row for count odd/even rows
 * @param entry
 *            value of cell
 * @param header
 *            <code>true</code> if this row is the header, otherwise
 *            <code>false</code>
 * @return the cell style
 */
private XSSFCellStyle getCellStyle(int rownum, Object entry, boolean header) {
    XSSFCellStyle style;
    String name = entry.getClass().getName();
    if (header) {
        name += "_header";
    } else if ((rownum % 2) == 0) {
        name += "_even";
    }
    if (styles.containsKey(name)) {
        // if we already have a style for this class, return it
        style = styles.get(name);
    } else {
        // create new style
        style = (XSSFCellStyle) workbook.createCellStyle();
        style.setVerticalAlignment(VerticalAlignment.TOP);
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBorderColor(XSSFCellBorder.BorderSide.BOTTOM, colorBorder);
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setBorderColor(XSSFCellBorder.BorderSide.LEFT, colorBorder);
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setBorderColor(XSSFCellBorder.BorderSide.RIGHT, colorBorder);
        // format data
        XSSFDataFormat fmt = (XSSFDataFormat) workbook.createDataFormat();
        short format = 0;
        if (name.contains("Date")) {
            format = fmt.getFormat(BuiltinFormats.getBuiltinFormat(0xe));
            style.setAlignment(CellStyle.ALIGN_LEFT);
        } else if (name.contains("Double")) {
            format = fmt.getFormat(BuiltinFormats.getBuiltinFormat(2));
            style.setAlignment(CellStyle.ALIGN_RIGHT);
        } else if (name.contains("Integer")) {
            format = fmt.getFormat(BuiltinFormats.getBuiltinFormat(1));
            style.setAlignment(CellStyle.ALIGN_RIGHT);
        } else {
            style.setAlignment(CellStyle.ALIGN_LEFT);
            if (!header) {
                style.setWrapText(true);
            }
        }

        if (header) {
            // for header
            style.setFillForegroundColor(colorHeaderBackground);
            style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        } else if (name.contains("_even")) {
            // for even rows
            style.setFillForegroundColor(colorEvenCellBackground);
            style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        }
        style.setDataFormat(format);
        // keep the style for reuse
        styles.put(name, style);
    }
    return style;
}

From source file:com.guardias.excel.CalendarToExcel.java

License:Apache License

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

    short borderColor = IndexedColors.GREY_50_PERCENT.getIndex();

    CellStyle style;
    Font titleFont = wb.createFont();
    titleFont.setFontHeightInPoints((short) 48);
    titleFont.setColor(IndexedColors.DARK_BLUE.getIndex());
    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setFont(titleFont);
    styles.put("title", style);

    Font monthFont = wb.createFont();
    monthFont.setFontHeightInPoints((short) 12);
    monthFont.setColor(IndexedColors.WHITE.getIndex());
    monthFont.setBold(true);
    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setFont(monthFont);
    styles.put("month", style);

    Font dayFont = wb.createFont();
    dayFont.setFontHeightInPoints((short) 14);
    dayFont.setBold(true);
    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.LEFT);
    style.setVerticalAlignment(VerticalAlignment.TOP);
    style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderLeft(BorderStyle.THIN);
    style.setLeftBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    style.setFont(dayFont);
    styles.put("weekend_left", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.TOP);
    style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    styles.put("weekend_right", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.LEFT);
    style.setVerticalAlignment(VerticalAlignment.TOP);
    style.setBorderLeft(BorderStyle.THIN);
    style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setLeftBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    style.setFont(dayFont);
    styles.put("workday_left", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.TOP);
    style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    styles.put("workday_right", style);

    style = wb.createCellStyle();
    style.setBorderLeft(BorderStyle.THIN);
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    styles.put("grey_left", style);

    style = wb.createCellStyle();
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    styles.put("grey_right", style);

    return styles;
}

From source file:com.lufs.java.apache.poi.example.CalendarDemo.java

License:Apache License

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

    short borderColor = IndexedColors.GREY_50_PERCENT.getIndex();

    CellStyle style;
    Font titleFont = wb.createFont();
    titleFont.setFontHeightInPoints((short) 48);
    titleFont.setColor(IndexedColors.DARK_BLUE.getIndex());
    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setFont(titleFont);
    styles.put("title", style);

    Font monthFont = wb.createFont();
    monthFont.setFontHeightInPoints((short) 12);
    monthFont.setColor(IndexedColors.WHITE.getIndex());
    monthFont.setBold(true);
    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setFont(monthFont);
    styles.put("month", style);

    Font dayFont = wb.createFont();
    dayFont.setFontHeightInPoints((short) 14);
    dayFont.setBold(true);
    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.LEFT);
    style.setVerticalAlignment(VerticalAlignment.TOP);
    style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderLeft(BorderStyle.THIN);
    style.setLeftBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    style.setFont(dayFont);
    styles.put("weekend_left", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.TOP);
    style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    styles.put("weekend_right", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.LEFT);
    style.setVerticalAlignment(VerticalAlignment.TOP);
    style.setBorderLeft(BorderStyle.THIN);
    style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setLeftBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    style.setFont(dayFont);
    styles.put("workday_left", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.TOP);
    style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    styles.put("workday_right", style);

    style = wb.createCellStyle();
    style.setBorderLeft(BorderStyle.THIN);
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    styles.put("grey_left", style);

    style = wb.createCellStyle();
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    styles.put("grey_right", style);

    return styles;
}

From source file:com.respam.comniq.models.POIexcelExporter.java

License:Open Source License

public void excelWriter(JSONObject parsedObj, int rownum) throws IOException {
    String path = System.getProperty("user.home") + File.separator + "comniq" + File.separator + "output";
    File file = new File(path + File.separator + "POImovieInfo.xlsx");

    String thumbnailPath = System.getProperty("user.home") + File.separator + "comniq" + File.separator
            + "output" + File.separator + "thumbnails";
    File posterFile = new File(thumbnailPath + File.separator + parsedObj.get("Title") + ".jpg");

    if (!file.exists()) {
        createFile();/*from  w w w. jav a  2  s  . c  o  m*/
    }

    if (file.exists() && checked.equals(false)) {
        findLastRow();
    }

    try {
        FileInputStream fis = new FileInputStream(file);
        XSSFWorkbook workbook = new XSSFWorkbook(fis);

        XSSFSheet sheet = workbook.getSheet("Movies");

        Map<String, Object[]> label = new TreeMap<>();
        label.put("1",
                new Object[] { "", parsedObj.get("Title"), parsedObj.get("Released"),
                        parsedObj.get("Metascore"), parsedObj.get("imdbRating"), parsedObj.get("Plot"),
                        parsedObj.get("imdbID"), parsedObj.get("Genre"), parsedObj.get("Director"),
                        parsedObj.get("Actors"), parsedObj.get("Rated"), parsedObj.get("Runtime") });

        Set<String> keyset = label.keySet();

        // Setting Style for the Label Row

        XSSFCellStyle contentStyle = workbook.createCellStyle();
        contentStyle.setWrapText(true);
        contentStyle.setVerticalAlignment(VerticalAlignment.TOP);

        rownum = rownum + lastRow;

        if (posterFile.exists()) {
            InputStream imageStream = new FileInputStream(
                    thumbnailPath + File.separator + parsedObj.get("Title") + ".jpg");
            byte[] imageBytes = IOUtils.toByteArray(imageStream);
            pictureureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG);
            imageStream.close();

            CreationHelper helper = workbook.getCreationHelper();
            drawing = sheet.createDrawingPatriarch();
            anchor = helper.createClientAnchor();

        }

        for (String key : keyset) {

            Row row = sheet.createRow(rownum++);
            row.setHeight((short) 2000);
            Object[] objArr = label.get(key);
            int cellnum = 0;
            for (Object obj : objArr) {
                Cell cell = row.createCell(cellnum++);
                cell.setCellStyle(contentStyle);
                cell.setCellValue((String) obj);
            }
            if (posterFile.exists()) {
                anchor.setCol1(0);
                anchor.setRow1(rownum - 1);
                anchor.setCol2(0);
                anchor.setRow2(rownum - 1);
                Picture pict = drawing.createPicture(anchor, pictureureIdx);
                pict.resize(1, 1);
            }
        }
        FileOutputStream out = new FileOutputStream(file);
        workbook.write(out);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:guru.qas.martini.report.DefaultState.java

License:Apache License

@Override
public void updateSuites(Sheet sheet) {
    int lastRowNum = sheet.getLastRowNum();
    Row row = sheet.createRow(0 == lastRowNum ? 0 : lastRowNum + 1);
    row.createCell(0, CellType.STRING).setCellValue("ID");
    row.createCell(1, CellType.STRING).setCellValue("Date");
    row.createCell(2, CellType.STRING).setCellValue("Name");
    row.createCell(3, CellType.STRING).setCellValue("Hostname");
    row.createCell(4, CellType.STRING).setCellValue("IP");
    row.createCell(5, CellType.STRING).setCellValue("Username");
    row.createCell(6, CellType.STRING).setCellValue("Profiles");
    row.createCell(7, CellType.STRING).setCellValue("Environment Variables");

    for (Map.Entry<String, JsonObject> mapEntry : suites.entrySet()) {
        row = sheet.createRow(sheet.getLastRowNum() + 1);

        String id = mapEntry.getKey();
        row.createCell(0, CellType.STRING).setCellValue(id);

        JsonObject suite = mapEntry.getValue();
        JsonPrimitive primitive = suite.getAsJsonPrimitive("startTimestamp");
        Long timestamp = null == primitive ? null : primitive.getAsLong();

        Cell cell = row.createCell(1);/*from  w  w  w .ja v a 2s . c  o m*/
        if (null != timestamp) {
            Workbook workbook = sheet.getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            CreationHelper creationHelper = workbook.getCreationHelper();
            cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("m/d/yy h:mm"));
            cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
            cell.setCellValue(new Date(timestamp));
            cell.setCellStyle(cellStyle);
        }

        cell = row.createCell(2);
        primitive = suite.getAsJsonPrimitive("name");
        cell.setCellValue(null == primitive ? "" : primitive.getAsString());

        cell = row.createCell(3);
        JsonObject host = suite.getAsJsonObject("host");
        primitive = null == host ? null : host.getAsJsonPrimitive("name");
        cell.setCellValue(null == primitive ? "" : primitive.getAsString());

        cell = row.createCell(4);
        primitive = null == host ? null : host.getAsJsonPrimitive("ip");
        cell.setCellValue(null == primitive ? "" : primitive.getAsString());

        cell = row.createCell(5);
        primitive = null == host ? null : host.getAsJsonPrimitive("username");
        cell.setCellValue(null == primitive ? "" : primitive.getAsString());

        cell = row.createCell(6);
        JsonArray array = suite.getAsJsonArray("profiles");
        List<String> profiles = Lists.newArrayList();
        if (null != array) {
            int size = array.size();
            for (int i = 0; i < size; i++) {
                JsonElement element = array.get(i);
                String profile = null == element ? null : element.getAsString();
                profiles.add(profile);
            }
            String profilesValue = Joiner.on('\n').skipNulls().join(profiles);
            cell.setCellValue(profilesValue);
        }

        cell = row.createCell(7);
        JsonObject environmentVariables = suite.getAsJsonObject("environment");
        Map<String, String> index = new TreeMap<>();
        if (null != environmentVariables) {
            Set<Map.Entry<String, JsonElement>> entries = environmentVariables.entrySet();
            for (Map.Entry<String, JsonElement> environmentEntry : entries) {
                String key = environmentEntry.getKey();
                JsonElement element = environmentEntry.getValue();
                String value = null == element ? "" : element.getAsString();
                index.put(key, value);
            }

            String variablesValue = Joiner.on('\n').withKeyValueSeparator('=').useForNull("").join(index);
            cell.setCellValue(variablesValue);
        }
    }

    for (int i = 0; i < 8; i++) {
        sheet.autoSizeColumn(i, false);
    }
}

From source file:guru.qas.martini.report.DefaultTraceabilityMatrix.java

License:Apache License

protected void addResult(State state, Sheet sheet, JsonObject object) {
    int index = sheet.getLastRowNum();
    Row row = sheet.createRow(index + 1);

    for (int i = 0; i < columns.size(); i++) {
        Cell cell = row.createCell(i);/*from w  ww.  j a va 2 s . c  o  m*/
        CellStyle cellStyle = cell.getCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
        TraceabilityColumn column = columns.get(i);
        column.addResult(state, cell, object);
    }
}

From source file:jdbreport.model.io.xls.poi.Excel2003Writer.java

License:Apache License

protected VerticalAlignment convertVerticalAlign(int vAlignment) {
    switch (vAlignment) {
    case jdbreport.model.CellStyle.TOP:
        return VerticalAlignment.TOP;
    case jdbreport.model.CellStyle.BOTTOM:
        return VerticalAlignment.BOTTOM;
    case jdbreport.model.CellStyle.CENTER:
        return VerticalAlignment.CENTER;
    }//  w  w  w  . j a v  a 2  s.c  o m
    return VerticalAlignment.TOP;
}

From source file:org.apache.tika.eval.reports.Report.java

License:Apache License

private void dumpXLSX(Connection c, Path reportsRoot) throws IOException, SQLException {
    Statement st = c.createStatement();
    Path out = reportsRoot.resolve(reportFilename);
    Files.createDirectories(out.getParent());

    SXSSFWorkbook wb = new SXSSFWorkbook(new XSSFWorkbook(), 100, true, true);
    wb.setCompressTempFiles(true);/*from ww  w  . j av  a  2s. c  o  m*/
    defaultIntegerFormatter.reset(wb.getXSSFWorkbook());
    defaultDoubleFormatter.reset(wb.getXSSFWorkbook());
    sqlCellStyle = wb.createCellStyle();
    sqlCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
    sqlCellStyle.setWrapText(true);

    try {
        dumpReportToWorkbook(st, wb);
    } finally {
        try (OutputStream os = Files.newOutputStream(out)) {
            wb.write(os);
        } finally {
            wb.dispose();
        }
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.table.xls.helper.HSSFCellStyleProducer.java

License:Open Source License

/**
 * Converts the given element alignment into one of the VerticalAlignment-constants.
 *
 * @param e the JFreeReport element alignment.
 * @return the VerticalAlignment-Alignment.
 * @throws IllegalArgumentException if an Unknown JFreeReport alignment is given.
 *///from  w ww. ja  v a2  s. c  o  m
protected static VerticalAlignment convertVerticalAlignment(final ElementAlignment e) {
    if (ElementAlignment.TOP.equals(e)) {
        return VerticalAlignment.TOP;
    } else if (ElementAlignment.BOTTOM.equals(e)) {
        return VerticalAlignment.BOTTOM;
    } else if (ElementAlignment.MIDDLE.equals(e)) {
        return VerticalAlignment.CENTER;
    }

    throw new IllegalArgumentException("Invalid alignment");
}