栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

页面数据导出(非模板)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

页面数据导出(非模板)

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 titleMap = new LinkedHashMap();
			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> 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 titleMap, List> 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 titleList = new ArrayList();
        // 给列写入数据,创建单元格,写入数据 创建标题
        int title = 0;
        for(Map.Entry 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;
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/969846.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号