如何使用jxls上载和更新设置的DB .xls或.xlsx文件

时间:2015-11-18 06:33:20

标签: java oracle gxt jxls

我使用了gxt2.5,java1.7,oracle 11g,MyBatis。 我想使用jxsl上传和更新到数据库中。

excel.xsl或excel.xlsx http://i.stack.imgur.com/Bk5As.jpg

mapping.xml

<?xml version="1.0" encoding="UTF-8"?>
<workbook>
  <worksheet name="Sheet1">
     <loop startRow="3" endRow="3" items="listFile" var="listFile" 
        varType="com.app.test.server.vo.listVO"
        <section startRow="3" endRow="3">
           <mapping row="3"col="0">listFile.a</mapping>
           <mapping row="3" col="1">listFile.b</mapping>
           <mapping row="3" col="2">listFile.c</mapping>
           <mapping row="3" col="3">listFile.d</mapping>
           <mapping row="3" col="4">listFile.e</mapping>
           <mapping row="3" col="5">listFile.f</mapping>
           <mapping row="3" col="6">listFile.g</mapping>                          
        </section>
      <loopbreakcondition>
         <rowcheck offset="0">
            <cellcheck offset="0">listFile.b</cellcheck>
         </rowcheck>
      </loopbreakcondition>
    </loop>
 </worksheet>

我不知道loopbreakcondition。 当listFile.b为null时我想要中断。因为,listFile.b是PK。

controller.java

private ClassPathResource uploadFileTemplate = new ClassPathResource("mapping.xml", getClass());

@RequestMapping(value = "/imp/XlsListImport.do")
public String handleFormUpload(@RequestParam("file") MultipartFile file,
        @ModelAttribute("listVO") listVO listVo,
        Model model, final HttpServletRequest request) throws InvalidFormatException, IOException,
        SAXException {

    listVo = new listVO();
    model.addAttribute(listVo);


    if  (listExcelService == null) { 
       WebApplicationContext wac = WebApplicationContextUtils.
               getRequiredWebApplicationContext(request.getSession().getServletContext());

      listExcelService = (ListExcelService) wac.getBean("ListExcelService" ); 
    }

    final MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
    final Map<String, MultipartFile> files = multiRequest.getFileMap();

    InputStream fis = null; 

    String sResult = "";

    Iterator<Entry<String, MultipartFile>> itr = files.entrySet().iterator();

    while (itr.hasNext()) {

        Entry<String, MultipartFile> entry = itr.next();

        file = entry.getValue();
            if (!"".equals(file.getOriginalFilename())) {
                if (file.getOriginalFilename().endsWith(".xls")
                        || file.getOriginalFilename().endsWith(".XLS") ) {                      
                    try {
                        fis = file.getInputStream();
                        excelService.updateXls(fis);
                        sResult =  "success";
                    } catch(Exception e) {
                          e.printStackTrace();
                        sResult = "error";
                    } finally {
                        if (fis != null)    
                            fis.close();
                    }
                } else  if (  file.getOriginalFilename().endsWith(".xlsx")
                        || file.getOriginalFilename().endsWith(".XLSX")) {

                    try {
                        fis = file.getInputStream();
                        excelService.updateXls(fis);
                        sResult =  "success";

                    } catch(Exception e) {
                          e.printStackTrace();
                        sResult = "error";

                    } finally {
                        if (fis != null)    
                            fis.close();
                    }
                }else{
                    logger.info(" Only xls, xlsx... check file extension"); 
                    sResult =  "success";
                }
            }
        }
    listExcelRead.read(uploadFileTemplate, file, model);

    System.out.println(listVo);
    System.out.println(listVo.getListVo());

    return sResult;
}

listExcelRead.java

    public XLSReadStatus read(Resource templateFile, MultipartFile file,
     Model model) throws InvalidFormatException, IOException, SAXException {

    return read(templateFile, file, model.asMap());
    }


public XLSReadStatus read(Resource templateFile, MultipartFile file,
        Map<?, ?> model) throws InvalidFormatException, IOException, SAXException {

    InputStream inputXML = null;
    InputStream inputXLS = null;

    try {
        inputXML = new BufferedInputStream(templateFile.getInputStream());
        XLSReader mainReader = ReaderBuilder.buildFromXML(inputXML);

        inputXLS = new BufferedInputStream(file.getInputStream());

        return mainReader.read(inputXLS, model);

    } finally {
        IOUtils.closeQuietly(inputXML);
        IOUtils.closeQuietly(inputXLS);
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用以下循环中断条件

<loopbreakcondition>
     <rowcheck offset="0">
        <cellcheck offset="1"/>
     </rowcheck>
  </loopbreakcondition>

因此,您指示检查listFile.b字段所在的B列(offset = 1)中的单元格,并在此单元格中的值为空时中断循环。