Search in sources :

Example 1 with CellType

use of com.serotonin.m2m2.vo.emport.AbstractSheetEmporter.CellType in project ma-core-public by infiniteautomation.

the class SpreadsheetEmporter method doExport.

/**
 * Export machine states to the spreadsheet
 * Check for error messages afterwards by calling getErrorMessages()
 * @param monitorId set to null if dont care
 * @param machineId set to null if dont care
 */
private void doExport(AbstractSheetEmporter sheetEmporter) {
    Sheet sheet = wb.createSheet(sheetEmporter.getSheetName());
    rowNum = 0;
    int cellNum = 0;
    Cell cell;
    Row row;
    CreationHelper createHelper = wb.getCreationHelper();
    CellStyle dateStyle = wb.createCellStyle();
    dateStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd/mm/yy hh:mm:ss"));
    CellStyle percentStyle = wb.createCellStyle();
    percentStyle.setDataFormat(createHelper.createDataFormat().getFormat(DeltamationCommon.decimalFormat));
    // headers
    CellStyle headerStyle = wb.createCellStyle();
    Font headerFont = wb.createFont();
    headerFont.setBold(true);
    headerStyle.setFont(headerFont);
    row = sheet.createRow(rowNum++);
    int[] columnWidths = sheetEmporter.getColumnWidths();
    for (String text : sheetEmporter.getHeaders()) {
        cell = row.createCell(cellNum);
        cell.setCellValue(text);
        sheet.setColumnWidth(cellNum, columnWidths[cellNum]);
        cell.setCellStyle(headerStyle);
        cellNum++;
    }
    List<List<Object>> rows = sheetEmporter.exportRows();
    CellType[] columnTypes = sheetEmporter.getColumnTypes();
    for (List<Object> rowData : rows) {
        row = sheet.createRow(rowNum++);
        cellNum = 0;
        for (Object cellData : rowData) {
            cell = row.createCell(cellNum);
            if (cellData == null) {
                // empty cell
                cellNum++;
                continue;
            }
            // Switch On Type
            switch(columnTypes[cellNum]) {
                case STRING:
                    cell.setCellValue((String) cellData);
                    break;
                case NUMERIC:
                    if (cellData instanceof Integer) {
                        cell.setCellValue((Integer) cellData);
                    } else if (cellData instanceof Long) {
                        cell.setCellValue((Long) cellData);
                    }
                    break;
                case DATE:
                    cell.setCellValue((Date) cellData);
                    cell.setCellStyle(dateStyle);
                    break;
                case PERCENT:
                    cell.setCellValue((Double) cellData);
                    cell.setCellStyle(percentStyle);
                    break;
                default:
                    throw new RuntimeException("Unknown cell data type");
            }
            // Increment It
            cellNum++;
        }
        rowsProcessed++;
    }
}
Also used : CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) Font(org.apache.poi.ss.usermodel.Font) CellType(com.serotonin.m2m2.vo.emport.AbstractSheetEmporter.CellType) ArrayList(java.util.ArrayList) List(java.util.List) Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Aggregations

CellType (com.serotonin.m2m2.vo.emport.AbstractSheetEmporter.CellType)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Cell (org.apache.poi.ss.usermodel.Cell)1 CellStyle (org.apache.poi.ss.usermodel.CellStyle)1 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)1 Font (org.apache.poi.ss.usermodel.Font)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1