你的位置:首页 > Java教程

[Java教程]excel上传与下载


后台:  
     @RequestMapping(value = "/uploadFile.do", method = RequestMethod.POST)
 public String uploadFile(
   @RequestParam("questionFile") MultipartFile uploadFile,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  String template = request.getParameter("template");
  System.out.println(uploadFile.getName());
  response.setCharacterEncoding("UTF-8");
  response.setContentType("text/html; charset=utf-8");
  OutputStreamWriter os = new OutputStreamWriter(
    response.getOutputStream(), "UTF-8");
  PrintWriter pw = new PrintWriter(os);
  List<List<String>> qList = ExcelUtil.resolveExcelFileToList(uploadFile
    .getInputStream());
  try {
   infoService.importInfoByFile(pw, qList,template);
  } catch (Exception e) {
   e.printStackTrace();
   String info = "导入文件出错,请核实情况后重新导入";
   pw.println("<script>window.parent.pa(\"" + info
     + "\")</script>");
   pw.flush();
  }
  pw.close();
  response.flushBuffer();
  return null;
 }    
 
 
       @Override
 public void importInfoByFile(PrintWriter pw, List<List<String>> list,
   String template) throws Exception {
  if("bjxx".equals(template)){
   for (List<String> colList : list) {
    PriceInfoEO price = new PriceInfoEO();
    price.setStockCode(colList.get(0));
    price.setStockName(colList.get(1));
    try{
     price.setCreateDate(DateHelper.parse(colList.get(2), "yyyy-MM-dd"));
    }catch (Exception e) {
     String dateStr = colList.get(2);
     String y = dateStr.substring(0, 4);
     String m = dateStr.substring(4, 6);
     String d = dateStr.substring(6, 8);
     price.setCreateDate(DateHelper.parse(y + "-" + m + "-" + d,"yyyy-MM-dd"));
    }
   
    price.setCommissionType(colList.get(3));
    price.setCommissionCount(colList.get(4));
    price.setCommissionMoney(colList.get(5));
    price.setTel(colList.get(6));
    price.setStatus("1");
    baseDao.saveOrUpdate(price);
   }
   String info = "导入完成";
   pw.println("<script>window.parent.pa(\"" + info
     + "\")</script>");
   pw.flush();
  }
  else if("dxzf".equals(template)){
   for (List<String> colList : list) {
    ComDxzfEO com = new ComDxzfEO();
    com.setGqdm(colList.get(0));
    com.setSj(colList.get(1));
    com.setZfjg(colList.get(2));
    com.setYjzfsl(colList.get(3));
    com.setSjzfsl(colList.get(4));
    com.setSjmjzj(colList.get(5));
    baseDao.saveOrUpdate(com);
   }
   String info = "导入完成";
   pw.println("<script>window.parent.pa(\"" + info
     + "\")</script>");
   pw.flush();
  }
  else if("fhkg".equals(template)){
   for (List<String> colList : list) {
    ComFhkgEO com = new ComFhkgEO();
    com.setGqdm(colList.get(0));
    com.setGgrq(DateHelper.parse(colList.get(1), "yyyy-MM-dd"));
    com.setFasm(colList.get(2));
    com.setGqdjr(DateHelper.parse(colList.get(3), "yyyy-MM-dd"));
    com.setGqcxr(DateHelper.parse(colList.get(4), "yyyy-MM-dd"));
    com.setQydzr(DateHelper.parse(colList.get(5), "yyyy-MM-dd"));
    baseDao.saveOrUpdate(com);
   }
   String info = "导入完成";
   pw.println("<script>window.parent.pa(\"" + info
     + "\")</script>");
   pw.flush();
  }
  else if("ggry".equals(template)){
   for (List<String> colList : list) {
    LeaderEO com = new LeaderEO();
    com.setGfdm(colList.get(0));
    com.setYears(colList.get(1));
    com.setName(colList.get(2));
    com.setPosition(colList.get(3));
    com.setAge(colList.get(4));
    com.setSex(colList.get(5));
    com.setBeginDate(colList.get(6));
    com.setQccgs(colList.get(7));
    com.setQmcgs(colList.get(8));
    com.setGfzjs(colList.get(9));
    com.setSflx(colList.get(10));
    com.setOrderNum(Integer.parseInt(colList.get(11)));
    baseDao.saveOrUpdate(com);
   }
   String info = "导入完成";
   pw.println("<script>window.parent.pa(\"" + info
     + "\")</script>");
   pw.flush();
  }
  else if("cwzb".equals(template)){
   for (List<String> colList : list) {
    ComCwzbEO com = new ComCwzbEO();
    com.setGqdm(colList.get(0));
    com.setYears(colList.get(1));
    com.setYysr(colList.get(2));
    com.setYylr(colList.get(3));
    com.setJlr(colList.get(4));
    com.setWfplr(colList.get(5));
    com.setZzc(colList.get(6));
    com.setZfz(colList.get(7));
    com.setJzc(colList.get(8));
    com.setMgsy(colList.get(9));
    com.setMgjzc(colList.get(10));
    com.setJzcsyl(colList.get(11));
    baseDao.saveOrUpdate(com);
   }
   String info = "导入完成";
   pw.println("<script>window.parent.pa(\"" + info
     + "\")</script>");
   pw.flush();
  }
  else if("zygd".equals(template)){
   for (List<String> colList : list) {
    ComGdEO com = new ComGdEO();
    com.setGqdm(colList.get(0));
    com.setNf(Integer.parseInt(colList.get(1)));
    com.setJzrq(DateHelper.parse(colList.get(2), "yyyy-MM-dd"));
    com.setGd(colList.get(3));
    com.setCgzs(colList.get(4));
    com.setWxsgf(colList.get(5));
    com.setCgbl(colList.get(6));
    com.setOrders(colList.get(7));
    baseDao.saveOrUpdate(com);
   }
   String info = "导入完成";
   pw.println("<script>window.parent.pa(\"" + info
     + "\")</script>");
   pw.flush();
  }
  else if("qbhuiyuan".equals(template)){
   List<ArticleEO> artList = new ArrayList<ArticleEO>();
   for(List<String> colList : list){
    String temp = "";
    temp = temp + "<table height='165' width='453' border='1' bordercolor='#000000' cellpadding='2' cellspacing='0'><tbody>";
    temp = temp + "<tr><td width='30%'>公司名称<br /></td><td>" + colList.get(1) + "</td></tr>";
    temp = temp + "<tr><td>注册资本<br /></td><td>" + colList.get(2) + "</td></tr>";
    temp = temp + "<tr><td>联系人<br /></td><td>" + colList.get(3) + "</td></tr>";
    temp = temp + "<tr><td>电话<br /></td><td>" + colList.get(4) + "</td></tr>";
    temp = temp + "<tr><td>地址<br /></td><td>" + colList.get(5) + "</td></tr>";
    temp = temp + "<tr><td>邮编<br /></td><td>" + colList.get(6) + "</td></tr>";
    temp = temp + "<tr><td>邮箱<br /></td><td>" + colList.get(7) + "</td></tr></tbody></table>";
 
 
    ArticleEO art = new ArticleEO();
    art.setTitle(colList.get(1));
    art.setContent(temp);
    art.setArticleType(Constant.ARTICLE_TYPE_SALE_SERVICE_USER_2);
    art.setStatus(Constant.STATUS_1);
    art.setUserType(Constant.USER_TYPE_3);
    art.setPublishDate(DateHelper.parse(colList.get(0)));
    art.setDisabledDate(DateHelper.parse("2099-12-31"));
    art.setIsShowIndex("0");
    art.setOrderNum(99);
    artList.add(art);
   }
   baseDao.saveOrUpdateAll(artList);
   String info = "导入完成";
   pw.println("<script>window.parent.pa(\"" + info
     + "\")</script>");
   pw.flush();
  }
 }
前端:
<div id="upload_down"   title="会员数据导入"
        data-options="modal:true,collapsible:false,minimizable:false,closed:true,selectOnCheck:false">
     <form name="uploadForm" method="post" id="upload" target="frameFile" action="<%=basePath %>uploadFile.do?template=qbhuiyuan" enctype="multipart/form-data">
   <fieldset >
         <legend>导入与模板下载:</legend>
          <div >
           <div >
          <input id="questionFile" type="file" name="questionFile" />
         </div>
         <div >
          <span id="upload_process" ></span>
         </div>
        <div >
         <input type="button" onClick="downLoad();" value="下载模板" id="Button1" />
         <input type="button" onClick="upload();"  value="导入" id="Button1" />
        </div>
          </div>
      </fieldset>
     </form>
    </div>