SpringMVC POI导出excel

时间:2024-03-19 09:20:07

1、Excel的基本概念

工作薄:所谓工作薄是指excel环境中用来存储并处理工作数据的文件。也就是说excel文档就是工作薄。它是Excel工作区中一个或多个工作表的集合,其扩展名为xls。

工作表:工作表时excel完成工作的基本单元。每张工作表是列和行所构成的“存储单元”所组成。这些存储单元被称为“单元格”,输入的所有数据保存在单元格中。

2、POI核心类

工作薄:

  • HSSFWorkbook : 这个类有读取和.xls 格式和写入Microsoft Excel文件的方法。它与微软Office97-2003版本兼容。

  • XSSFWorkbook : 这个类有读写Microsoft Excel和OpenOffice的XML文件的格式.xls或.xlsx的方法。它与MS-Office版本2007或更高版本兼容。

工作表:HSSFSheet、XSSFSheet

行:XSSFRow

单元格: XSSFCell,单元格样式:XSSFCellStyle

其他的如颜色、字体等不在一一列出,具体的内容参考poi手册:https://www.yiibai.com/apache_poi/

3、POI如何生成Excel

在POI中,是这样理解的:一个Excel文件对应一个workbook,一个workerbook是若干个sheet组成的。一个sheet有多个row,一个row一般存在多个cell。

SpringMVC POI导出excel

从上面的图片和Excel的组织结构,我们就可以明白创建Excel的步骤。

        1、生成文档对象HSSHWorkbook。

        2、通过HSSFWorkbook生成表单HSSFSheet。

        3、通过HSSFSheet生成行HSSFRow

        4、通过HSSFRow生成单元格HSSFCell。

4、SpringMVC集成POI

引入依赖:

            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.15</version>
            </dependency>

继承AbstractExcelView类

public class TaskResultExcelView extends AbstractExcelView{
	WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
	IIEDeviceTypeService iieDeviceTypeService = (IIEDeviceTypeService) ctx.getBean("iieDeviceTypeService");

	@Override
	protected void buildExcelDocument(Map<String, Object> modelMap, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
		int projectID = (int) modelMap.get("projectID");
		int taskID = (int) modelMap.get("taskID");
		String filename = "设备识别结果_"+projectID + "_" + taskID+".xls";
		response.setHeader("Content-Disposition", "inline;fileName=" + new String(filename.getBytes(), "iso8859-1"));
				
		Map<String, IIEDeviceType> deviceType1Map = iieDeviceTypeService.getDeviceType1Map();
		Map<String, IIEDeviceType> deviceType2Map = iieDeviceTypeService.getDeviceType2Map();
		
		List<ResultInfo> resultInfoList = new ArrayList<>();
		List<TaskResult> taskResultList = (List<TaskResult>) modelMap.get("taskResultList");
		for(TaskResult taskResult : taskResultList){
			ResultInfo resultInfo = new ResultInfo();
			
			String ipInfo = taskResult.getIpInfo();
			JSONObject ipInfoJsonObj = new JSONObject(ipInfo);
			
			resultInfo.ip=taskResult.getIpAddr();
			resultInfo.os = ipInfoJsonObj.getString("os");
			
			JSONObject deviceSummJsonObj = ipInfoJsonObj.getJSONObject("device_info_summary");
			String userType = deviceSummJsonObj.getString("user_type");
			String userBrand = deviceSummJsonObj.getString("user_brand");
			String brand = deviceSummJsonObj.getString("brand");
			String model = deviceSummJsonObj.getString("model");
			String deviceType1 = deviceSummJsonObj.getString("device_type_1");
			String deviceType2 = deviceSummJsonObj.getString("device_type_2");
			if(deviceType1Map.get(deviceType1) != null){
				resultInfo.deviceType1 = deviceType1Map.get(deviceType1).getTypeCnName();
			}
			if (userType.length() > 0) {
				if (deviceType2Map.get(userType) != null) {
					resultInfo.deviceType2 = deviceType2Map.get(userType).getTypeCnName() + "(" + userType + ")";
				}
			} else {
				if (deviceType2Map.get(deviceType2) != null) {
					resultInfo.deviceType2 = deviceType2Map.get(deviceType2).getTypeCnName() + "(" + deviceType2 + ")";
				}
			}
			if(userBrand.length() > 0){
				resultInfo.brand = userBrand;
			}else{
				resultInfo.brand = brand;
			}
			resultInfo.model = model;
			resultInfo.vulNum = taskResult.getVulNum();
			
			int extNum =0,highNum=0,midNum=0,lowNum=0;
			JSONArray portInfoArr = ipInfoJsonObj.getJSONArray("port_list");
			for(int i=0;i<portInfoArr.length();i++){
				JSONArray vulArr = portInfoArr.getJSONObject(i).getJSONArray("vul_list");
				for(int j =0;j<vulArr.length();j++){
					String level = vulArr.getJSONObject(j).getString("priv_vul_level");
					if(level.equals("严重")){
						extNum++;
					}else if(level.equals("高危")){
						highNum++;
					}else if(level.equals("中危")){
						midNum++;
					}else if(level.equals("低危")){
						lowNum++;
					}
				}
			}
			JSONObject onvifObj = ipInfoJsonObj.getJSONObject("onvif");
			JSONArray vulArr = onvifObj.getJSONArray("vul_list");
			for(int j =0;j<vulArr.length();j++){
				String level = vulArr.getJSONObject(j).getString("priv_vul_level");
				if(level.equals("严重")){
					extNum++;
				}else if(level.equals("高危")){
					highNum++;
				}else if(level.equals("中危")){
					midNum++;
				}else if(level.equals("低危")){
					lowNum++;
				}
			}
			resultInfo.extNum = extNum;
			resultInfo.highNum = highNum;
			resultInfo.midNum = midNum;
			resultInfo.lowNum = lowNum;
			
			resultInfoList.add(resultInfo);
		}
	    
		HSSFSheet sheet = workbook.createSheet("设备识别结果");
		sheet.setDefaultColumnWidth(30);
		
		CellStyle baseCellStyle = workbook.createCellStyle();
		baseCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		baseCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		baseCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		baseCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		baseCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		
		CellStyle headerCellStyle = workbook.createCellStyle();
		headerCellStyle.cloneStyleFrom(baseCellStyle);
		headerCellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
		headerCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		
		HSSFRow header = sheet.createRow(0);
		header.createCell(0).setCellValue("IP地址");
		header.createCell(1).setCellValue("设备大类");
		header.createCell(2).setCellValue("设备类型");
		header.createCell(3).setCellValue("设备品牌");
		header.createCell(4).setCellValue("设备型号");
		header.createCell(5).setCellValue("操作系统");
		header.createCell(6).setCellValue("漏洞数量");
		header.createCell(7).setCellValue("极危");
		header.createCell(8).setCellValue("高危");
		header.createCell(9).setCellValue("中危");
		header.createCell(10).setCellValue("低危");
		for (int i = 0; i < 11; i++) {
			header.getCell(i).setCellStyle(headerCellStyle);
		}
		
		int rowNum =1;
		for(ResultInfo resultInfo : resultInfoList){
			HSSFRow row = sheet.createRow(rowNum++);
			row.createCell(0).setCellValue(resultInfo.ip);
			row.createCell(1).setCellValue(resultInfo.deviceType1);
			row.createCell(2).setCellValue(resultInfo.deviceType2);
			row.createCell(3).setCellValue(resultInfo.brand);
			row.createCell(4).setCellValue(resultInfo.model);
			row.createCell(5).setCellValue(resultInfo.os);
			row.createCell(6).setCellValue(resultInfo.vulNum);
			row.createCell(7).setCellValue(resultInfo.extNum);
			row.createCell(8).setCellValue(resultInfo.highNum);
			row.createCell(9).setCellValue(resultInfo.midNum);
			row.createCell(10).setCellValue(resultInfo.lowNum);
		}
	}
	
	
	private class ResultInfo {
		public String ip = "";
		public String deviceType1 = "";
		public String deviceType2 = "";
		public String brand = "";
		public String model = "";
		public String os = "";
		public int vulNum = 0;
		public int extNum = 0;
		public int highNum = 0;
		public int midNum = 0;
		public int lowNum = 0;
	}

}

配置视图解析器:

<!-- 配置excel解析器 -->
	<bean class = "org.springframework.web.servlet.view.BeanNameViewResolver" p:order = "1"/>
	<bean id = "taskResultExcel" class="com.cyberpecker.util.TaskResultExcelView"/>

视图解析器的优先级要比InternalResourceViewResolver的高。

controller中路由:

	@RequestMapping(value = "/export-excel")
	public String exportResultExcel(@RequestParam("projectID") int projectID, @RequestParam("taskID") int taskID, ModelMap modelMap) {
		List<TaskResult> taskResultList = taskResultService.getTaskResult(taskID,projectID);
		modelMap.addAttribute("projectID",projectID);
		modelMap.addAttribute("taskID",taskID);
		modelMap.addAttribute("taskResultList", taskResultList);
		return "taskResultExcel";
	}