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++;
}
}
Aggregations