List of usage examples for org.apache.poi.ss.usermodel CreationHelper createRichTextString
RichTextString createRichTextString(String text);
From source file:org.betaconceptframework.astroboa.commons.excelbuilder.ExcelSheetBuilder.java
License:Open Source License
private void setPropertyCellStyleAndValue(String propertyPath, CmsPropertyDefinition propertyDefinition, Object value, HelperRow row, CreationHelper creationHelper, String locale) { appendPropertyPathList(propertyPath); appendHeaderPropertyList(propertyPath, propertyDefinition); switch (propertyDefinition.getValueType()) { case String: { row.addValueAndStyleForPath(propertyPath, creationHelper.createRichTextString(((String) value)), stringPropertyValueCellStyle); break;/*from w w w . jav a 2 s .co m*/ } case Date: { if (((CalendarPropertyDefinition) propertyDefinition).isDateTime()) { row.addValueAndStyleForPath(propertyPath, (Calendar) value, dateTimePropertyValueCellStyle); } else { row.addValueAndStyleForPath(propertyPath, (Calendar) value, datePropertyValueCellStyle); } break; } case TopicReference: { String cellValue = getTopicPath((Topic) value, locale); row.addValueAndStyleForPath(propertyPath, creationHelper.createRichTextString(cellValue), stringPropertyValueCellStyle); break; } case Double: { row.addValueAndStyleForPath(propertyPath, (Double) value, doublePropertyValueCellStyle); break; } case Long: { row.addValueAndStyleForPath(propertyPath, (Long) value, integerPropertyValueCellStyle); break; } case ObjectReference: { String cellValue = getContentObjectTitle((ContentObject) value, locale); row.addValueAndStyleForPath(propertyPath, creationHelper.createRichTextString(cellValue), stringPropertyValueCellStyle); break; } case Binary: { String cellValue = ((BinaryChannel) value).buildResourceApiURL(null, null, null, null, null, true, false); row.addValueAndStyleForPath(propertyPath, creationHelper.createRichTextString(cellValue), stringPropertyValueCellStyle); break; } default: break; } }
From source file:org.codelabor.example.poi.xssf.usermodel.XSSFWorkbookTest.java
License:Apache License
@Test public void testCreateCell() { String path = "C:/temp/workbook3.xlsx"; OutputStream outputStream = null; try {// w w w.j ava2 s . co m XSSFWorkbook workbook = new XSSFWorkbook(); Sheet sheet1 = workbook.createSheet("Sheet1"); CreationHelper creationHelper = workbook.getCreationHelper(); Row row = sheet1.createRow(0); Cell cell = row.createCell(0); cell.setCellValue(1); cell = row.createCell(1); cell.setCellValue(1.2); cell = row.createCell(2); cell.setCellValue(creationHelper.createRichTextString("This is a string")); cell = row.createCell(3); cell.setCellValue(true); outputStream = new FileOutputStream(path); workbook.write(outputStream); } catch (IOException e) { e.printStackTrace(); fail(); } finally { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:org.datanucleus.store.excel.fieldmanager.StoreFieldManager.java
License:Open Source License
public void storeStringField(int fieldNumber, String value) { if (!isStorable(fieldNumber)) { return;/*from ww w.ja v a 2 s . c o m*/ } Cell cell = row.getCell(getColumnMapping(fieldNumber).getColumn(0).getPosition(), Row.CREATE_NULL_AS_BLANK); if (value == null) { row.removeCell(cell); } else { CreationHelper createHelper = row.getSheet().getWorkbook().getCreationHelper(); cell.setCellValue(createHelper.createRichTextString(value)); } }
From source file:org.datanucleus.store.excel.fieldmanager.StoreFieldManager.java
License:Open Source License
protected void storeObjectFieldInternal(int fieldNumber, Object value, AbstractMemberMetaData mmd, ClassLoaderResolver clr, RelationType relationType) { MemberColumnMapping mapping = getColumnMapping(fieldNumber); if (relationType == RelationType.NONE) { if (mapping.getTypeConverter() != null) { // Persist using the provided converter TypeConverter conv = mapping.getTypeConverter(); Object datastoreValue = conv.toDatastoreType(value); Class datastoreType = TypeConverterHelper.getDatastoreTypeForTypeConverter(conv, mmd.getType()); if (mapping.getNumberOfColumns() == 1) { Cell cell = row.getCell(mapping.getColumn(0).getPosition(), Row.CREATE_NULL_AS_BLANK); if (value == null) { row.removeCell(cell); return; }/* www . ja v a 2 s . c om*/ boolean cellSet = setValueInCellForType(datastoreValue, datastoreType, cell, mapping.getColumn(0).getJdbcType()); if (!cellSet) { NucleusLogger.DATASTORE_PERSIST.warn("TypeConverter for member " + mmd.getFullFieldName() + " converts to " + datastoreType.getName() + " - not yet supported for storing in Excel cell"); } } else { if (value == null) { for (int i = 0; i < mapping.getNumberOfColumns(); i++) { Cell cell = row.getCell(mapping.getColumn(0).getPosition()); if (cell != null) { row.removeCell(cell); } } return; } Class[] colTypes = ((MultiColumnConverter) conv).getDatastoreColumnTypes(); for (int i = 0; i < mapping.getNumberOfColumns(); i++) { // Set each component cell Cell cell = row.getCell(mapping.getColumn(i).getPosition(), Row.CREATE_NULL_AS_BLANK); Object cellValue = Array.get(datastoreValue, i); Class cellValueType = colTypes[i]; if (cellValueType == int.class) { cellValueType = Integer.class; } if (cellValueType == long.class) { cellValueType = Long.class; } boolean cellSet = setValueInCellForType(cellValue, cellValueType, cell, mapping.getColumn(i).getJdbcType()); if (!cellSet) { NucleusLogger.DATASTORE_PERSIST.warn("TypeConverter for member " + mmd.getFullFieldName() + " converts to column " + i + " having value of type " + datastoreType.getName() + " - not yet supported for storing in Excel cell"); } } } return; } Cell cell = row.getCell(mapping.getColumn(0).getPosition(), Row.CREATE_NULL_AS_BLANK); if (value == null) { row.removeCell(cell); return; } boolean cellSet = setValueInCellForType(value, mmd.getType(), cell, mapping.getColumn(0).getJdbcType()); if (!cellSet) { // Try to persist using converters TypeManager typeMgr = ec.getNucleusContext().getTypeManager(); boolean useLong = MetaDataUtils.isJdbcTypeNumeric(mapping.getColumn(0).getJdbcType()); TypeConverter longConv = typeMgr.getTypeConverterForType(mmd.getType(), Long.class); if (useLong) { if (longConv != null) { cell.setCellValue((Long) longConv.toDatastoreType(value)); return; } } else { TypeConverter strConv = typeMgr.getTypeConverterForType(mmd.getType(), String.class); if (strConv != null) { CreationHelper createHelper = row.getSheet().getWorkbook().getCreationHelper(); cell.setCellValue( createHelper.createRichTextString((String) strConv.toDatastoreType(value))); return; } else if (longConv != null) { cell.setCellValue((Long) longConv.toDatastoreType(value)); return; } } NucleusLogger.PERSISTENCE.warn("DataNucleus doesnt currently support persistence of field " + mmd.getFullFieldName() + " type=" + value.getClass().getName() + " - ignoring"); } } else if (RelationType.isRelationSingleValued(relationType)) { if ((insert && !mmd.isCascadePersist()) || (!insert && !mmd.isCascadeUpdate())) { if (!ec.getApiAdapter().isDetached(value) && !ec.getApiAdapter().isPersistent(value)) { // Related PC object not persistent, but cant do cascade-persist so throw exception if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug(Localiser.msg("007006", mmd.getFullFieldName())); } throw new ReachableObjectNotCascadedException(mmd.getFullFieldName(), value); } } // Persistable object - persist the related object and store the identity in the cell Cell cell = row.getCell(mapping.getColumn(0).getPosition(), Row.CREATE_NULL_AS_BLANK); if (value == null) { row.removeCell(cell); return; } Object valuePC = ec.persistObjectInternal(value, op, fieldNumber, -1); Object valueId = ec.getApiAdapter().getIdForObject(valuePC); CreationHelper createHelper = row.getSheet().getWorkbook().getCreationHelper(); cell.setCellValue(createHelper .createRichTextString("[" + IdentityUtils.getPersistableIdentityForId(valueId) + "]")); return; } else if (RelationType.isRelationMultiValued(relationType)) { // Collection/Map/Array Cell cell = row.getCell(mapping.getColumn(0).getPosition(), Row.CREATE_NULL_AS_BLANK); if (value == null) { row.removeCell(cell); return; } if (mmd.hasCollection()) { Collection coll = (Collection) value; if ((insert && !mmd.isCascadePersist()) || (!insert && !mmd.isCascadeUpdate())) { // Field doesnt support cascade-persist so no reachability if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug(Localiser.msg("007006", mmd.getFullFieldName())); } // Check for any persistable elements that aren't persistent for (Object element : coll) { if (!ec.getApiAdapter().isDetached(element) && !ec.getApiAdapter().isPersistent(element)) { // Element is not persistent so throw exception throw new ReachableObjectNotCascadedException(mmd.getFullFieldName(), element); } } } StringBuilder cellValue = new StringBuilder("["); Iterator collIter = coll.iterator(); while (collIter.hasNext()) { Object element = collIter.next(); Object elementPC = ec.persistObjectInternal(element, op, fieldNumber, -1); Object elementID = ec.getApiAdapter().getIdForObject(elementPC); cellValue.append(IdentityUtils.getPersistableIdentityForId(elementID)); if (collIter.hasNext()) { cellValue.append(","); } } cellValue.append("]"); CreationHelper createHelper = row.getSheet().getWorkbook().getCreationHelper(); cell.setCellValue(createHelper.createRichTextString(cellValue.toString())); } else if (mmd.hasMap()) { AbstractClassMetaData keyCmd = mmd.getMap().getKeyClassMetaData(clr, ec.getMetaDataManager()); AbstractClassMetaData valCmd = mmd.getMap().getValueClassMetaData(clr, ec.getMetaDataManager()); StringBuilder cellValue = new StringBuilder("["); Map map = (Map) value; Iterator<Map.Entry> mapIter = map.entrySet().iterator(); while (mapIter.hasNext()) { Map.Entry entry = mapIter.next(); cellValue.append("["); if (keyCmd != null) { Object keyPC = ec.persistObjectInternal(entry.getKey(), op, fieldNumber, -1); Object keyID = ec.getApiAdapter().getIdForObject(keyPC); cellValue.append(IdentityUtils.getPersistableIdentityForId(keyID)); } else { cellValue.append(entry.getKey()); } cellValue.append("],["); if (valCmd != null) { Object valPC = ec.persistObjectInternal(entry.getValue(), op, fieldNumber, -1); Object valID = ec.getApiAdapter().getIdForObject(valPC); cellValue.append(IdentityUtils.getPersistableIdentityForId(valID)); } else { cellValue.append(entry.getValue()); } cellValue.append("]"); if (mapIter.hasNext()) { cellValue.append(","); } } cellValue.append("]"); CreationHelper createHelper = row.getSheet().getWorkbook().getCreationHelper(); cell.setCellValue(createHelper.createRichTextString(cellValue.toString())); } else if (mmd.hasArray()) { StringBuilder cellValue = new StringBuilder("["); for (int i = 0; i < Array.getLength(value); i++) { Object element = Array.get(value, i); Object elementPC = ec.persistObjectInternal(element, op, fieldNumber, -1); Object elementID = ec.getApiAdapter().getIdForObject(elementPC); cellValue.append(IdentityUtils.getPersistableIdentityForId(elementID)); if (i < (Array.getLength(value) - 1)) { cellValue.append(","); } } cellValue.append("]"); CreationHelper createHelper = row.getSheet().getWorkbook().getCreationHelper(); cell.setCellValue(createHelper.createRichTextString(cellValue.toString())); } return; } }
From source file:org.eclipse.emfforms.internal.spreadsheet.core.renderer.EMFFormsSpreadsheetControlRenderer.java
License:Open Source License
private Comment createComment(Workbook workbook, Sheet sheet, VDomainModelReference domainModelReference, int row, int column) throws IOException { final CreationHelper factory = workbook.getCreationHelper(); // When the comment box is visible, have it show in a 1x3 space final ClientAnchor anchor = factory.createClientAnchor(); anchor.setCol1(column);/* w ww. j a va 2 s.co m*/ anchor.setCol2(column + 1); anchor.setRow1(row); anchor.setRow2(row + 1); final Drawing drawing = sheet.createDrawingPatriarch(); final Comment comment = drawing.createCellComment(anchor); comment.setAuthor("EMFForms Spreadsheet Renderer"); //$NON-NLS-1$ comment.setVisible(false); comment.setString(factory.createRichTextString(getSerializedDMR(domainModelReference))); return comment; }
From source file:org.eclipse.emfforms.spreadsheet.integrationtest.ImportErrors_ITest.java
License:Open Source License
private Comment createComment(Workbook workbook, Sheet sheet, int row, int column) throws IOException { final CreationHelper factory = workbook.getCreationHelper(); // When the comment box is visible, have it show in a 1x3 space final ClientAnchor anchor = factory.createClientAnchor(); anchor.setCol1(column);//from www.j a v a 2 s .com anchor.setCol2(column + 1); anchor.setRow1(row); anchor.setRow2(row + 1); final Drawing drawing = sheet.createDrawingPatriarch(); final Comment comment = drawing.createCellComment(anchor); comment.setAuthor("EMFForms Spreadsheet Renderer"); //$NON-NLS-1$ comment.setVisible(false); comment.setString(factory.createRichTextString("Ignore Sheet")); //$NON-NLS-1$ return comment; }
From source file:org.eclipse.emfforms.spreadsheet.integrationtest.ImportErrors_ITest.java
License:Open Source License
@Test public void testNoObjectIdColumn() throws IOException { /* setup */// w w w . ja va2 s.c o m final Workbook workbook = new HSSFWorkbook(); final Sheet sheet = workbook.createSheet("root"); //$NON-NLS-1$ final Row rowLabel = sheet.createRow(0); rowLabel.createCell(0).setCellValue("My feature"); //$NON-NLS-1$ final CreationHelper factory = workbook.getCreationHelper(); // When the comment box is visible, have it show in a 1x3 space final ClientAnchor anchor = factory.createClientAnchor(); anchor.setCol1(0); anchor.setCol2(1); anchor.setRow1(0); anchor.setRow2(1); final Drawing drawing = sheet.createDrawingPatriarch(); final Comment comment = drawing.createCellComment(anchor); comment.setString(factory.createRichTextString( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><org.eclipse.emf.ecp.view.model:FeaturePathDomainModelReference xmi:version=\"2.0\" xmlns:xmi=\"http://www.omg.org/XMI\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ecore=\"http://www.eclipse.org/emf/2002/Ecore\" xmlns:org.eclipse.emf.ecp.view.model=\"http://org/eclipse/emf/ecp/view/model/170\"><domainModelEFeature xsi:type=\"ecore:EAttribute\" href=\"http://eclipse/org/emf/ecp/makeithappen/model/task#//User/lastName\"/></org.eclipse.emf.ecp.view.model:FeaturePathDomainModelReference>")); //$NON-NLS-1$ final Row rowDescription = sheet.createRow(1); rowDescription.createCell(0).setCellValue("My feature description"); //$NON-NLS-1$ final Row rowMeta = sheet.createRow(2); rowMeta.createCell(0).setCellValue("Enter Numbers"); //$NON-NLS-1$ final Row rowData = sheet.createRow(3); rowData.createCell(0).setCellValue("My Feature Value"); //$NON-NLS-1$ /* act */ final SpreadsheetImportResult result = EMFFormsSpreadsheetImporter.INSTANCE.importSpreadsheet(workbook, eClass); assertEquals(1, result.getErrorReports().size()); }
From source file:org.eclipse.emfforms.spreadsheet.integrationtest.ImportErrors_ITest.java
License:Open Source License
@Test public void testDMRThatNeedsMigration() throws IOException { /* setup *///from www. ja v a 2 s . c o m stream = bundle.getEntry("errorSheets/basexls").openStream(); //$NON-NLS-1$ final Workbook workbook = new HSSFWorkbook(stream); final CreationHelper factory = workbook.getCreationHelper(); final Sheet sheet = workbook.getSheetAt(0); final RichTextString dmr = factory.createRichTextString( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><org.eclipse.emf.ecp.view.model:FeaturePathDomainModelReference xmi:version=\"2.0\" xmlns:xmi=\"http://www.omg.org/XMI\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ecore=\"http://www.eclipse.org/emf/2002/Ecore\" xmlns:org.eclipse.emf.ecp.view.model=\"http://org/eclipse/emf/ecp/view/model\"><domainModelEFeature xsi:type=\"ecore:EAttribute\" href=\"http://eclipse/org/emf/ecp/makeithappen/model/task#//User/lastName\"/></org.eclipse.emf.ecp.view.model:FeaturePathDomainModelReference>"); //$NON-NLS-1$ sheet.getRow(0).getCell(1).getCellComment().setString(dmr); /* act */ final SpreadsheetImportResult result = EMFFormsSpreadsheetImporter.INSTANCE.importSpreadsheet(workbook, eClass); final EList<ErrorReport> errorReports = result.getErrorReports(); /* assert */ assertEquals(0, errorReports.size()); }
From source file:org.exist.xquery.corenlp.Tokenize.java
License:Open Source License
private void createXSLXSpreadsheet(List<List<CoreLabel>> sentences, List<CoreLabel> tokens) { Workbook workbook = null;//w ww . j a v a2 s . co m if (outputFormat == OutDocType.XSLX) { workbook = new SXSSFWorkbook(); } else { workbook = new HSSFWorkbook(); } CreationHelper creationHelper = workbook.getCreationHelper(); org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet(); Font boldFont = workbook.createFont(); boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD); // Header CellStyle headerStyle = workbook.createCellStyle(); headerStyle.setFont(boldFont); int lineIndex = 0; for (List<CoreLabel> sentence : sentences) { for (CoreLabel token : sentence) { String value = token.get(CoreAnnotations.OriginalTextAnnotation.class); Row row = sheet.createRow(lineIndex); row.createCell(0).setCellValue(creationHelper.createRichTextString(value)); row.createCell(1).setCellValue(creationHelper.createRichTextString(backgroundSymbol)); lineIndex++; } Row row = sheet.createRow(lineIndex); row.createCell(0).setCellValue(creationHelper.createRichTextString("")); row.createCell(1).setCellValue(creationHelper.createRichTextString("")); lineIndex++; } try (OutputStream os = Files.newOutputStream(tempOutFile)) { workbook.write(os); } catch (FileNotFoundException fe) { LOG.error(fe); } catch (IOException ioe) { LOG.error(ioe); } finally { if (workbook != null) { if (workbook instanceof SXSSFWorkbook) { ((SXSSFWorkbook) workbook).dispose(); } else { workbook = null; } } } }
From source file:org.geoserver.wfs.response.ExcelOutputFormat.java
License:Open Source License
/** * @see WFSGetFeatureOutputFormat#write(Object, OutputStream, Operation) *///from w w w . j a v a2s. c o m @Override protected void write(FeatureCollectionResponse featureCollection, OutputStream output, Operation getFeature) throws IOException, ServiceException { // Create the workbook Workbook wb = getNewWorkbook(); CreationHelper helper = wb.getCreationHelper(); ExcelCellStyles styles = new ExcelCellStyles(wb); for (Iterator it = featureCollection.getFeature().iterator(); it.hasNext();) { SimpleFeatureCollection fc = (SimpleFeatureCollection) it.next(); // create the sheet for this feature collection Sheet sheet = wb.createSheet(fc.getSchema().getTypeName()); // write out the header Row header = sheet.createRow(0); SimpleFeatureType ft = fc.getSchema(); Cell cell; cell = header.createCell(0); cell.setCellValue(helper.createRichTextString("FID")); for (int i = 0; i < ft.getAttributeCount() && i < colLimit; i++) { AttributeDescriptor ad = ft.getDescriptor(i); cell = header.createCell(i + 1); cell.setCellValue(helper.createRichTextString(ad.getLocalName())); cell.setCellStyle(styles.getHeaderStyle()); } // write out the features SimpleFeatureIterator i = fc.features(); int r = 0; // row index try { Row row; while (i.hasNext()) { r++; // start at 1, since header is at 0 row = sheet.createRow(r); cell = row.createCell(0); if (r == (rowLimit - 1) && i.hasNext()) { // there are more features than rows available in this // Excel format. write out a warning line and break RichTextString rowWarning = helper.createRichTextString( TRUNCATE_WARNING + ": ROWS " + r + " - " + fc.size() + " NOT SHOWN"); cell.setCellValue(rowWarning); cell.setCellStyle(styles.getWarningStyle()); break; } SimpleFeature f = i.next(); cell.setCellValue(helper.createRichTextString(f.getID())); for (int j = 0; j < f.getAttributeCount() && j < colLimit; j++) { Object att = f.getAttribute(j); if (att != null) { cell = row.createCell(j + 1); if (att instanceof Number) { cell.setCellValue(((Number) att).doubleValue()); } else if (att instanceof Date) { cell.setCellValue((Date) att); cell.setCellStyle(styles.getDateStyle()); } else if (att instanceof Calendar) { cell.setCellValue((Calendar) att); cell.setCellStyle(styles.getDateStyle()); } else if (att instanceof Boolean) { cell.setCellValue((Boolean) att); } else { // ok, it seems we have no better way than dump it as a string String stringVal = att.toString(); // if string length > excel cell limit, truncate it and warn the // user, otherwise excel workbook will be corrupted if (stringVal.length() > CELL_CHAR_LIMIT) { stringVal = TRUNCATE_WARNING + " " + stringVal.substring(0, CELL_CHAR_LIMIT - TRUNCATE_WARNING.length() - 1); cell.setCellStyle(styles.getWarningStyle()); } cell.setCellValue(helper.createRichTextString(stringVal)); } } } } } finally { i.close(); } } // write to output wb.write(output); }