use of org.apache.poi.xssf.usermodel.XSSFCreationHelper in project dhis2-core by dhis2.
the class ExcelNodeSerializer method startWriteRootNode.
@Override
protected void startWriteRootNode(RootNode rootNode) throws Exception {
XSSFCreationHelper creationHelper = workbook.getCreationHelper();
int rowIdx = 1;
for (Node collectionNode : rootNode.getChildren()) {
if (collectionNode.isCollection()) {
for (Node complexNode : collectionNode.getChildren()) {
XSSFRow row = sheet.createRow(rowIdx++);
int cellIdx = 0;
for (Node node : complexNode.getChildren()) {
if (node.isSimple()) {
XSSFCell cell = row.createCell(cellIdx++);
cell.setCellValue(getValue((SimpleNode) node));
if (node.haveProperty() && PropertyType.URL.equals(node.getProperty().getPropertyType())) {
XSSFHyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
hyperlink.setAddress(getValue((SimpleNode) node));
hyperlink.setLabel(getValue((SimpleNode) node));
cell.setHyperlink(hyperlink);
} else if (node.haveProperty() && PropertyType.EMAIL.equals(node.getProperty().getPropertyType())) {
XSSFHyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.EMAIL);
hyperlink.setAddress(getValue((SimpleNode) node));
hyperlink.setLabel(getValue((SimpleNode) node));
cell.setHyperlink(hyperlink);
}
}
}
}
}
}
}
use of org.apache.poi.xssf.usermodel.XSSFCreationHelper in project xm-ms-entity by xm-online.
the class EntityToExcelConverterUtils method toExcel.
/**
* Writes entities to excel file.
* @param entities the entities list
* @return byte array of excel file
*/
public static byte[] toExcel(List<SimpleExportXmEntityDto> entities, String sheetName) {
if (CollectionUtils.isEmpty(entities)) {
log.warn("Passed empty object for serialize, therefore return empty byte array which represents excel file");
return new byte[0];
}
try (XSSFWorkbook workbook = new XSSFWorkbook();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
XSSFSheet sheet = workbook.createSheet(sheetName);
XSSFCreationHelper creationHelper = workbook.getCreationHelper();
XSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat(DateFormatConverter.getJavaDateTimePattern(0, Locale.US)));
int rowCount = 0;
XSSFRow headerRow = sheet.createRow(rowCount);
IntStream.range(0, headers.length).forEach(i -> headerRow.createCell(i).setCellValue(headers[i]));
for (SimpleExportXmEntityDto entity : entities) {
Row row = sheet.createRow(++rowCount);
int columnCount = 0;
Cell cell = row.createCell(columnCount);
cell.setCellValue(entity.getOrElseId(0L));
cell = row.createCell(++columnCount);
cell.setCellValue(entity.getOrElseKey(StringUtils.EMPTY));
cell = row.createCell(++columnCount);
cell.setCellValue(entity.getOrElseTypeKey(StringUtils.EMPTY));
cell = row.createCell(++columnCount);
cell.setCellValue(entity.getOrElseStateKey(StringUtils.EMPTY));
cell = row.createCell(++columnCount);
cell.setCellValue(entity.getOrElseName(StringUtils.EMPTY));
cell = row.createCell(++columnCount);
cell.setCellValue(Date.from(entity.getOrElseStartDate(Instant.now())));
cell.setCellStyle(cellStyle);
cell = row.createCell(++columnCount);
cell.setCellValue(Date.from(entity.getOrElseStartDate(Instant.now())));
cell.setCellStyle(cellStyle);
cell = row.createCell(++columnCount);
cell.setCellValue(Date.from(entity.getOrElseEndDate(Instant.now())));
cell.setCellStyle(cellStyle);
cell = row.createCell(++columnCount);
cell.setCellValue(entity.getOrElseAvatarUrl(StringUtils.EMPTY));
cell = row.createCell(++columnCount);
cell.setCellValue(entity.getOrElseDescription(StringUtils.EMPTY));
cell = row.createCell(++columnCount);
cell.setCellValue(entity.isOrElseRemoved(false));
cell = row.createCell(++columnCount);
cell.setCellValue(entity.getOrElseCreatedBy(StringUtils.EMPTY));
}
workbook.write(outputStream);
return outputStream.toByteArray();
} catch (IOException e) {
throw new IllegalStateException("Exception while writing data to excel file");
}
}
Aggregations