Search in sources :

Example 1 with XSSFCreationHelper

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);
                        }
                    }
                }
            }
        }
    }
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFCreationHelper(org.apache.poi.xssf.usermodel.XSSFCreationHelper) CollectionNode(org.hisp.dhis.node.types.CollectionNode) Node(org.hisp.dhis.node.Node) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) RootNode(org.hisp.dhis.node.types.RootNode) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) SimpleNode(org.hisp.dhis.node.types.SimpleNode) XSSFHyperlink(org.apache.poi.xssf.usermodel.XSSFHyperlink)

Example 2 with XSSFCreationHelper

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");
    }
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) SimpleExportXmEntityDto(com.icthh.xm.ms.entity.domain.SimpleExportXmEntityDto) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFCreationHelper(org.apache.poi.xssf.usermodel.XSSFCreationHelper) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) Row(org.apache.poi.ss.usermodel.Row) IOException(java.io.IOException) Cell(org.apache.poi.ss.usermodel.Cell)

Aggregations

XSSFCreationHelper (org.apache.poi.xssf.usermodel.XSSFCreationHelper)2 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)2 SimpleExportXmEntityDto (com.icthh.xm.ms.entity.domain.SimpleExportXmEntityDto)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Cell (org.apache.poi.ss.usermodel.Cell)1 Row (org.apache.poi.ss.usermodel.Row)1 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)1 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)1 XSSFHyperlink (org.apache.poi.xssf.usermodel.XSSFHyperlink)1 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1 Node (org.hisp.dhis.node.Node)1 CollectionNode (org.hisp.dhis.node.types.CollectionNode)1 ComplexNode (org.hisp.dhis.node.types.ComplexNode)1 RootNode (org.hisp.dhis.node.types.RootNode)1 SimpleNode (org.hisp.dhis.node.types.SimpleNode)1