1.导入
前端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"); }
@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(); DecMainPage decMainPage=new DecMainPage(); row=sheet.getRow(1); String ieflag=getCellFormatValue(row.getCell(1));//进出口标识
}
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;
}
2.导出
前台js:
<t:dgToolBar title="导出" icon="icon-putout" funname="newbaoguan"></t:dgToolBar>
function newbaoguan(title,url, id,width,height,isRestful) { gridname=id; var rowsData = $('#'+id).datagrid('getSelections'); if (!rowsData || rowsData.length==0) { tip('请选择导出项目'); return; } if (rowsData.length>1) { tip('请选择一条记录再导出'); return; } if (rowsData.length==1) { JeecgExcelExport("decMainController.do?newbaoguan&ids="+rowsData[0].id,"decMainList"); } }
后台java代码
@RequestMapping(params = "newbaoguan") public void newbaoguan(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap,OutputStream output) throws IOException { Workbook tempWorkBook = null; String lujing=request.getSession().getServletContext().getRealPath("/"); String lujing1=lujing+"export\template\newbaoguancus20180918.xls"; InputStream in = new FileInputStream(new File(lujing1)); HSSFWorkbook work = new HSSFWorkbook(in); HSSFSheet sheetModel =work.getSheetAt(0); HSSFSheet newSheet = work.createSheet("Sheet"+(i+2)); copySheet(work, sheetModel, newSheet, sheetModel.getFirstRowNum(), sheetModel.getLastRowNum()); HSSFCellStyle setBorder = (HSSFCellStyle)work.createCellStyle(); setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 HSSFCellStyle setBorder1 = (HSSFCellStyle)work.createCellStyle(); setBorder1.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 HSSFCellStyle setBorder2 = (HSSFCellStyle)work.createCellStyle(); setBorder2.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 HSSFRow row = newSheet.getRow(1); HSSFCell cell = row.getCell(1); cell.setCellValue(""); response.reset(); response.setCharacterEncoding("UTF-8"); response.setContentType("application/vnd.ms-excel"); //保证不乱码 try { Date date=new Date(); SimpleDateFormat format=new SimpleDateFormat("MMddHHmmss"); String time="im"+format.format(date)+".xls"; response.setHeader("Content-Disposition","attachment;" + " filename=" + new String(time.getBytes("utf-8"), "ISO-8859-1")); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { ByteArrayOutputStream oss =new ByteArrayOutputStream(); OutputStream os = response.getOutputStream(); work.write(oss); byte temp[] = oss.toByteArray(); ByteArrayInputStream in1 = new ByteArrayInputStream(temp); int n = 0; while ((n = in1.read(temp)) >0) { os.write(temp, 0, n); } os.flush(); os.close(); } catch(Exception e) { e.printStackTrace(); } }