77、java解析word表格,把数据入库
使用poi来解析word内容
    
     /**
     * 解析word doc   实测可以
     *
     * @param file
     * @return
     * @author LZH
     * @date 2023/3/24 15:50:06
     * @version 1.0
     */
public Result<?> analysisWord(MultipartFile file, HttpServletRequest request) throws Exception {
        AjCaseInfoDto aj = new AjCaseInfoDto();
        InputStream inputStream = file.getInputStream();
//        解析内容 doc 文档
        HWPFDocument document = new HWPFDocument(inputStream);
//        获取文档的范围
        Range range = document.getRange();
//        迭代文档中的表格
        TableIterator it = new TableIterator(range);
        String info = "";
        String cellString = "";
        if (it.hasNext()){
            TableRow tr = null;
            TableCell td = null;
            Paragraph para = null;
            Table tb = it.next();
            for (int i = 0; i < tb.numRows(); i++) {
                tr = tb.getRow(i);
                for (int j = 0; j < tr.numCells(); j++) {
                    int totalrow = tr.numCells();
                    td = tr.getCell(j);// 取得单元格
                    // 取得单元格的内容
                    cellString = td.text();
                    boolean flag = true;
                    if (cellString != null && cellString.compareTo("") != 0 && flag == true) {
                        // 如果不trim,取出的内容后会有一个乱码字符
                        cellString = cellString.trim().replaceAll("\\s*", "");
                    }
                    if ("填报单位:".equals(cellString)) {
                        String content = tr.getCell(++j).text().trim().replaceAll("\\s*", "");
                        if (ObjectUtil.isNotEmpty(content))
                        aj.setReportDepart(content);
                    }if ("填报日期:".equals(cellString)) {
                        String content = tr.getCell(++j).text().trim().replaceAll("\\s*", "");
                        if (ObjectUtil.isNotEmpty(content))
                        aj.setReportTime(parseDateString(content));
                    } else if ("案件编码".equals(cellString)) {
                        String content = tr.getCell(++j).text().trim().replaceAll("\\s*", "");
                        if (ObjectUtil.isNotEmpty(content))
                        aj.setCaseCode(content);
                    } else if ("涉案人员编码".equals(cellString)) {
                        String content = tr.getCell(++j).text().trim().replaceAll("\\s*", "");
                        if (ObjectUtil.isNotEmpty(content))
                        aj.setInvolvedPersonCode(content);
                    }
                 }
            }
        }
    //数据入库
    this.addOne(aj);
}

    /**
     * 解析word docx
     *
     * @param file
     * @return
     * @author LZH
     * @date 2023/3/24 15:50:06
     * @version 1.0
     */
public Result<?> analysisWord(MultipartFile file, HttpServletRequest request) throws Exception {
        AjCaseInfoDto aj = new AjCaseInfoDto();
        XWPFDocument doc = new XWPFDocument(is);
        List<XWPFParagraph> paras = doc.getParagraphs();
        List<XWPFRun> runs = paras.get(0).getRuns();
        for (int i = 0; i < runs.size(); i++) {
            runs.get(i);
        }
        for (XWPFRun run : runs) {
            String text = run.getText(0);
        }
        for (XWPFParagraph para : paras) {//当前段落的属性
            String text = para.getText();
        }
        List tables = doc.getTables();
        XWPFTable xwpf = (XWPFTable) tables.get(0);
        String text = xwpf.getText();
        CTTblPr pr = xwpf.getCTTbl().getTblPr();
        List<XWPFTableRow> rows = xwpf.getRows();
        for (XWPFTableRow row : rows) {
            List<XWPFTableCell> tableCells = row.getTableCells();
            for (int i = 0; i < tableCells.size(); i++) {
                if (tableCells.get(i).getText().equals("填报单位:")) {
                    aj.setReportDepart(tableCells.get(++i).getText());
                } else if (tableCells.get(i).getText().equals("填报日期:")) {
                    aj.setReportTime(tableCells.get(++i).getText());
                } else if (tableCells.get(i).getText().equals("案件编码")) {
                    aj.setCaseCode(tableCells.get(++i).getText());
                } else if (tableCells.get(i).getText().equals("涉案人员编码")) {
                    aj.setInvolvedPersonCode(tableCells.get(++i).getText());
                } 
            }
        }
    //数据入库
    this.addOne(aj);
}

  /**
     * 格式化date
     *
     * @param date
     * @return
     * @author LZH
     * @date 2023/3/27 18:28:55
     * @version 1.0
     */
    Date parseDateString(String date){
        if (date.contains("/"))
            date = date.replace("/","-");
        if (ObjectUtil.isNotEmpty(date)){
            if (date.length()>7){
                return DateUtil.parse(date,"yyyy-MM-dd");
            }else {
                String[] arr = new String[0];
                if (date.contains("-")){
                    arr = date.split("-");
                }
                if (arr.length > 1){
//					年份--月份
                    if (!arr[1].contains("0") && arr[1].length() == 1)
                        date = arr[0]+"-0"+arr[1];
                    return DateUtil.parse(date,"yyyy-MM");
                }else {
//					年份
                    return DateUtil.parse(date,"yyyy");
                }

            }
        }
        return null;
    }
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐