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

java 复制代码
@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为表格形式

相关推荐
周不易6 分钟前
ubuntu20.04+qt5.12.8安装serialbus
开发语言·c++·qt·modbus·serialbus
嘤国大力士10 分钟前
C++11&QT复习 (十七)
开发语言·c++·qt
.格子衫.44 分钟前
008二分答案+贪心判断——算法备赛
开发语言·c++·算法
小蒜学长1 小时前
机动车号牌管理系统设计与实现(代码+数据库+LW)
开发语言·数据库·spring boot·后端·spring·oracle
Pasregret1 小时前
11-Java并发编程终极指南:ThreadLocal与并发设计模式实战
java·开发语言·设计模式
Clocky72 小时前
opencv-python基础
开发语言·python
Zz_waiting.2 小时前
多线程进阶
java·开发语言·jvm·javaee
满怀10152 小时前
【Python Requests 库详解】
开发语言·python
来自星星的坤2 小时前
如何在 Postman(测试工具) 中实现 Cookie 持久化并保持同一会话
java·开发语言·spring boot·后端
佟格湾2 小时前
聊透多线程编程-线程池-6.C# APM(异步编程模型)
开发语言·后端·c#·多线程