导入
前端js和跳转页面
<t:dgToolBar title="导入单一模板" icon="icon-put" funname="Importonlyone"></t:dgToolBar>
function Importonlyone(title,url,gname) { gridname=gname; var ids = []; var rows = $("#"+gname).datagrid('getSelections'); if(rows.length==1){ openuploadwin('Excel导入', 'decMainController.do?upload&num=1&ids='+rows[0].id, "decMainList"); } }
<t:formvalid formid="formobj" layout="div" dialog="true" beforeSubmit="upload"> <fieldset class="step"> <div class="form"><t:upload name="fiels" buttonText="选择要导入的文件" uploader="${controller_name}.do?${empty method_name?'importExcel':method_name }"
extend="*.xls;*.xlsx" id="file_upload" formData="documentTitle"></t:upload></div> <div class="form" id="filediv" style="height: 50px"></div> </fieldset> </t:formvalid>
后台跳转方法及导入解析
@RequestMapping(params = "upload") public ModelAndView upload(HttpServletRequest req) { req.setAttribute("controller_name","decMainController"); req.setAttribute("method_name","importonlyone"); req.setAttribute("ids", req.getParameter("ids")); return new ModelAndView("com/jeecg/decmain/pub_excel_upload"); }
@RequestMapping(params = "upload3") public ModelAndView upload3(HttpServletRequest req) { req.setAttribute("controller_name","decOrderController"); req.setAttribute("method_name","importonlyone"); return new ModelAndView("com/jeecg/decorder/dec_excel_upload"); } @SuppressWarnings("unchecked") @RequestMapping(params = "importonlyone", method = RequestMethod.POST) @ResponseBody public AjaxJson importonlyone(HttpServletRequest request, HttpServletResponse response) throws Exception { AjaxJson j = new AjaxJson(); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; List<MultipartFile> contactFile= new ArrayList<MultipartFile>(); Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { MultipartFile file = entity.getValue();// 获取上传文件对象 contactFile.add(file); } XSSFWorkbook wb;//2007以前的是HSSFWorkbook // 页 XSSFSheet sheet;//成品表 // 行 XSSFRow row;//成品表行 // 打开文件 try { wb = new XSSFWorkbook(contactFile.get(0).getInputStream()); } catch (IOException e) { e.printStackTrace(); wb = new XSSFWorkbook(); } sheet = wb.getSheetAt(0); int rowNum = sheet.getLastRowNum(); for(int i=2;i<rowNum;i++){ XSSFCell cell = sheet.getRow(i).getCell(1); String cellFormatValue = getCellFormatValue(cell); if(StringUtils.isNotEmpty(cellFormatValue.trim())){ String invoiceno = getCellFormatValue(sheet.getRow(i).getCell(1)); for(int k =8;k<26;k++){ LdcOrderTaxEntity ldcordertaxentity = new LdcOrderTaxEntity(); String order_tax = getCellFormatValue(sheet.getRow(i).getCell(k));//费用 String order_tax_name = getCellFormatValue(sheet.getRow(1).getCell(k));//费用类型 ldcordertaxentity.setInvoiceno(invoiceno);//设置票号 ldcordertaxentity.setOrderTax(order_tax);//设置费用 ldcordertaxentity.setOrderTaxName(order_tax_name);//设置费用类型 if(StringUtils.isNotEmpty(order_tax.trim())){ ldcOrderTaxService.save(ldcordertaxentity);//保存 } } } } return j; } private String getCellFormatValue(XSSFCell xssfCell) { String cellvalue = ""; if (xssfCell != null) { // 判断当前Cell的Type switch (xssfCell.getCellType()) { // 如果当前Cell的Type为NUMERIC case XSSFCell.CELL_TYPE_NUMERIC: case XSSFCell.CELL_TYPE_FORMULA: { // 判断当前的cell是否为Date if (HSSFDateUtil.isCellDateFormatted(xssfCell)) { Date date = xssfCell.getDateCellValue(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); cellvalue = sdf.format(date); } // 如果是纯数字 else { // 取得当前Cell的数值 cellvalue = String.valueOf(xssfCell.getNumericCellValue()); } break; } // 如果当前Cell的Type为STRIN case XSSFCell.CELL_TYPE_STRING: // 取得当前的Cell字符串 cellvalue = xssfCell.getRichStringCellValue().getString(); break; // 默认的Cell值 default: cellvalue = " "; } } else { cellvalue = ""; } return cellvalue; }