List of usage examples for org.apache.poi.ss.usermodel Row createCell
Cell createCell(int column, CellType type);
From source file:com.blackducksoftware.tools.commonframework.standard.protex.report.template.TemplateWriter.java
License:Apache License
/** * Write pojo values to row for the provided sheet. * // w ww . j a va 2 s. co m * @param activeSheet * the provided sheet for pojo values write out to the rows * @param activeRow * the active row * @param pojo * the pojo * @param columnMap * the column map * @param cloneStyle * - if true, apply the styles to the new cells */ private void writePojoValuesToRow(Sheet activeSheet, Row activeRow, TemplatePojo pojo, Map<String, TemplateColumn> columnMap, boolean cloneStyle) { Iterator<String> it = columnMap.keySet().iterator(); while (it.hasNext()) { String key = it.next(); TemplateColumn column = columnMap.get(key); Integer position = column.getColumnPos(); CellStyle styleFromTemplate = column.getCellStyle(); Cell activeCell; int cellType = column.getCellType(); if (cellType == Cell.CELL_TYPE_FORMULA) { activeCell = activeRow.createCell(position, Cell.CELL_TYPE_FORMULA); log.debug("Active Cell is PartOfArrayFormulaGroup: " + activeCell.isPartOfArrayFormulaGroup()); } else if (cellType == Cell.CELL_TYPE_NUMERIC) { activeCell = activeRow.createCell(position, Cell.CELL_TYPE_NUMERIC); } else { activeCell = activeRow.createCell(position, Cell.CELL_TYPE_STRING); } // Set the value String pojoValue = getValueFromPojo(pojo, column.getLookupMappingName()); activeCell.setCellValue(pojoValue); // Set the cell style // TODO: This catches the XML Disconnected exception, but the styles come out all wrong on subsequent // sheets. // Appears to only happen in the unit tests. if (cloneStyle) { try { CellStyle newcs = book.createCellStyle(); newcs.cloneStyleFrom(styleFromTemplate); activeCell.setCellStyle(newcs); } catch (Exception e) { log.warn("Unable to copy cell styles!" + e.getMessage()); } } if (cellType == Cell.CELL_TYPE_FORMULA) { copyFormula(activeSheet, activeCell, activeRow, column); } } }
From source file:com.github.crab2died.ExcelUtils.java
License:Open Source License
private void generateSheet(Workbook workbook, List<?> data, List<String> header, String sheetName) { Sheet sheet;//from w w w .ja v a 2 s. co m if (null != sheetName && !"".equals(sheetName)) { sheet = workbook.createSheet(sheetName); } else { sheet = workbook.createSheet(); } int rowIndex = 0; if (null != header && header.size() > 0) { // Row row = sheet.createRow(rowIndex++); for (int i = 0; i < header.size(); i++) { row.createCell(i, CellType.STRING).setCellValue(header.get(i)); } } for (Object object : data) { Row row = sheet.createRow(rowIndex++); if (object.getClass().isArray()) { for (int j = 0; j < Array.getLength(object); j++) { row.createCell(j, CellType.STRING).setCellValue(Array.get(object, j).toString()); } } else if (object instanceof Collection) { Collection<?> items = (Collection<?>) object; int j = 0; for (Object item : items) { row.createCell(j++, CellType.STRING).setCellValue(item.toString()); } } else { row.createCell(0, CellType.STRING).setCellValue(object.toString()); } } }
From source file:com.github.pascalgn.jiracli.testutil.ExcelUtils.java
License:Apache License
/** * @param row 0-based index// w w w . j a v a 2 s .c om * @param column 0-based index */ public static void writeCell(Sheet sheet, int row, int column, String value) { Row r = sheet.getRow(row); if (r == null) { r = sheet.createRow(row); } Cell cell = r.getCell(column); if (cell == null) { cell = r.createCell(column, Cell.CELL_TYPE_STRING); } cell.setCellValue(value); }
From source file:com.ipcglobal.fredimport.xls.DistinctCategoriesSpreadsheet.java
License:Apache License
/** * Creates the sheet.//from w ww .java2s .co m * * @param distinctCategoryItems the distinct category items * @throws Exception the exception */ public void createSheet(Collection<DistinctCategoryItem> distinctCategoryItems) throws Exception { List<XlsDefItem> xlsDefItems = initHhdrWidthItems(); String sheetName = "DistinctCategoryItems"; Sheet sheet = wb.createSheet(sheetName); processColumnWidths(sheet, xlsDefItems); sheet.createFreezePane(0, 1, 0, 1); // freeze top row sheet.setAutoFilter(CellRangeAddress.valueOf("A1:AB1")); // hack - i know the number of columns sheet.getPrintSetup().setLandscape(true); sheet.setAutobreaks(true); sheet.getPrintSetup().setFitWidth((short) 1); sheet.getPrintSetup().setFitHeight((short) 1); int rowCnt = 0; // Header int colCnt = 0; Row rowHdr = sheet.createRow(rowCnt); for (XlsDefItem xlsDefItem : xlsDefItems) { Cell cellHdr = rowHdr.createCell(colCnt, Cell.CELL_TYPE_STRING); CellStyle style = findCellStyle("Arial", HSSFColor.WHITE.index, (short) 11, XSSFFont.BOLDWEIGHT_BOLD, cellStyleFromHdrAlign(HdrAlign.Left), XSSFCellStyle.VERTICAL_TOP, HSSFColor.LIGHT_BLUE.index, CellBorder.All_Thin, formatGeneral); style.setWrapText(true); cellHdr.setCellStyle(style); cellHdr.setCellValue(xlsDefItem.getName()); colCnt++; } rowCnt++; // Data for (DistinctCategoryItem distinctCategoryItem : distinctCategoryItems) { Row rowData = sheet.createRow(rowCnt); int colNum = 0; for (XlsDefItem xlsDefItem : xlsDefItems) populateCell(rowData, colNum++, xlsDefItem.getDataType(), getByNameAsString(distinctCategoryItem, xlsDefItem.getName())); rowCnt++; } }
From source file:com.ipcglobal.fredimport.xls.DistinctCategoriesSpreadsheet.java
License:Apache License
/** * Populate cell.//from w w w. j av a 2 s.com * * @param rowData the row data * @param colCnt the col cnt * @param dataType the data type * @param obj the obj * @throws Exception the exception */ private void populateCell(Row rowData, int colCnt, DataType dataType, Object obj) throws Exception { int cellType = 0; if (dataType == DataType.Numeric) cellType = XSSFCell.CELL_TYPE_NUMERIC; else if (dataType == DataType.NumericDec2) cellType = XSSFCell.CELL_TYPE_NUMERIC; else if (dataType == DataType.Text) cellType = XSSFCell.CELL_TYPE_STRING; else if (dataType == DataType.Date) cellType = XSSFCell.CELL_TYPE_STRING; else if (dataType == DataType.Accounting) cellType = XSSFCell.CELL_TYPE_NUMERIC; else if (dataType == DataType.Percent) cellType = XSSFCell.CELL_TYPE_NUMERIC; Cell cellData = rowData.createCell(colCnt, cellType); short findFormat = -1; if (dataType == DataType.Date) findFormat = formatMmDdYyyy; else if (dataType == DataType.Percent) findFormat = formatPercent; else if (dataType == DataType.Accounting) findFormat = formatAccounting; else if (dataType == DataType.Numeric) findFormat = formatNumeric; else if (dataType == DataType.NumericDec2) findFormat = formatNumericDec2; else findFormat = formatGeneral; CellStyle style = findCellStyle("Arial", HSSFColor.BLACK.index, (short) 11, XSSFFont.BOLDWEIGHT_NORMAL, cellStyleFromDataAlign(findAlignByDataType(dataType)), XSSFCellStyle.VERTICAL_TOP, BG_COLOR_NONE, CellBorder.All_Thin, findFormat); cellData.setCellStyle(style); if (dataType == DataType.Numeric || dataType == DataType.NumericDec2 || dataType == DataType.Accounting || dataType == DataType.Percent) { if (obj == null) ; // leave the cell empty else if (obj instanceof BigDecimal) { BigDecimal value = (BigDecimal) obj; if (value != null) cellData.setCellValue(value.doubleValue()); } else if (obj instanceof Integer) { Integer value = (Integer) obj; if (value != null) cellData.setCellValue(value.intValue()); } else if (obj instanceof Long) { Long value = (Long) obj; if (value != null) cellData.setCellValue(value.longValue()); } else if (obj instanceof Double) { Double value = (Double) obj; if (value != null) cellData.setCellValue(value.doubleValue()); } else if (obj instanceof Short) { Short value = (Short) obj; if (value != null) cellData.setCellValue(value.shortValue()); } else if (obj instanceof String) { String value = (String) obj; if (value != null) cellData.setCellValue(value); } else throw new Exception("Unsupported numeric type: " + obj.getClass().getSimpleName()); } else if (dataType == DataType.Date) { Date date = (Date) obj; if (date != null) cellData.setCellValue(date); } else { cellData.setCellValue((String) obj); } }
From source file:com.jeans.iservlet.action.asset.AssetExportAction.java
private void appendRow(Sheet sheet, Asset asset, int rowNumber) { // /*from w w w . jav a 2 s . co m*/ DataFormat df = sheet.getWorkbook().createDataFormat(); // ?10? Font font = sheet.getWorkbook().createFont(); font.setFontName(""); font.setFontHeightInPoints((short) 10); // ?1??????? CellStyle cellStyleString = sheet.getWorkbook().createCellStyle(); cellStyleString.setAlignment(CellStyle.ALIGN_CENTER); cellStyleString.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyleString.setFont(font); cellStyleString.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); cellStyleString.setWrapText(false); // ?2????(yyyyMM)??? CellStyle cellStyleDate = sheet.getWorkbook().createCellStyle(); cellStyleDate.setAlignment(CellStyle.ALIGN_CENTER); cellStyleDate.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyleDate.setFont(font); cellStyleDate.setDataFormat(df.getFormat("yyyyMM")); cellStyleDate.setWrapText(false); // ?3??????(#)??? CellStyle cellStyleQuantity = sheet.getWorkbook().createCellStyle(); cellStyleQuantity.setAlignment(CellStyle.ALIGN_RIGHT); cellStyleQuantity.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyleQuantity.setFont(font); cellStyleQuantity.setDataFormat(df.getFormat("#")); cellStyleQuantity.setWrapText(false); // ?4?????(#,##0.00_ )??? CellStyle cellStyleCost = sheet.getWorkbook().createCellStyle(); cellStyleCost.setAlignment(CellStyle.ALIGN_RIGHT); cellStyleCost.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyleCost.setFont(font); cellStyleCost.setDataFormat(df.getFormat("#,##0.00_ ")); cellStyleCost.setWrapText(false); // 20 Row row = sheet.createRow(rowNumber); row.setHeightInPoints(20); Cell cell = null; if (asset instanceof Hardware) { cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(((Hardware) asset).getCode()); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(((Hardware) asset).getFinancialCode()); cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); Company company = asset.getCompany(); if (null != company) { cell.setCellValue(company.getAlias()); } cell = row.createCell(3, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(AssetConstants.getAssetCatalogName(asset.getCatalog())); cell = row.createCell(4, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getName()); cell = row.createCell(5, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getVendor()); cell = row.createCell(6, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getModelOrVersion()); cell = row.createCell(7, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getAssetUsage()); cell = row.createCell(8, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(((Hardware) asset).getSn()); cell = row.createCell(9, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(((Hardware) asset).getConfiguration()); cell = row.createCell(10, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleDate); Date pt = asset.getPurchaseTime(); if (null != pt) { cell.setCellValue(pt); } cell = row.createCell(11, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleQuantity); cell.setCellValue(asset.getQuantity()); cell = row.createCell(12, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleCost); cell.setCellValue(asset.getCost().doubleValue()); cell = row.createCell(13, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(AssetConstants.getAssetStateName(asset.getState())); cell = row.createCell(14, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(AssetConstants.getHardwareWarrantyName(((Hardware) asset).getWarranty())); cell = row.createCell(15, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(((Hardware) asset).getLocation()); cell = row.createCell(16, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(((Hardware) asset).getIp()); cell = row.createCell(17, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(AssetConstants.getHardwareImportanceName(((Hardware) asset).getImportance())); cell = row.createCell(18, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); Employee owner = ((Hardware) asset).getOwner(); if (null != owner) { cell.setCellValue(owner.getName()); } cell = row.createCell(19, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getComment()); } else if (asset instanceof Software) { cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); Company company = asset.getCompany(); if (null != company) { cell.setCellValue(company.getAlias()); } cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(AssetConstants.getAssetCatalogName(asset.getCatalog())); cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getName()); cell = row.createCell(3, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getVendor()); cell = row.createCell(4, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getModelOrVersion()); cell = row.createCell(5, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getAssetUsage()); cell = row.createCell(6, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleDate); Date pt = asset.getPurchaseTime(); if (null != pt) { cell.setCellValue(pt); } cell = row.createCell(7, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleQuantity); cell.setCellValue(asset.getQuantity()); cell = row.createCell(8, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleCost); cell.setCellValue(asset.getCost().doubleValue()); cell = row.createCell(9, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(AssetConstants.getAssetStateName(asset.getState())); cell = row.createCell(10, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(AssetConstants.getSoftwareTypeName(((Software) asset).getSoftwareType())); cell = row.createCell(11, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(((Software) asset).getLicense()); cell = row.createCell(12, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleDate); Date et = ((Software) asset).getExpiredTime(); if (null != et) { cell.setCellValue(et); } cell = row.createCell(13, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getComment()); } }
From source file:com.jeans.iservlet.action.asset.AssetExportAction.java
private void generateSheetHeader(Sheet sheet, boolean hardware) { // /*from ww w. ja v a 2s. co m*/ // ?10?? Font font = sheet.getWorkbook().createFont(); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontName(""); font.setFontHeightInPoints((short) 10); // ????????? CellStyle cellStyle = sheet.getWorkbook().createCellStyle(); cellStyle.setAlignment(CellStyle.ALIGN_CENTER); cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyle.setFont(font); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); cellStyle.setWrapText(false); // 20 Row row = sheet.createRow(0); row.setHeightInPoints(20); Cell cell = null; if (hardware) { for (int i = 0; i < 20; i++) { cell = row.createCell(i, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyle); cell.setCellValue(HARDWARE_HEADERS[i]); sheet.setColumnWidth(i, HARDWARE_HEADERS_WIDTH[i] * 256); } } else { for (int i = 0; i < 14; i++) { cell = row.createCell(i, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyle); cell.setCellValue(SOFTWARE_HEADERS[i]); sheet.setColumnWidth(i, SOFTWARE_HEADERS_WIDTH[i] * 256); } } }
From source file:com.jeans.iservlet.controller.impl.ExportController.java
private void appendRow(Sheet sheet, Asset asset, int rowNumber, CellStyle cellStyleString, CellStyle cellStyleDate, CellStyle cellStyleQuantity, CellStyle cellStyleCost) { // /*from w w w . jav a 2 s .c o m*/ // 20 Row row = sheet.createRow(rowNumber); row.setHeightInPoints(20); Cell cell = null; if (asset instanceof Hardware) { cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(((Hardware) asset).getCode()); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(((Hardware) asset).getFinancialCode()); cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); Company company = asset.getCompany(); if (null != company) { cell.setCellValue(company.getAlias()); } cell = row.createCell(3, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(AssetConstants.getAssetCatalogName(asset.getCatalog())); cell = row.createCell(4, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getName()); cell = row.createCell(5, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getVendor()); cell = row.createCell(6, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getModelOrVersion()); cell = row.createCell(7, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getAssetUsage()); cell = row.createCell(8, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(((Hardware) asset).getSn()); cell = row.createCell(9, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(((Hardware) asset).getConfiguration()); cell = row.createCell(10, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleDate); Date pt = asset.getPurchaseTime(); if (null != pt) { cell.setCellValue(pt); } cell = row.createCell(11, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleQuantity); cell.setCellValue(asset.getQuantity()); cell = row.createCell(12, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleCost); cell.setCellValue(asset.getCost().doubleValue()); cell = row.createCell(13, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(AssetConstants.getAssetStateName(asset.getState())); cell = row.createCell(14, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(AssetConstants.getHardwareWarrantyName(((Hardware) asset).getWarranty())); cell = row.createCell(15, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(((Hardware) asset).getLocation()); cell = row.createCell(16, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(((Hardware) asset).getIp()); cell = row.createCell(17, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(AssetConstants.getHardwareImportanceName(((Hardware) asset).getImportance())); cell = row.createCell(18, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); Employee owner = ((Hardware) asset).getOwner(); if (null != owner) { cell.setCellValue(owner.getName()); } cell = row.createCell(19, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getComment()); } else if (asset instanceof Software) { cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); Company company = asset.getCompany(); if (null != company) { cell.setCellValue(company.getAlias()); } cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(AssetConstants.getAssetCatalogName(asset.getCatalog())); cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getName()); cell = row.createCell(3, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getVendor()); cell = row.createCell(4, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getModelOrVersion()); cell = row.createCell(5, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getAssetUsage()); cell = row.createCell(6, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleDate); Date pt = asset.getPurchaseTime(); if (null != pt) { cell.setCellValue(pt); } cell = row.createCell(7, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleQuantity); cell.setCellValue(asset.getQuantity()); cell = row.createCell(8, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleCost); cell.setCellValue(asset.getCost().doubleValue()); cell = row.createCell(9, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(AssetConstants.getAssetStateName(asset.getState())); cell = row.createCell(10, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(AssetConstants.getSoftwareTypeName(((Software) asset).getSoftwareType())); cell = row.createCell(11, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(((Software) asset).getLicense()); cell = row.createCell(12, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleDate); Date et = ((Software) asset).getExpiredTime(); if (null != et) { cell.setCellValue(et); } cell = row.createCell(13, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(asset.getComment()); } }
From source file:com.jeans.iservlet.controller.impl.ExportController.java
/** * ?????//w w w.ja v a 2 s. c o m * * @param storedOnly * ??? * @return * @throws IOException */ @RequestMapping(method = RequestMethod.POST, value = "/accessories") public ResponseEntity<byte[]> exportAccessories(@RequestParam boolean storedOnly) throws IOException { StringBuilder fn = new StringBuilder(getCurrentCompany().getName()); Date n = new Date(); String today = (new SimpleDateFormat("yyyyMMdd")).format(n); String now = (new SimpleDateFormat("yyyy-MM-dd HHmmss")).format(n); Workbook wb = new XSSFWorkbook(); fn.append(" - ???(").append(today).append(").xlsx"); Sheet sheet = wb.createSheet(now); // // ?10?? Font font = wb.createFont(); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontName(""); font.setFontHeightInPoints((short) 10); // ????????? CellStyle cellStyle = wb.createCellStyle(); cellStyle.setAlignment(CellStyle.ALIGN_CENTER); cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyle.setFont(font); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); cellStyle.setWrapText(false); // 20 Row row = sheet.createRow(0); row.setHeightInPoints(20); Cell cell = null; for (int i = 0; i < 7; i++) { cell = row.createCell(i, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyle); cell.setCellValue(ACS_HEADERS[i]); sheet.setColumnWidth(i, ACS_HEADERS_WIDTH[i] * 256); } List<Accessory> acs = acsService.listAccessories(getCurrentCompany(), storedOnly); Collections.sort(acs, new Comparator<Accessory>() { @Override public int compare(Accessory o1, Accessory o2) { int ret = o1.getType().compareTo(o2.getType()); if (ret == 0) { ret = Collator.getInstance(java.util.Locale.CHINA).compare(o1.getName(), o2.getName()); if (ret == 0) { ret = Collator.getInstance(java.util.Locale.CHINA).compare(o1.getBrand(), o2.getBrand()); if (ret == 0) { ret = Collator.getInstance(java.util.Locale.CHINA).compare(o1.getModel(), o2.getModel()); } } } return ret; } }); // DataFormat df = wb.createDataFormat(); // ?10? Font dFont = wb.createFont(); dFont.setFontName(""); dFont.setFontHeightInPoints((short) 10); // ?1??????? CellStyle cellStyleString = wb.createCellStyle(); cellStyleString.setAlignment(CellStyle.ALIGN_CENTER); cellStyleString.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyleString.setFont(dFont); cellStyleString.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); cellStyleString.setWrapText(false); // ?2??????(#)??? CellStyle cellStyleQuantity = sheet.getWorkbook().createCellStyle(); cellStyleQuantity.setAlignment(CellStyle.ALIGN_RIGHT); cellStyleQuantity.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyleQuantity.setFont(dFont); cellStyleQuantity.setDataFormat(df.getFormat("#")); cellStyleQuantity.setWrapText(false); int rowNumber = 1; for (Accessory ac : acs) { // 20 Row dRow = sheet.createRow(rowNumber); dRow.setHeightInPoints(20); Cell dCell = null; dCell = dRow.createCell(0, Cell.CELL_TYPE_STRING); dCell.setCellStyle(cellStyleString); dCell.setCellValue(ac.getType().getTitle()); dCell = dRow.createCell(1, Cell.CELL_TYPE_STRING); dCell.setCellStyle(cellStyleString); dCell.setCellValue(ac.getName()); dCell = dRow.createCell(2, Cell.CELL_TYPE_STRING); dCell.setCellStyle(cellStyleString); dCell.setCellValue(ac.getBrand()); dCell = dRow.createCell(3, Cell.CELL_TYPE_STRING); dCell.setCellStyle(cellStyleString); dCell.setCellValue(ac.getModel()); dCell = dRow.createCell(4, Cell.CELL_TYPE_NUMERIC); dCell.setCellStyle(cellStyleQuantity); dCell.setCellValue(null == ac.getStorage() ? 0 : ac.getStorage().getQuantity()); dCell = dRow.createCell(5, Cell.CELL_TYPE_STRING); dCell.setCellStyle(cellStyleString); dCell.setCellValue(ac.getUnit()); dCell = dRow.createCell(6, Cell.CELL_TYPE_STRING); dCell.setCellStyle(cellStyleString); dCell.setCellValue(ac.getDescription()); rowNumber++; } String filename = null; if (isIE()) { filename = URLEncoder.encode(fn.toString(), "UTF-8").replaceAll("\\+", "%20"); } else { filename = new String(fn.toString().getBytes("UTF-8"), "iso8859-1"); } response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-disposition", "attachment; filename=" + filename); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream(), 4096); wb.write(out); wb.close(); out.close(); return null; }
From source file:com.jeans.iservlet.controller.impl.ExportController.java
/** * ????/*from w w w . ja v a 2s. com*/ * * @param ids * ???(AccessoryEntryInvoice.id)? * @return * @throws IOException */ private void exportAccessoryEntries(List<Long> idList) throws IOException { StringBuilder fn = new StringBuilder(getCurrentCompany().getName()); fn.append(" - ????.xlsx"); Workbook wb = new XSSFWorkbook(); DataFormat df = wb.createDataFormat(); // ?10?? Font bFont = wb.createFont(); bFont.setBoldweight(Font.BOLDWEIGHT_BOLD); bFont.setFontName(""); bFont.setFontHeightInPoints((short) 10); // ?10? Font nFont = wb.createFont(); nFont.setFontName(""); nFont.setFontHeightInPoints((short) 10); // ??1??????? CellStyle csLeftTextBold = wb.createCellStyle(); csLeftTextBold.setAlignment(CellStyle.ALIGN_LEFT); csLeftTextBold.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftTextBold.setFont(bFont); csLeftTextBold.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csLeftTextBold.setWrapText(false); // ??2??????? CellStyle csLeftText = wb.createCellStyle(); csLeftText.setAlignment(CellStyle.ALIGN_LEFT); csLeftText.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftText.setFont(nFont); csLeftText.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csLeftText.setWrapText(false); // ??3??????? CellStyle csLeftTime = wb.createCellStyle(); csLeftTime.setAlignment(CellStyle.ALIGN_LEFT); csLeftTime.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftTime.setFont(nFont); csLeftTime.setDataFormat(df.getFormat("yyyy-MM-dd HH:mm")); csLeftTime.setWrapText(false); // ??4??????? CellStyle csCenterTextBold = wb.createCellStyle(); csCenterTextBold.setAlignment(CellStyle.ALIGN_CENTER); csCenterTextBold.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterTextBold.setFont(bFont); csCenterTextBold.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csCenterTextBold.setWrapText(false); // ??5??????? CellStyle csCenterText = wb.createCellStyle(); csCenterText.setAlignment(CellStyle.ALIGN_CENTER); csCenterText.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterText.setFont(nFont); csCenterText.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csCenterText.setWrapText(false); // ??6??????? CellStyle csCenterNumber = wb.createCellStyle(); csCenterNumber.setAlignment(CellStyle.ALIGN_CENTER); csCenterNumber.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterNumber.setFont(nFont); csCenterNumber.setDataFormat(df.getFormat("#")); csCenterNumber.setWrapText(false); // ??7????8???? CellStyle csLeftId = wb.createCellStyle(); csLeftId.setAlignment(CellStyle.ALIGN_LEFT); csLeftId.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftId.setFont(nFont); csLeftId.setDataFormat(df.getFormat("#00000000")); csLeftId.setWrapText(false); // ??8??????? CellStyle csCenterNumberBold = wb.createCellStyle(); csCenterNumberBold.setAlignment(CellStyle.ALIGN_CENTER); csCenterNumberBold.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterNumberBold.setFont(bFont); csCenterNumberBold.setDataFormat(df.getFormat("#")); csCenterNumberBold.setWrapText(false); // ???sheet for (long id : idList) { AccessoryEntryInvoice invoice = arService.loadEntryInvoice(id); if (null == invoice) { continue; } Sheet sheet = wb.createSheet("?#" + id); // ??xxxxxx Row row = sheet.createRow(0); row.setHeightInPoints(20); Cell cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue("??"); cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csLeftId); cell.setCellValue(id); // yyyy-MM-dd HH:mm row = sheet.createRow(1); row.setHeightInPoints(20); cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue(""); cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csLeftTime); cell.setCellValue(invoice.getTime()); // xxx row = sheet.createRow(2); row.setHeightInPoints(20); cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue(""); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftText); cell.setCellValue(invoice.getOperator().getName()); // row = sheet.createRow(3); row.setHeightInPoints(20); for (int i = 0; i < 8; i++) { cell = row.createCell(i, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterTextBold); cell.setCellValue(ENT_HEADERS[i]); sheet.setColumnWidth(i, ENT_HEADERS_WIDTH[i] * 256); } // List<AccessoryEntry> entries = invoice.getEntries(); int rowNumber = 4; int totalQuantity = 0; int totalRemained = 0; for (AccessoryEntry entry : entries) { Accessory acs = entry.getAccessory(); row = sheet.createRow(rowNumber); row.setHeightInPoints(20); cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getType().getTitle()); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getName()); cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getBrand()); cell = row.createCell(3, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getModel()); cell = row.createCell(4, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getDescription()); cell = row.createCell(5, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumber); cell.setCellValue(entry.getQuantity()); totalQuantity += entry.getQuantity(); cell = row.createCell(6, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getUnit()); cell = row.createCell(7, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumber); cell.setCellValue(entry.getRemained()); totalRemained += entry.getRemained(); rowNumber++; } // ? row = sheet.createRow(rowNumber); row.setHeightInPoints(20); cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterTextBold); cell.setCellValue("?"); cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumberBold); cell.setCellValue(entries.size()); cell = row.createCell(5, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumberBold); cell.setCellValue(totalQuantity); cell = row.createCell(7, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumberBold); cell.setCellValue(totalRemained); } String filename = null; if (isIE()) { filename = URLEncoder.encode(fn.toString(), "UTF-8").replaceAll("\\+", "%20"); } else { filename = new String(fn.toString().getBytes("UTF-8"), "iso8859-1"); } response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-disposition", "attachment; filename=" + filename); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream(), 4096); wb.write(out); wb.close(); out.close(); }