List of usage examples for org.apache.poi.xssf.streaming SXSSFSheet getLastRowNum
@Override public int getLastRowNum()
From source file:com.plugin.excel.util.ExcelUtil.java
License:Apache License
/** * @param newSheet/*from ww w.java 2 s .c o m*/ * the sheet to create from the copy. * @param sheet * the sheet to copy. * @param copyStyle * true copy the style. */ public static void copySheets(SXSSFSheet newSheet, SXSSFSheet sheet, boolean copyStyle) { int maxColumnNum = 0; Map<Integer, CellStyle> styleMap = (copyStyle) ? new HashMap<Integer, CellStyle>() : null; for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) { Row srcRow = sheet.getRow(i); Row destRow = newSheet.createRow(i); if (srcRow != null) { ExcelUtil.copyRow(sheet, newSheet, srcRow, destRow, styleMap); if (srcRow.getLastCellNum() > maxColumnNum) { maxColumnNum = srcRow.getLastCellNum(); } } } for (int i = 0; i <= maxColumnNum; i++) { newSheet.setColumnWidth(i, sheet.getColumnWidth(i)); } }
From source file:tools.xor.service.AggregateManager.java
License:Apache License
private void createBOSheet(Workbook wb, String sheetName, String entityInfo, List<BusinessObject> boList, BusinessObject owner) {/*w w w . j av a2s . co m*/ int colNo = 0; int rowNo = 1; SXSSFSheet sh = (SXSSFSheet) wb.getSheet(sheetName); if (sh == null) { sh = (SXSSFSheet) wb.createSheet(sheetName); } else { rowNo = sh.getLastRowNum() + 1; } Map<String, Integer> propertyColIndex = new HashMap<String, Integer>(); for (BusinessObject bo : boList) { if (bo.getContainmentProperty() != null && bo.getContainmentProperty().isMany()) { createBOSheet(wb, sheetName, entityInfo, bo.getList(), (BusinessObject) bo.getContainer()); continue; } List<String> propertyPaths = new ArrayList<String>(); // Based on polymorphism, the actual instance can be a different subtype // so we need to get a fresh property list and calculate the column indexes // as new properties might be present and would need to be mapped to additional columns if (owner == null && bo.getContainer() != null) { owner = (BusinessObject) bo.getContainer(); } if (owner != null) { propertyPaths.add(Constants.XOR.OWNER_ID); } propertyPaths.add(Constants.XOR.ID); propertyPaths.add(Constants.XOR.TYPE); for (Property property : bo.getType().getProperties()) { if (property.isMany()) { propertyPaths.add(ExcelJsonCreationStrategy.getCollectionTypeKey(property)); // Collections are handled separately continue; } // Skip open content until we come with a default serialized form for empty object // Currently it fails validation since empty string does not equal JSONObject if (property.isOpenContent()) { continue; } // Handle embedded objects and expand them if necessary propertyPaths.addAll(property.expand()); } for (String propertyPath : propertyPaths) { if (!propertyColIndex.containsKey(propertyPath)) { propertyColIndex.put(propertyPath, colNo++); } } // TODO: add columns only if the value is not null Row row = sh.createRow(rowNo++); for (String propertyPath : propertyPaths) { Cell cell = row.createCell(propertyColIndex.get(propertyPath)); Object value = null; if (Constants.XOR.OWNER_ID.equals(propertyPath)) { value = owner.getOpenProperty(Constants.XOR.ID); } else if (Constants.XOR.ID.equals(propertyPath) || propertyPath.startsWith(Constants.XOR.TYPE + Constants.XOR.SEP)) { value = bo.getOpenProperty(propertyPath); } else if (Constants.XOR.TYPE.equals(propertyPath)) { value = bo.getInstanceClassName(); } else if (propertyPath.startsWith(Constants.XOR.OBJECTREF)) { String path = propertyPath.substring(Constants.XOR.OBJECTREF.length()); value = bo.getExistingDataObject(Settings.convertToBOPath(path)); if (value != null && value instanceof BusinessObject) { value = ((BusinessObject) value).getOpenProperty(Constants.XOR.ID); } else if (value != null) { throw new RuntimeException("ObjectRef needs to refer to an Entity: " + value.toString()); } } else { value = bo.get(Settings.convertToBOPath(propertyPath)); } if (value != null) { cell.setCellValue(value.toString()); } } } writeColumnNames(sh, propertyColIndex); }