List of usage examples for org.apache.poi.ss.usermodel Cell setHyperlink
void setHyperlink(Hyperlink link);
From source file:utilities.XLSReportsManager.java
License:Open Source License
private void processDataListForXLS(ArrayList<ArrayList<KeyValue>> dArray, Sheet sheet, Sheet settingsSheet, Map<String, CellStyle> styles, ArrayList<Column> cols, String tz, ArrayList<KeyValue> settings) throws IOException { CreationHelper createHelper = wb.getCreationHelper(); for (int index = 0; index < dArray.size(); index++) { Row row = sheet.createRow(rowNumber++); ArrayList<KeyValue> record = dArray.get(index); for (Column col : cols) { Cell cell = row.createCell(col.colIndex); String value = "error"; if (col.dataIndex >= 0) { value = record.get(col.dataIndex).v; }/*from w ww. ja v a 2 s . c o m*/ cell.setCellStyle(styles.get("default")); if (value != null && (value.startsWith("https://") || value.startsWith("http://"))) { cell.setCellStyle(styles.get("link")); Hyperlink url = createHelper.createHyperlink(HyperlinkType.URL); url.setAddress(value); cell.setHyperlink(url); /* if(isXLSX) { XSSFHyperlink url = (XSSFHyperlink)createHelper.createHyperlink(HyperlinkType.URL); url.setAddress(value); cell.setHyperlink(url); } else { HSSFHyperlink url = new HSSFHyperlink(HyperlinkType.URL); url.setAddress(value); cell.setHyperlink(url); } */ } boolean cellWritten = false; if (col.type.equals("datetime")) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { java.util.Date date = dateFormat.parse(value); cell.setCellStyle(styles.get("datetime")); cell.setCellValue(date); cellWritten = true; } catch (Exception e) { // Ignore } } else if (col.type.equals("date")) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { java.util.Date date = dateFormat.parse(value); cell.setCellStyle(styles.get("date")); cell.setCellValue(date); cellWritten = true; } catch (Exception e) { // Ignore } } if (!cellWritten) { // Try to write as number by default try { double vDouble = Double.parseDouble(value); cell.setCellStyle(styles.get("default")); cell.setCellValue(vDouble); cellWritten = true; } catch (Exception e) { // Ignore } } if (!cellWritten) { cell.setCellStyle(styles.get("default")); cell.setCellValue(value); } } } // Populate settings sheet int settingsRowIdx = 0; Row settingsRow = settingsSheet.createRow(settingsRowIdx++); Cell k = settingsRow.createCell(0); Cell v = settingsRow.createCell(1); k.setCellStyle(styles.get("header")); k.setCellValue("Time Zone:"); v.setCellValue(tz); // Show filter settings settingsRowIdx++; settingsRow = settingsSheet.createRow(settingsRowIdx++); Cell f = settingsRow.createCell(0); f.setCellStyle(styles.get("header2")); f.setCellValue("Filters:"); if (settings != null) { for (KeyValue kv : settings) { settingsRow = settingsSheet.createRow(settingsRowIdx++); k = settingsRow.createCell(1); v = settingsRow.createCell(2); k.setCellStyle(styles.get("header")); k.setCellValue(kv.k); v.setCellValue(kv.v); } } }
From source file:utilities.XLSResultsManager.java
License:Open Source License
private void closeRecord(ArrayList<CellItem> record, Sheet sheet, Map<String, CellStyle> styles, boolean embedImages) throws IOException { CreationHelper createHelper = wb.getCreationHelper(); Row row = sheet.createRow(rowIndex++); if (embedImages) { row.setHeight((short) 1000); }/* w w w. ja va 2 s . co m*/ for (int i = 0; i < record.size(); i++) { CellItem ci = record.get(i); Cell cell = row.createCell(i); if (ci.v != null && (ci.v.startsWith("https://") || ci.v.startsWith("http://"))) { if (embedImages) { if (ci.v.endsWith(".jpg") || ci.v.endsWith(".png")) { int idx = ci.v.indexOf("attachments"); int idxName = ci.v.lastIndexOf('/'); if (idx > 0 && idxName > 0) { String fileName = ci.v.substring(idxName); String stem = basePath + "/" + ci.v.substring(idx, idxName); String imageName = stem + "/thumbs" + fileName + ".jpg"; try { InputStream inputStream = new FileInputStream(imageName); byte[] imageBytes = IOUtils.toByteArray(inputStream); int pictureureIdx = wb.addPicture(imageBytes, Workbook.PICTURE_TYPE_JPEG); inputStream.close(); ClientAnchor anchor = createHelper.createClientAnchor(); anchor.setCol1(i); anchor.setRow1(rowIndex - 1); anchor.setCol2(i + 1); anchor.setRow2(rowIndex); anchor.setAnchorType(ClientAnchor.MOVE_AND_RESIZE); //sheet.setColumnWidth(i, 20 * 256); Drawing drawing = sheet.createDrawingPatriarch(); Picture pict = drawing.createPicture(anchor, pictureureIdx); //pict.resize(); } catch (Exception e) { log.info("Error: Missing image file: " + imageName); } } } } cell.setCellStyle(styles.get("link")); if (isXLSX) { XSSFHyperlink url = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL); url.setAddress(ci.v); cell.setHyperlink(url); } else { HSSFHyperlink url = new HSSFHyperlink(HSSFHyperlink.LINK_URL); url.setAddress(ci.v); cell.setHyperlink(url); } cell.setCellValue(ci.v); } else { /* * Write the value as double or string */ boolean cellWritten = false; if (ci.type == CellItem.DECIMAL || ci.type == CellItem.INTEGER && ci.v != null) { try { double vDouble = Double.parseDouble(ci.v); cell.setCellStyle(styles.get("default")); cell.setCellValue(vDouble); cellWritten = true; } catch (Exception e) { // Ignore } } else if (ci.type == CellItem.DATETIME) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { java.util.Date date = dateFormat.parse(ci.v); cell.setCellStyle(styles.get("datetime")); cell.setCellValue(date); cellWritten = true; } catch (Exception e) { // Ignore } } else if (ci.type == CellItem.DATE) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { java.util.Date date = dateFormat.parse(ci.v); cell.setCellStyle(styles.get("date")); cell.setCellValue(date); cellWritten = true; } catch (Exception e) { // Ignore } } if (!cellWritten) { cell.setCellStyle(styles.get("default")); cell.setCellValue(ci.v); } } } }