导入导出功能
Posted inspred
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了导入导出功能相关的知识,希望对你有一定的参考价值。
一,步骤
1.导入:
一.ftl <form id="form" action="" enctype="multipart/form-data" method="post" > <input type="file" id="fileID" name="fileName" class="btn dropdown-toggle btn-primary" style="width: 220px; float:left;" /> <input type="hidden" name="projectId" id="projectId" value=""> <input type="button" value="导入源声" onclick="importList();" class="btn dropdown-toggle btn-success" style="margin-left:20px;"/> </form> 注意:后台获取属性的值,必须指定 name = "" 二.js function importList() { var filePath = $("#fileID").val(); if (filePath == ‘‘) { alert(‘请选择文件!‘); return; } if(pnationId == ‘-1‘ || nationId == ‘-1‘){ alert(‘请选择国家!‘); return; } var form = $("#form"); var projectId = GetQueryString("projectId "); $(‘#projectId‘).val(projectId); form.attr("action", "/nps/analysis/import"); form.attr("method", "POST"); form.submit(); } //获取projectId function GetQueryString(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; } 三.controller @RequestMapping(value="/analysis/import",method = RequestMethod.POST) public String importSource(HttpServletRequest request,@RequestParam("fileName") CommonsMultipartFile file, Integer projectId){ InputStream in =null; String msg = ""; try { in = file.getInputStream(); msg = npsSourceAnalysisService.importList(in, file.getOriginalFilename(),pnationId,nationId); in.close(); } catch (IOException e) { e.printStackTrace(); logger.info(e.getMessage()); msg = "导入失败!"; } finally { if (in != null) { try { in.close(); // 关闭流 } catch (IOException e) { logger.debug("in close IOException:" + e.getMessage()); } } } return "redirect:/nps/analysis/list?projectId="+projectId+"&msg="+msg; } 四.service public String importList(InputStream in,String fileName); @Transactional public String importList(InputStream in, String fileName,Integer pnationId,Integer nationId) { String[][] table = null; String msg = null; try { table = ExcelUtils.readExcel(in,fileName,0); } catch (Exception e) { e.printStackTrace(); return "文件导入失败,请检查文件格式!"; } Map<String,Integer> tableColumMap = new HashMap<String, Integer>(); if (CheckUtils.isNullOrBlank(table) || table.length == 0) { return "表格中没有任何数据!"; } if (table.length == 1) { return "表格中只存在表头数据!"; } //校验导入文件内容 for (int i = 0; i < table[0].length; i++) { String cell = table[0][i].trim(); tableColumMap.put(cell, i); } List<String> errorMsgs = new ArrayList<String>(); String[] titles ={"ID","描述","产品名称","型号","版本","时间","次数","得分(问题1)","得分(问题2)"}; for (int i = 0; i < titles.length; i++) { String title = titles[i]; if(!tableColumMap.containsKey(title)){ errorMsgs.add("Excel表格表头列有误,不存在"+title+"列,请修改Excel后导入"); if(errorMsgs.size() >= 10) return "导入文件表头不正确,请检查后重新导入!"; } } if (errorMsgs.size() == 0) { NPSSourceAnalysis npsSourceAnalysis=new NPSSourceAnalysis(); for (int i = 1; i < table.length; i++) { String sourceID = table[i][tableColumMap.get("ID")].trim(); String descript = table[i][tableColumMap.get("描述")].trim(); String product = table[i][tableColumMap.get("名称")].trim(); String inner = table[i][tableColumMap.get("型号")].trim(); String ROM = table[i][tableColumMap.get("版本")].trim(); String createTime = table[i][tableColumMap.get("时间")].trim(); String SN = table[i][tableColumMap.get("SN")].trim(); String CSR = table[i][tableColumMap.get("得分(问题1)")].trim(); String recommend = table[i][tableColumMap.get("得分(问题2)")].trim(); npsSourceAnalysis.setSourceID(Integer.parseInt(sourceID)); npsSourceAnalysis.setDescript(descript); npsSourceAnalysis.setProduct(product); npsSourceAnalysis.setInnerVersion(inner); npsSourceAnalysis.setOuterVersion(outer); npsSourceAnalysis.setROM(ROM); npsSourceAnalysis.setCreateTime(createTime); npsSourceAnalysis.setSN(SN); npsSourceAnalysis.setCSR(Integer.parseInt(CSR)); npsSourceAnalysis.setRecommend(Integer.parseInt(recommend)); npsSourceAnalysis.setHandled(0); npsSourceAnalysis.setAbroad(0); npsSourceAnalysis.setPnationId(pnationId); npsSourceAnalysis.setNationId(nationId); nPSSourceAnalysisMapper.insert(npsSourceAnalysis); } } else{ for (String errorMsg : errorMsgs) { msg = msg + errorMsg +";"; } } return msg; } 五,需要的工具类 ExcelUtils.readExcel() 六.依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.16-beta1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.16-beta1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.1</version> </dependency <!-- https://mvnrepository.com/artifact/org.samba.jcifs/jcifs --> <dependency> <groupId>org.samba.jcifs</groupId> <artifactId>jcifs</artifactId> <version>1.2.19</version> </dependency>
2.导出:
一.Controller /** * 源声分析结果导出 */ @RequestMapping(value = "/devide/explorExcel", method = { RequestMethod.POST }) public void explorExcel(HttpServletRequest request,HttpServletResponse response) throws CSException { Integer projectId = Integer.valueOf(request.getParameter("projectId")); String starttime = request.getParameter("starttime"); String endtime = request.getParameter("endtime"); List<Version> versions=versionService.getVersionListByProjectId(projectId); String product=versions.get(0).getProduct(); List<NPSAnalysisDevide> list = npsAnalysisDevideService.selectAllBad(product,starttime,endtime); npsAnalysisDevideService.exportList(response,list,product); } 二.service public void exportList(HttpServletResponse response, List<NPSAnalysisDevide> list,String projectName); @Override public void exportList(HttpServletResponse response,List<NPSAnalysisDevide> items,String projectName) { BuildExcelUtils bEUtils = new BuildExcelUtils(items,projectName){ @Override public void buildDataTo2007Excel(ExcelWrite2007 excel,XSSFSheet sheet, XSSFCellStyle contentFormat,List<?> items,String projectName) { @SuppressWarnings("unchecked") List<NPSAnalysisDevide> list = (List<NPSAnalysisDevide>) items; int rowIdx = 0; for (NPSAnalysisDevide item : list) { rowIdx++; excel.setVal(sheet, rowIdx, 0, item.getSourceID(), contentFormat); excel.setVal(sheet, rowIdx, 1, item.getDescript(), contentFormat); excel.setVal(sheet, rowIdx, 2, item.getProduct(), contentFormat); excel.setVal(sheet, rowIdx, 3, item.getInnerVersion(), contentFormat); excel.setVal(sheet, rowIdx, 4, item.getOuterVersion(), contentFormat); excel.setVal(sheet, rowIdx, 5, item.getROM(), contentFormat); excel.setVal(sheet, rowIdx, 6, item.getCreateTime(), contentFormat); excel.setVal(sheet, rowIdx, 6, item.getCreateTime(), contentFormat); excel.setVal(sheet, rowIdx, 7, item.getSN(), contentFormat); excel.setVal(sheet, rowIdx, 8, item.getCSR(), contentFormat); excel.setVal(sheet, rowIdx, 9, item.getRecommend(), contentFormat); excel.setVal(sheet, rowIdx, 10, item.getClassfy(), contentFormat); excel.setVal(sheet, rowIdx, 11, item.getOpinion(), contentFormat); excel.setVal(sheet, rowIdx, 12, item.getEmotion(), contentFormat); excel.setVal(sheet, rowIdx, 13, item.getRegional(), contentFormat); excel.setVal(sheet, rowIdx, 14, item.getNationStr(), contentFormat); } } }; int[] redTitleCols = {};//设置红色样式的列表头 int[] colsWidths = {3000,3000,5000,15000,2500,2500,3500,5000,5000,2000,3000,3000,3000,3000,3000};//设置列宽 bEUtils.exportExcel("结果", historyTitle,redTitleCols ,colsWidths, response); } 三.工具类 BuildExcelUtils 四.依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.16-beta1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.16-beta1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.1</version> </dependency
以上是关于导入导出功能的主要内容,如果未能解决你的问题,请参考以下文章