导出execl
Controller
/*** 導出參考價設置** @return* @throws IOException* @throws InvalidFormatException* @throws NoSuchMethodException* @throws InvocationTargetException* @throws IllegalAccessException*///用post原因,輸入的查詢參數太長了@RequestMapping(value = "/exportPrice", method = {RequestMethod.POST})@ResponseBody@ApiOperation(value = "導出參考價設置", notes = "導出參考價設置", response = String.class)public void exportinsurance(HttpServletResponse response,@ApiParam(value = "發貨機構") @RequestParam(required = false) List<Long> deliveryOrgIds,@ApiParam(value = "到貨機構") @RequestParam(required = false) List<Long> receiveOrgIds,@ApiParam(value = "狀態:1有效、2無效") @RequestParam(required = true) Integer validStatus)throws IOException, InvalidFormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {List<LinkedHashMap> result = new LinkedList<>();List<ConfigReferencePriceDto> exportList = configReferencePriceService.findExportList(deliveryOrgIds, receiveOrgIds, validStatus);String[] cols = {"deliveryOrgId", "receiveOrgId", "unitVolumePrice", "unitWeightPrice", "thresholdVolume", "thresholdVolumePrice", "thresholdWeight", "thresholdWeightPrice", "singlePiecePrice","abnormityCoefficient", "equipmentCoefficient", "status", "updateTime"};List<Organization> organizationList = organizationService.findByNamedParamList(null);List<String> columns = new ArrayList<>();for (String s : cols) {columns.add(s);}for (ConfigReferencePriceDto priceDto : exportList) {LinkedHashMap<String, String> data = new LinkedHashMap<>();for (String column : columns) {String value = "";if (column.equals("deliveryOrgId")) {//收貨機構if (priceDto.getDeliveryOrgId() != null) {for (Organization organ : organizationList) {if (organ.getId().equals(priceDto.getDeliveryOrgId())) {value = organ.getName();}}}} else if (column.equals("receiveOrgId")) {//收貨機構if (priceDto.getReceiveOrgId() != null) {for (Organization organ : organizationList) {if (organ.getId().equals(priceDto.getReceiveOrgId())) {value = organ.getName();}}}} else if (column.equals("unitVolumePrice")) {//標準體積參考價value = String.valueOf(priceDto.getUnitVolumePrice());} else if (column.equals("unitWeightPrice")) {//標準重量參考價value = String.valueOf(priceDto.getUnitWeightPrice());} else if (column.equals("thresholdVolume")) {//體積閾值value = String.valueOf(priceDto.getThresholdVolume());} else if (column.equals("thresholdVolumePrice")) {//超體積閾值價格value = String.valueOf(priceDto.getThresholdVolumePrice());} else if (column.equals("thresholdWeight")) {//重量閾值value = String.valueOf(priceDto.getThresholdWeight());} else if (column.equals("thresholdWeightPrice")) {//超重量閾值價格value = String.valueOf(priceDto.getThresholdWeightPrice());} else if (column.equals("singlePiecePrice")) {//單價參考價value = String.valueOf(priceDto.getSinglePiecePrice());} else if (column.equals("abnormityCoefficient")) {//異形件系數value = String.valueOf(priceDto.getAbnormityCoefficient());} else if (column.equals("equipmentCoefficient")) {//設備系數value = String.valueOf(priceDto.getEquipmentCoefficient());} else if (column.equals("status")) {switch (priceDto.getStatus()) {case 1:value = "有效";break;case 2:value = "無效";break;default:value = "";}} else if (column.equals("updateTime")) {value = DateUtils.getStrDate(priceDto.getUpdateTime(), "yyyy-MM-dd HH:mm:ss");}data.put(column, value);}result.add(data);}exporterFactory.process(response, result, new ExporterHead("參考價設置"), ReferencePriceExporterTemplate.class, columns);}ExporterConfig類
exporterTemplate.put(ReferencePriceExporterTemplate.class, getReferencePriceExporterTemplate());@Beanpublic ReferencePriceExporterTemplate getReferencePriceExporterTemplate() {ReferencePriceExporterTemplate referencePriceExporterTemplate = new ReferencePriceExporterTemplate();referencePriceExporterTemplate.setDefaultFileName("參考價設置");referencePriceExporterTemplate.setStartRowNumber(3);return referencePriceExporterTemplate;}ReferencePriceExporterTemplate類
package com.opensesame.platform.web.exporter.template;import com.opensesame.core.lang.Tuple; import com.opensesame.core.util.DateUtils; import com.opensesame.platform.web.exporter.head.ExporterHead; import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.RegionUtil; import org.apache.poi.xssf.usermodel.XSSFCellStyle;import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map;@SuppressWarnings("unchecked") public class ReferencePriceExporterTemplate extends AbstractExporterTemplate {private static final Map<String, Tuple<String, Integer>> METADATA = new HashMap<String, Tuple<String, Integer>>();static {METADATA.put("deliveryOrgId", new Tuple<String, Integer>("收貨機構", 20 * 256));METADATA.put("receiveOrgId", new Tuple<String, Integer>("到貨機構", 20 * 256));METADATA.put("unitVolumePrice", new Tuple<String, Integer>("標準體積參考價(元 /m3)", 25 * 256));METADATA.put("unitWeightPrice", new Tuple<String, Integer>("標準重量參考價(元 /kg)", 25 * 256));METADATA.put("thresholdVolume", new Tuple<String, Integer>("體積閾值(m3)", 20 * 256));METADATA.put("thresholdVolumePrice", new Tuple<String, Integer>("超體積閾值價格(元/m3)", 25 * 256));METADATA.put("thresholdWeight", new Tuple<String, Integer>("重量閾值(kg)", 20 * 256));METADATA.put("thresholdWeightPrice", new Tuple<String, Integer>("超重量閾值價格(元/kg)", 25 * 256));METADATA.put("singlePiecePrice", new Tuple<String, Integer>("單件參考價", 15 * 256));METADATA.put("abnormityCoefficient", new Tuple<String, Integer>("異形件系數", 15 * 256));METADATA.put("equipmentCoefficient", new Tuple<String, Integer>("設備系數", 15 * 256));METADATA.put("status", new Tuple<String, Integer>("狀態", 15 * 256));METADATA.put("updateTime", new Tuple<String, Integer>("時間", 25 * 256));}@Overridepublic void drawHead(Workbook workBook, Sheet sheet, ExporterHead exporterHead, CellStyle cellStyle, Object... args) {List<String> columns = (List<String>) args[0];// 設置標題CellRangeAddress head = new CellRangeAddress(0, 0, 0, columns.size());sheet.addMergedRegion(head);Cell headCell = sheet.createRow(0).createCell(0);headCell.setCellValue(exporterHead.getTitle());headCell.setCellStyle(getHeadCellStyle(workBook));RegionUtil.setBorderBottom(XSSFCellStyle.BORDER_THIN, head, sheet, workBook);RegionUtil.setBorderLeft(XSSFCellStyle.BORDER_THIN, head, sheet, workBook);RegionUtil.setBorderRight(XSSFCellStyle.BORDER_THIN, head, sheet, workBook);RegionUtil.setBorderTop(XSSFCellStyle.BORDER_THIN, head, sheet, workBook);// 設置時間CellRangeAddress date = new CellRangeAddress(1, 1, 0, columns.size());sheet.addMergedRegion(date);Cell dateCell = sheet.createRow(1).createCell(0);dateCell.setCellValue("導出時間:" + DateFormatUtils.format(new Date(), DateUtils.DATE_TIME_FORMAT));dateCell.setCellStyle(getDateCellStyle(workBook));RegionUtil.setBorderBottom(XSSFCellStyle.BORDER_THIN, date, sheet, workBook);RegionUtil.setBorderLeft(XSSFCellStyle.BORDER_THIN, date, sheet, workBook);RegionUtil.setBorderRight(XSSFCellStyle.BORDER_THIN, date, sheet, workBook);RegionUtil.setBorderTop(XSSFCellStyle.BORDER_THIN, date, sheet, workBook);Row row = sheet.createRow(2);int columnNum = 0;CellStyle titleCellStyle = getTitleCellStyle(workBook);for (String column : columns) {Cell cell = row.createCell(columnNum++);cell.setCellStyle(titleCellStyle);cell.setCellValue(METADATA.get(column).getA());}//將字段改為數字格式List<LinkedHashMap> data = (List<LinkedHashMap>) args[1];int startRow = 3;for (LinkedHashMap<String, Object> temp : data) {Row dataRow = sheet.createRow(startRow++);int rowNum = 0;for (Map.Entry<String, Object> value : temp.entrySet()) {Cell cell = dataRow.createCell(rowNum++);if (value.getKey().equals("capacity") || value.getKey().equals("netWeight")) {CellStyle cellStyleNumber = getCellStyle(workBook);cell.setCellStyle(cellStyleNumber);cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);cell.setCellValue(Double.parseDouble(value.getValue().toString()));} else {cell.setCellStyle(cellStyle);cell.setCellValue(value.getValue().toString());}}}}@Overridepublic void windup(Workbook workBook, Sheet sheet, ExporterHead exporterHead, CellStyle cellStyle, Object... args) {List<String> columns = (List<String>) args[0];int columnNum = 0;//去掉列自適應寬度 // sheet.autoSizeColumn(columnNum++, true);for (String column : columns) {sheet.setColumnWidth(columnNum++, METADATA.get(column).getB());}}/*** 獲取單元格樣式** @param workBook* @return*/public static CellStyle getHeadCellStyle(Workbook workBook) {CellStyle style = workBook.createCellStyle();style.setAlignment(CellStyle.ALIGN_CENTER);Font font = workBook.createFont();font.setFontName("Calibri");font.setBoldweight(Font.BOLDWEIGHT_BOLD);font.setFontHeightInPoints((short) 20);style.setFont(font);return style;}/*** 獲取單元格樣式** @param workBook* @return*/public static CellStyle getDateCellStyle(Workbook workBook) {CellStyle style = workBook.createCellStyle();style.setAlignment(CellStyle.ALIGN_CENTER);Font font = workBook.createFont();font.setFontName("Calibri");font.setFontHeightInPoints((short) 12);style.setFont(font);return style;}/*** 獲取單元格樣式** @param workBook* @return*/public static CellStyle getTitleCellStyle(Workbook workBook) {CellStyle style = workBook.createCellStyle();style.setAlignment(CellStyle.ALIGN_CENTER);style.setBorderBottom(XSSFCellStyle.BORDER_THIN);style.setBorderTop(XSSFCellStyle.BORDER_THIN);style.setBorderRight(XSSFCellStyle.BORDER_THIN);style.setBorderLeft(XSSFCellStyle.BORDER_THIN);Font font = workBook.createFont();font.setFontName("Calibri");font.setFontHeightInPoints((short) 12);style.setFont(font);return style;}}總結
- 上一篇: Linux系统下i350网卡固件更新说明
- 下一篇: 网分测花岗岩介电常数测试方案