SpringMvc 使用poi导入导出Excel

Posted cnsu-cmh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMvc 使用poi导入导出Excel相关的知识,希望对你有一定的参考价值。

controller <pre name="code" class="java">    @ResponseBody
    @RequestMapping(value = "/chxm/exportAndUpdate")
    public void exportAndUpdate(@RequestParam(value = "projectId") String projectId,
    		@RequestParam(value = "projectName") String  projectName,HttpServletResponse response){
    	chxmService.exportAndUpdate(projectId,projectName,response);
    }


service
<pre name="code" class="java">@Override
	public void exportAndUpdate(String projectId,String projectName,HttpServletResponse response) {
		try{  
			response.setContentType("application/vnd.ms-excel;charset=utf-8"); 
            OutputStream outputStream = response.getOutputStream();  
            String fileName = new String((projectName).getBytes(), "utf-8");  
            response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");// 组装附件名称和格式  
            response.setCharacterEncoding("utf-8");
            
            // 准备excel文件内容  
            String[] zxxTitles = { "幢号","建筑物名称","楼栋西南角X坐标","楼栋西南角Y坐标","占地面积","建筑面积",
            					"基本用途","总层数","地上层数","地下层数","单元数","建筑结构", "特殊幢标识符","备注" }; 
            List<TZxx> zxxs = zxxDao.findZxxByChxmId(Long.parseLong(projectId));
            String[] cxxTitles = { "所属幢号", "实际层", "名义层","层建筑面积","层阳台面积","层共有建筑面积",
            					"层分摊建筑面积","层半墙面积","层高","备注","层图文件夹" }; 
            List<TCxx> cxxs = cxxDao.findHxxByChxmId(Long.parseLong(projectId));
            String[] hxxTitles = { "幢号","户编号", "单元", "户型结构","所在层(最低层)","所在层(最高层)","房屋用途",
            					"套内面积","分摊面积","建筑面积","地下部分建筑面积","其他建筑面积","分摊系数","房屋类型",
            					"房屋性质","设计用途","阳台数","封闭阳台数","非封闭式阳台","备注","房屋坐落","户型","户型图文件夹" }; 
            List<THxx> hxxs = hxxDao.findHxxByChxmId(Long.parseLong(projectId));
            String[] hcgTitles = { "幢号","户编号","层高","实际层","备注" }; 
            List<THcg> hcgs = hcgDao.findHcgxxByChxmId(Long.parseLong(projectId));
            
            // 创建一个workbook 对应一个excel应用文件  
            XSSFWorkbook workBook = new XSSFWorkbook();  
            this.export(workBook,"幢",zxxTitles,zxxs,TZxx.class);
            this.export(workBook,"层",cxxTitles,cxxs,TCxx.class);
            this.export(workBook,"户",hxxTitles,hxxs,THxx.class);
            this.export(workBook,"户层高",hcgTitles,hcgs,THcg.class);
            try{  
                workBook.write(outputStream);  
                outputStream.flush();  
                outputStream.close(); 
                logger.info("项目"+fileName+"更正导出幢、层、户、户层高信息OK!");
            }catch (IOException e){  
            	logger.info("项目"+fileName+"更正导出幢、层、户、户层高信息error!");
            }finally{  
	            try  
	            {  
	            	outputStream.close();  
	            }  
	            catch (IOException e)  
	            {  
	                e.printStackTrace();  
	            }  
            }
		}catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
	}

	private void export(XSSFWorkbook workBook, String sheetName, String[] titles, List<?> list ,Class<?> class1) {
		
        // 在workbook中添加一个sheet,对应Excel文件中的sheet  
        XSSFSheet sheet = workBook.createSheet(sheetName); 
        
        // 构建表头  
        XSSFRow headRow = sheet.createRow(0);
       
        XSSFCell cell = null;  
        for (int i = 0; i < titles.length; i++)  
        {  
            cell = headRow.createCell(i);  
            cell.setCellValue(titles[i]);
            sheet.setColumnWidth(i,titles[i].getBytes().length * 256);
        }  
        
        // 构建表体数据  
        if (list != null && list.size() > 0)  
        {  
        	if(class1.getName().equals(TZxx.class.getName())){
        		for (int j = 0; j < list.size(); j++){  
        			XSSFRow bodyRow = sheet.createRow(j + 1); 
                	TZxx zxx = (TZxx) list.get(j); 
                	//幢号
                	cell = bodyRow.createCell(0);  
                	cell.setCellValue(zxx.getZh_());
                	//建筑物名称
                	cell = bodyRow.createCell(1);  
                	cell.setCellValue(zxx.getFcqmc()); 
                	//楼栋西南角X坐标
                	cell = bodyRow.createCell(2);  
                	cell.setCellValue(zxx.getFxnjx()); 
                	//楼栋西南角Y坐标
                	cell = bodyRow.createCell(3);  
                	cell.setCellValue(zxx.getFxnjy()); 
                	//占地面积
                	cell = bodyRow.createCell(4);  
                	cell.setCellValue(zxx.getBzzdmj());  
        		}	
        	}else if (class1.getName().equals(TCxx.class.getName())) {
        		for (int j = 0; j < list.size(); j++){  
        			XSSFRow bodyRow = sheet.createRow(j + 1); 
                	TCxx cxx = (TCxx) list.get(j);  
                	
                	//所属幢号
                	cell = bodyRow.createCell(0);  
                	cell.setCellValue(cxx.getZh_());  
                	//实际层
                	cell = bodyRow.createCell(1);  
                	cell.setCellValue(cxx.getSjc());  
                	//名义层
                	cell = bodyRow.createCell(2);  
                	cell.setCellValue(cxx.getMyc()); 
                	//层建筑面积
                	cell = bodyRow.createCell(3);  
                	cell.setCellValue(cxx.getBcjzmj());  
                	//层阳台面积
                	cell = bodyRow.createCell(4);  
                	cell.setCellValue(cxx.getBcytmj());  
                	//层共有建筑面积
                	cell = bodyRow.createCell(5);  
                	cell.setCellValue(cxx.getBcgyjzmj());  
		<span style="white-space:pre">	</span>}
        	}else if(class1.getName().equals(THxx.class.getName())){
        		for (int j = 0; j < list.size(); j++){  
        			XSSFRow bodyRow = sheet.createRow(j + 1); 
					THxx hxx = (THxx) list.get(j);  
					
			//幢号
                	cell = bodyRow.createCell(0);  
                	cell.setCellValue(hxx.getZh_());
                	//户编号
                	cell = bodyRow.createCell(1);  
                	cell.setCellValue(hxx.getHh()); 
                	//单元
                	cell = bodyRow.createCell(2);  
                	cell.setCellValue(hxx.getDyh()); 
                	//户型结构
                	cell = bodyRow.createCell(3);  
                	cell.setCellValue(hxx.getJg()); 
                	//所在层(最低层)
                	cell = bodyRow.createCell(4);  
                	cell.setCellValue(hxx.getLayer()); 
                	//所在层(最高层)
                	cell = bodyRow.createCell(5);  
                	cell.setCellValue(hxx.getToplayer()); 
	        	}
            }else if(class1.getName().equals(THcg.class.getName())){
        		for (int j = 0; j < list.size(); j++){ 
        			XSSFRow bodyRow = sheet.createRow(j + 1); 
        			THcg hcg = (THcg) list.get(j);  
					
			//幢号
                	cell = bodyRow.createCell(0);  
                	cell.setCellValue(hcg.getZh_());
                	//户编号
                	cell = bodyRow.createCell(1);  
                	cell.setCellValue(hcg.getHh()); 
                	//层高
                	cell = bodyRow.createCell(2);  
                	cell.setCellValue(hcg.getScg().toString()); 
                	//实际层
                	cell = bodyRow.createCell(3);  
                	cell.setCellValue(hcg.getSjc()); 
                	//备注
                	cell = bodyRow.createCell(4);  
                	cell.setCellValue(hcg.getBz()); 
        		}
        	}    
	}
}
dao

以上是关于SpringMvc 使用poi导入导出Excel的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot图文教程9—SpringBoot 导入导出 Excel 「Apache Poi」

java工厂类 poi导出

springMVC导入excel案例poi

SpringMVC 5.0.4集成easyexcel读取excel文档

normalexcelconstants导出怎么设置底部

用SpringMvc实现Excel导出功能