导入word模板的数据到DB,偏自学,可自改套用

@GetMapping("/importTestPeople")
    public void importTestPeople(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        InputStream inputStream = null;
        File file = null;
        try {

            // 创建临时文件
            file = File.createTempFile("temp", null);
            // 把multipartFile写入临时文件
            multipartFile.transferTo(file);
            // 使用文件创建 inputStream 流
            inputStream = new FileInputStream(file);

            //读取Word文档
            XWPFDocument document = new XWPFDocument(inputStream);

            List<StringBuffer> joinList = new ArrayList<>();

            // 获取文档中的所有表格
            List<XWPFTable> tables = document.getTables();

            for (XWPFTable table : tables) {
                // 获取表格的行
                List<XWPFTableRow> rows = table.getRows();
                // 遍历每一行
                for (XWPFTableRow row : rows) {
                    // 获取行中的单元格
                    List<XWPFTableCell> cells = row.getTableCells();
                    //存入数据
                    StringBuffer stringBuffer = new StringBuffer();
                    // 遍历每个单元格
                    for (XWPFTableCell cell : cells) {
                        // 输出单元格的文本内容
                        System.out.print(cell.getText() + "|");
                        stringBuffer.append(cell.getText() + "|");
                    }
                    joinList.add(stringBuffer);
                    System.out.println(); // 换行
                }
                System.out.println(); // 表格间换行
            }

            for(int i = 0; i<joinList.size();i ++){
                String[] split = joinList.get(i).toString().split("\\|");
                ManualModeInfomation manualModeInfomation = new ManualModeInfomation();
                manualModeInfomation.setId(Integer.valueOf(split[0]));
                manualModeInfomation.setModelId(2);
                //父级
                manualModeInfomation.setFatherLevel(split[1]);
                //子级
                manualModeInfomation.setChildLevel(split[2]);
                //故障现象
                manualModeInfomation.setFaultPhenomenon(split[4]);
                //主要原因
                manualModeInfomation.setMainCause(split[5]);
                //解决方法
                manualModeInfomationMapper.insert(manualModeInfomation);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 最后记得删除文件
            file.deleteOnExit();
            // 关闭流
            inputStream.close();
        }

说明:应用方面是word版且为表格,可解析,能看懂的自然会微调,感谢大家!!!

如:需word为表格形式

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值