java poi读取word树形表格

//melon-树形word进数据库
    @RequestMapping(value = "/test_word", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String test_word(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return NR.r(CodeDict.CODE_MESSAGE, -1, CodeDict.UPLOAD_EMPTY, null, null, 0, 0);
        } else {
            String fileName = setFile(file, "CheckItemWord\\");
            String path = System.getProperty("catalina.home") + "\\webapps\\CheckItemWord\\" + fileName;
            uploadService.testWord(path);
            return NR.r(CodeDict.CODE_MESSAGE, 1, 0, null, null, 0, 0);
        }
    }

操作整个word:

 public void testWord(String path) throws IOException {
        InputStream is = new FileInputStream(path);
        XWPFDocument doc = new XWPFDocument(is);
        List<XWPFParagraph> paras = doc.getParagraphs();
        for (XWPFParagraph para : paras) {
            //当前段落的属性
//       CTPPr pr = para.getCTP().getPPr();
           // System.out.println("段落属性:"+para.getText());
        }
        //获取文档中所有的表格
        List<XWPFTable> tables = doc.getTables();
        List<XWPFTableRow> rows;
        List<XWPFTableCell> cells;
        for (XWPFTable table : tables) {
            //表格属性
//       CTTblPr pr = table.getCTTbl().getTblPr();
            //获取表格对应的行
            rows = table.getRows();
            System.out.println(table.getRow(0));
            for (XWPFTableRow row : rows) {
                //获取行对应的单元格
                cells = row.getTableCells();
                
                for (XWPFTableCell cell : cells) {
                    System.out.println(cell.getText());
                }
            }
        }
        //关闭流
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
public void testWord(String path) throws IOException {
        List<HSEImportDto> lists = new ArrayList<>();//接收从Excel中读出来映射的实体
        HSEImportDto hSEImportDto = new HSEImportDto();
        HashMap<String, String> result = new HashMap<>();//存放最终结果
        HashSet<HSEImportMapDto>hs=new HashSet<>();//存放最终放入数据库的集合
        HashMap<String, Integer> index1 = new HashMap<>();//判断子节点序号大小
        HashMap<String, Integer> index2 = new HashMap<>();//判断子节点序号大小
        HashMap<String, Integer> index3 = new HashMap<>();//判断子节点序号大小
        HashMap<String, String> mid = new HashMap<>();//存放最后一级节点的值
        String s11="";String s22="";String s33="";
        int num=0;
        InputStream is = new FileInputStream(path);
        XWPFDocument doc = new XWPFDocument(is);
        List<XWPFTable> tables = doc.getTables();//获取文档中所有的表格
        List<XWPFTableRow> rows;
       // List cells;
        for (XWPFTable table : tables) { //获取表格对应的行
            rows = table.getRows();
            for (XWPFTableRow row : rows) {
                if("".equals(row.getCell(0).getText())) hSEImportDto.setTheme(s11);//第一单元格
                else {
                    hSEImportDto.setTheme(row.getCell(0).getText());
                    s11=row.getCell(0).getText();//存入s11
                }
                if("".equals(row.getCell(1).getText())) hSEImportDto.setItems(s22);//第二单元格
                else {
                    hSEImportDto.setItems(row.getCell(1).getText());
                    s22=row.getCell(1).getText();//存入s22
                }
                if("".equals(row.getCell(2).getText())) hSEImportDto.setContent(s33);//第三单元格
                else {
                    hSEImportDto.setContent(row.getCell(2).getText());
                    s33=row.getCell(2).getText();//存入s33
                }
                    hSEImportDto.setAssessment(row.getCell(3).getText());
                    hSEImportDto.setInstructions(row.getCell(4).getText());
                lists.add(hSEImportDto);
                for (HSEImportDto pojo : lists) {
                    if (!result.containsKey(pojo.getTheme())) {           //第一层集合中没有的话添加
                        result.put(pojo.getTheme(), "0" + num);//放入全局map中
                        num++;
                        index1.put(pojo.getTheme(), 0);//判断本身有几个孩子节点
                    }
                    if (!result.containsKey(pojo.getItems())) {         //第二级结果中没有
                        Integer s = index1.get(pojo.getTheme());//得到编号
                        index1.put(pojo.getTheme(), s + 1);//编号加一放回
                        result.put(pojo.getItems(), result.get(pojo.getTheme()) + "0" + (s + 1));
                        index2.put(pojo.getItems(), 0);//判断本身有几个孩子节点
                    }
                    if(!result.containsKey(pojo.getContent())){        //第三级没有
                        Integer s1 = index2.get(pojo.getItems());//得到编号
                        index2.put(pojo.getItems(), s1+1);//编号加一放回
                        result.put(pojo.getContent(), result.get(pojo.getItems())+"0"+(s1+1));
                        index3.put(pojo.getContent(), 0);//判断本身有几个孩子节点
                    }
                    if(!result.containsKey(pojo.getAssessment())){      //第四级没有
                        Integer s2 = index3.get(pojo.getContent());//得到编号
                        index3.put(pojo.getContent(), s2+1);//编号加一放回
                        result.put(pojo.getAssessment(), result.get(pojo.getContent())+"0"+(s2+1));
                        mid.put(pojo.getAssessment(),pojo.getInstructions());
                    }
                }
            }
        }

        if (is != null) {//关闭流
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //遍历map
        for (Map.Entry<String, String> entry : result.entrySet()) {
            HSEImportMapDto hSEImportMapDto=new HSEImportMapDto();
            hSEImportMapDto.setStatus("启用");
            hSEImportMapDto.setType("HSE审核");
            hSEImportMapDto.setHseAuditSystemCode(entry.getValue());
            hSEImportMapDto.setEvalueItem(entry.getKey());
            if(mid.containsKey(entry.getKey())) hSEImportMapDto.setEvalueDescritption(mid.get(entry.getKey()));
            hs.add(hSEImportMapDto);
        }
        List<HSEImportMapDto> pojos=new ArrayList<>();
        pojos.addAll(hs);
        //批量插入数据库
        checkListDao.banchHSEImportMap(pojos);
    }

仅供参考,作者自己使用

你可能感兴趣的:(java,java,poi)