页面数据导出(非模板)

1、Controller

	@RequestMapping(value = "/pasteListExport", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	@ApiOperation(httpMethod = "POST", value = "贴标列表导出")
	public void pasteListExport(@Valid @ApiParam @RequestBody DeliverListExcelQuery deliverListExcelQuery,
			HttpServletRequest request, HttpServletResponse response) {
		XSSFWorkbook workbook = null;
        OutputStream out = null;
        try {
//设置Title
			Map<String , String> titleMap = new LinkedHashMap<String , String>();
			titleMap.put("waybillNo","运单号");
			titleMap.put("batchNo","发货批号");
			titleMap.put("pasteContent","贴标备注");
			titleMap.put("pastePieceNo","贴标件数");
			titleMap.put("custNo","客户编号");
			titleMap.put("customsTime","清关时间");
			titleMap.put("warehousingTime","入库时间");
			titleMap.put("warehousingBatchNo","入库批号");
			titleMap.put("warehousingAccount","入库人");
			titleMap.put("ladingbillNo","提单号");
			titleMap.put("moveOutBatchNo","搬出批号");
			titleMap.put("warehousingArea","库区");
			titleMap.put("warehousingNo","库号");
			
			List<Map<String , Object>> list = waybillWarehouseService.deliverListExport(request.getHeader("accountId"), deliverListExcelQuery);
			workbook = POIUtil.createExcel(titleMap, list);
        	// 写入Excel文件到前端
            if(null != workbook){
                String fileName = "贴标列表导出.xlsx";
                fileName = new String(fileName.getBytes("UTF-8"),"iso8859-1");
                response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
                response.setContentType("application/x-download");
                response.setCharacterEncoding("UTF-8");
                response.addHeader("Pargam", "no-cache");
                response.addHeader("Cache-Control", "no-cache");
                response.flushBuffer();
                out = response.getOutputStream();
                workbook.write(out);
                out.flush();
            }
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				if (workbook != null) {
					workbook.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}

2、Service层就是你从数据库中查出来的数据,根据自己实际开发业务来写

3、导出数据方法

    public static XSSFWorkbook createExcel(Map<String , String> titleMap, List<Map<String,Object>> listresult){
        // 1.创建HSSFWorkbook,一个HSSFWorkbook对应一个Excel文件
        XSSFWorkbook wb = new XSSFWorkbook();
        // 2.在workbook中添加一个sheet,对应Excel文件中的sheet
        XSSFSheet sheet = wb.createSheet("sheet1");
        // 3.设置表头,即每个列的列名
        // 3.1创建第一行
        XSSFRow row = sheet.createRow(0);   
        List<String> titleList = new ArrayList<String>();
        // 给列写入数据,创建单元格,写入数据 创建标题
        int title = 0;
        for(Map.Entry<String, String> entry : titleMap.entrySet()){
        	titleList.add(entry.getKey());
        	row.createCell(title).setCellValue(entry.getValue());
        	title++;
        }
        // 写入正式数据
        for (int i = 0; i < listresult.size(); i++) {
            // 创建行
            row = sheet.createRow(i+1);
            //列当中写入数据
            for(int j = 0; j < titleList.size(); j++) {
            	String key = titleList.get(j);
            	String val = CommUtil.toString(listresult.get(i).get(key));
            	row.createCell(j).setCellValue(val);
            }
        }
        
        /**
         * 上面的操作已经是生成一个完整的文件了,只需要将生成的流转换成文件即可;
         * 下面的设置宽度可有可无,对整体影响不大
         */
        // 设置单元格宽度
        int curColWidth = 0;
        for (int i = 0; i <= titleList.size(); i++) {
            // 列自适应宽度,对于中文半角不友好,如果列内包含中文需要对包含中文的重新设置。
            sheet.autoSizeColumn(i, true);
            // 为每一列设置一个最小值,方便中文显示
            curColWidth = sheet.getColumnWidth(i);  
            if(curColWidth<3000){
                sheet.setColumnWidth(i, 3000); 
            }
        }
        return wb;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值