Search in sources :

Example 31 with CreationHelper

use of org.apache.poi.ss.usermodel.CreationHelper in project pentaho-kettle by pentaho.

the class ExcelWriterStep method writeField.

// VisibleForTesting
void writeField(Object v, ValueMetaInterface vMeta, ExcelWriterStepField excelField, Row xlsRow, int posX, Object[] row, int fieldNr, boolean isTitle) throws KettleException {
    try {
        boolean cellExisted = true;
        // get the cell
        Cell cell = xlsRow.getCell(posX);
        if (cell == null) {
            cellExisted = false;
            cell = xlsRow.createCell(posX);
        }
        // if cell existed and existing cell's styles should not be changed, don't
        if (!(cellExisted && meta.isLeaveExistingStylesUnchanged())) {
            // if the style of this field is cached, reuse it
            if (!isTitle && data.getCachedStyle(fieldNr) != null) {
                cell.setCellStyle(data.getCachedStyle(fieldNr));
            } else {
                // apply style if requested
                if (excelField != null) {
                    // determine correct cell for title or data rows
                    String styleRef = null;
                    if (!isTitle && !Utils.isEmpty(excelField.getStyleCell())) {
                        styleRef = excelField.getStyleCell();
                    } else if (isTitle && !Utils.isEmpty(excelField.getTitleStyleCell())) {
                        styleRef = excelField.getTitleStyleCell();
                    }
                    if (styleRef != null) {
                        Cell styleCell = getCellFromReference(styleRef);
                        if (styleCell != null && cell != styleCell) {
                            cell.setCellStyle(styleCell.getCellStyle());
                        }
                    }
                }
                // set cell format as specified, specific format overrides cell specification
                if (!isTitle && excelField != null && !Utils.isEmpty(excelField.getFormat()) && !excelField.getFormat().startsWith("Image")) {
                    setDataFormat(excelField.getFormat(), cell);
                }
                // cache it for later runs
                if (!isTitle) {
                    data.cacheStyle(fieldNr, cell.getCellStyle());
                }
            }
        }
        // create link on cell if requested
        if (!isTitle && excelField != null && data.linkfieldnrs[fieldNr] >= 0) {
            String link = data.inputRowMeta.getValueMeta(data.linkfieldnrs[fieldNr]).getString(row[data.linkfieldnrs[fieldNr]]);
            if (!Utils.isEmpty(link)) {
                CreationHelper ch = data.wb.getCreationHelper();
                // set the link on the cell depending on link type
                Hyperlink hyperLink = null;
                if (link.startsWith("http:") || link.startsWith("https:") || link.startsWith("ftp:")) {
                    hyperLink = ch.createHyperlink(HyperlinkType.URL);
                    hyperLink.setLabel("URL Link");
                } else if (link.startsWith("mailto:")) {
                    hyperLink = ch.createHyperlink(HyperlinkType.EMAIL);
                    hyperLink.setLabel("Email Link");
                } else if (link.startsWith("'")) {
                    hyperLink = ch.createHyperlink(HyperlinkType.DOCUMENT);
                    hyperLink.setLabel("Link within this document");
                } else {
                    hyperLink = ch.createHyperlink(HyperlinkType.FILE);
                    hyperLink.setLabel("Link to a file");
                }
                hyperLink.setAddress(link);
                cell.setHyperlink(hyperLink);
                // if cell existed and existing cell's styles should not be changed, don't
                if (!(cellExisted && meta.isLeaveExistingStylesUnchanged())) {
                    if (data.getCachedLinkStyle(fieldNr) != null) {
                        cell.setCellStyle(data.getCachedLinkStyle(fieldNr));
                    } else {
                        // CellStyle style = cell.getCellStyle();
                        Font origFont = data.wb.getFontAt(cell.getCellStyle().getFontIndex());
                        Font hlink_font = data.wb.createFont();
                        // reporduce original font characteristics
                        hlink_font.setBold(origFont.getBold());
                        hlink_font.setCharSet(origFont.getCharSet());
                        hlink_font.setFontHeight(origFont.getFontHeight());
                        hlink_font.setFontName(origFont.getFontName());
                        hlink_font.setItalic(origFont.getItalic());
                        hlink_font.setStrikeout(origFont.getStrikeout());
                        hlink_font.setTypeOffset(origFont.getTypeOffset());
                        // make it blue and underlined
                        hlink_font.setUnderline(Font.U_SINGLE);
                        hlink_font.setColor(IndexedColors.BLUE.getIndex());
                        CellStyle style = cell.getCellStyle();
                        style.setFont(hlink_font);
                        cell.setCellStyle(style);
                        data.cacheLinkStyle(fieldNr, cell.getCellStyle());
                    }
                }
            }
        }
        // create comment on cell if requrested
        if (!isTitle && excelField != null && data.commentfieldnrs[fieldNr] >= 0 && data.wb instanceof XSSFWorkbook) {
            String comment = data.inputRowMeta.getValueMeta(data.commentfieldnrs[fieldNr]).getString(row[data.commentfieldnrs[fieldNr]]);
            if (!Utils.isEmpty(comment)) {
                String author = data.commentauthorfieldnrs[fieldNr] >= 0 ? data.inputRowMeta.getValueMeta(data.commentauthorfieldnrs[fieldNr]).getString(row[data.commentauthorfieldnrs[fieldNr]]) : "Kettle PDI";
                cell.setCellComment(createCellComment(author, comment));
            }
        }
        // cell is getting a formula value or static content
        if (!isTitle && excelField != null && excelField.isFormula()) {
            // formula case
            cell.setCellFormula(vMeta.getString(v));
        } else {
            // static content case
            switch(vMeta.getType()) {
                case ValueMetaInterface.TYPE_DATE:
                    if (v != null && vMeta.getDate(v) != null) {
                        cell.setCellValue(vMeta.getDate(v));
                    }
                    break;
                case ValueMetaInterface.TYPE_BOOLEAN:
                    if (v != null) {
                        cell.setCellValue(vMeta.getBoolean(v));
                    }
                    break;
                case ValueMetaInterface.TYPE_STRING:
                case ValueMetaInterface.TYPE_BINARY:
                    if (v != null) {
                        cell.setCellValue(vMeta.getString(v));
                    }
                    break;
                case ValueMetaInterface.TYPE_BIGNUMBER:
                case ValueMetaInterface.TYPE_NUMBER:
                case ValueMetaInterface.TYPE_INTEGER:
                    if (v != null) {
                        cell.setCellValue(vMeta.getNumber(v));
                    }
                    break;
                default:
                    break;
            }
        }
    } catch (Exception e) {
        logError("Error writing field (" + data.posX + "," + data.posY + ") : " + e.toString());
        logError(Const.getStackTracker(e));
        throw new KettleException(e);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) RichTextString(org.apache.poi.ss.usermodel.RichTextString) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Cell(org.apache.poi.ss.usermodel.Cell) Font(org.apache.poi.ss.usermodel.Font) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) Hyperlink(org.apache.poi.ss.usermodel.Hyperlink)

Example 32 with CreationHelper

use of org.apache.poi.ss.usermodel.CreationHelper in project CzechIdMng by bcvsolutions.

the class IdentityReportXlsxRenderer method render.

@Override
public InputStream render(RptReportDto report) {
    try {
        // read json stream
        JsonParser jParser = getMapper().getFactory().createParser(getReportData(report));
        XSSFWorkbook workbook = new XSSFWorkbook();
        CreationHelper createHelper = workbook.getCreationHelper();
        XSSFSheet sheet = workbook.createSheet("Report");
        // header
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("Username");
        cell = row.createCell(1);
        cell.setCellValue("First name");
        cell = row.createCell(2);
        cell.setCellValue("Last name");
        cell = row.createCell(3);
        cell.setCellValue("Disabled");
        int rowNum = 1;
        // json is array of identities
        if (jParser.nextToken() == JsonToken.START_ARRAY) {
            // write single identity
            while (jParser.nextToken() == JsonToken.START_OBJECT) {
                IdmIdentityDto identity = getMapper().readValue(jParser, IdmIdentityDto.class);
                row = sheet.createRow(rowNum++);
                cell = row.createCell(0);
                cell.setCellValue(identity.getUsername());
                cell = row.createCell(1);
                cell.setCellValue(identity.getFirstName());
                cell = row.createCell(2);
                cell.setCellValue(identity.getLastName());
                cell = row.createCell(3);
                cell.setCellValue(identity.isDisabled());
            }
        }
        // close json stream
        jParser.close();
        // 
        // footer info about more available reports
        rowNum++;
        rowNum++;
        row = sheet.createRow(rowNum++);
        cell = row.createCell(0);
        cell.setCellValue("More reports are available in reports module:");
        row = sheet.createRow(rowNum++);
        cell = row.createCell(0);
        cell.setCellValue("https://wiki.czechidm.com/devel/documentation/modules_reports");
        Hyperlink link = createHelper.createHyperlink(XSSFHyperlink.LINK_URL);
        link.setAddress("https://wiki.czechidm.com/devel/documentation/modules_reports");
        cell.setHyperlink(link);
        // close and return input stream
        return getInputStream(workbook);
    } catch (IOException ex) {
        throw new ReportRenderException(report.getName(), ex);
    }
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ReportRenderException(eu.bcvsolutions.idm.rpt.api.exception.ReportRenderException) Row(org.apache.poi.ss.usermodel.Row) IOException(java.io.IOException) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) Cell(org.apache.poi.ss.usermodel.Cell) JsonParser(com.fasterxml.jackson.core.JsonParser) XSSFHyperlink(org.apache.poi.xssf.usermodel.XSSFHyperlink) Hyperlink(org.apache.poi.ss.usermodel.Hyperlink)

Example 33 with CreationHelper

use of org.apache.poi.ss.usermodel.CreationHelper in project swift by luastar.

the class TestController method download.

@HttpService("/download")
public void download(HttpRequest request, HttpResponse response) {
    try {
        Workbook wb = new XSSFWorkbook();
        CreationHelper createHelper = wb.getCreationHelper();
        Sheet sheet = wb.createSheet("sheet1");
        Row row = sheet.createRow((short) 0);
        row.createCell(0).setCellValue(createHelper.createRichTextString("aaa"));
        row.createCell(1).setCellValue(createHelper.createRichTextString("bbb"));
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        wb.write(outputStream);
        response.setResponseContentTypeStream("aaa.xlsx");
        response.setOutputStream(outputStream);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HttpService(com.luastar.swift.http.server.HttpService)

Example 34 with CreationHelper

use of org.apache.poi.ss.usermodel.CreationHelper in project Aspose.Cells-for-Java by aspose-cells.

the class ApacheAddCommentsToCell method main.

public static void main(String[] args) throws IOException {
    // The path to the documents directory.
    String dataDir = Utils.getDataDir(ApacheAddCommentsToCell.class);
    // or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    CreationHelper factory = wb.getCreationHelper();
    Sheet sheet = wb.createSheet();
    Row row = sheet.createRow(3);
    Cell cell = row.createCell(5);
    cell.setCellValue("F4");
    Drawing drawing = sheet.createDrawingPatriarch();
    // When the comment box is visible, have it show in a 1x3 space
    ClientAnchor anchor = factory.createClientAnchor();
    anchor.setCol1(cell.getColumnIndex());
    anchor.setCol2(cell.getColumnIndex() + 1);
    anchor.setRow1(row.getRowNum());
    anchor.setRow2(row.getRowNum() + 3);
    // Create the comment and set the text+author
    Comment comment = drawing.createCellComment(anchor);
    RichTextString str = factory.createRichTextString("Hello, World!");
    comment.setString(str);
    comment.setAuthor("Apache POI");
    // Assign the comment to the cell
    cell.setCellComment(comment);
    String fname = "AsposeComment-xssf.xls";
    if (wb instanceof XSSFWorkbook)
        fname += "x";
    FileOutputStream out = new FileOutputStream(dataDir + fname);
    wb.write(out);
    out.close();
    System.out.println("Done.");
}
Also used : Drawing(org.apache.poi.ss.usermodel.Drawing) Comment(org.apache.poi.ss.usermodel.Comment) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) RichTextString(org.apache.poi.ss.usermodel.RichTextString) Row(org.apache.poi.ss.usermodel.Row) RichTextString(org.apache.poi.ss.usermodel.RichTextString) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 35 with CreationHelper

use of org.apache.poi.ss.usermodel.CreationHelper in project Aspose.Cells-for-Java by aspose-cells.

the class ApacheHyperlinks method main.

public static void main(String[] args) throws Exception {
    // The path to the documents directory.
    String dataDir = Utils.getDataDir(ApacheHyperlinks.class);
    // or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    CreationHelper createHelper = wb.getCreationHelper();
    // cell style for hyperlinks
    // by default hyperlinks are blue and underlined
    CellStyle hlink_style = wb.createCellStyle();
    Font hlink_font = wb.createFont();
    hlink_font.setUnderline(Font.U_SINGLE);
    hlink_font.setColor(IndexedColors.BLUE.getIndex());
    hlink_style.setFont(hlink_font);
    Cell cell;
    Sheet sheet = wb.createSheet("Hyperlinks");
    // URL
    cell = sheet.createRow(0).createCell((short) 0);
    cell.setCellValue("URL Link");
    Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);
    link.setAddress("http://poi.apache.org/");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);
    // link to a file in the current directory
    cell = sheet.createRow(1).createCell((short) 0);
    cell.setCellValue("File Link");
    link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
    link.setAddress("link1.xls");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);
    // e-mail link
    cell = sheet.createRow(2).createCell((short) 0);
    cell.setCellValue("Email Link");
    link = createHelper.createHyperlink(Hyperlink.LINK_EMAIL);
    // note, if subject contains white spaces, make sure they are url-encoded
    link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
    cell.setHyperlink(link);
    cell.setCellStyle(hlink_style);
    // link to a place in this workbook
    // create a target sheet and cell
    Sheet sheet2 = wb.createSheet("Target Sheet");
    sheet2.createRow(0).createCell((short) 0).setCellValue("Target Cell");
    cell = sheet.createRow(3).createCell((short) 0);
    cell.setCellValue("Worksheet Link");
    Hyperlink link2 = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
    link2.setAddress("'Target Sheet'!A1");
    cell.setHyperlink(link2);
    cell.setCellStyle(hlink_style);
    FileOutputStream out = new FileOutputStream(dataDir + "ApacheHyperlinks.xlsx");
    wb.write(out);
    out.close();
    System.out.println("Done..");
}
Also used : CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Cell(org.apache.poi.ss.usermodel.Cell) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Font(org.apache.poi.ss.usermodel.Font) Hyperlink(org.apache.poi.ss.usermodel.Hyperlink)

Aggregations

CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)37 Sheet (org.apache.poi.ss.usermodel.Sheet)21 Cell (org.apache.poi.ss.usermodel.Cell)19 Workbook (org.apache.poi.ss.usermodel.Workbook)19 Row (org.apache.poi.ss.usermodel.Row)17 CellStyle (org.apache.poi.ss.usermodel.CellStyle)15 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)15 Font (org.apache.poi.ss.usermodel.Font)14 ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)12 FileOutputStream (java.io.FileOutputStream)10 RichTextString (org.apache.poi.ss.usermodel.RichTextString)10 Test (org.junit.Test)9 Comment (org.apache.poi.ss.usermodel.Comment)8 Hyperlink (org.apache.poi.ss.usermodel.Hyperlink)8 IOException (java.io.IOException)7 Drawing (org.apache.poi.ss.usermodel.Drawing)5 OutputStream (java.io.OutputStream)4 ArrayList (java.util.ArrayList)4 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)4 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)3