导入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为表格形式

相关推荐
Chester_19994 小时前
CSP202203C.计算资源调度器
开发语言·数据结构·c++·蓝桥杯
EXI-小洲5 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
会周易的程序员6 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计
开发语言·c++·虚拟机·软plc·iec61131·stvm
机器视觉知识推荐、就业指导6 小时前
为什么老说“纯 Qt 没啥就业市场”?
开发语言·qt
telepan6 小时前
Qt 开发避坑与性能指南:如何优雅、安全地定义全局常量字符串?
开发语言·qt
m0_695251496 小时前
Qt MaintenanceTool 使用国内镜像加速下载(超详细教程)
开发语言·qt
大衛說7 小时前
《Python 从入门到精通》系列总览与学习路线
开发语言·python·学习
可乐鸡翅yeah_9 小时前
hls.js 内存泄漏实战排查,直播 M3U8 长时间播放异常定位方案
开发语言·javascript·python·django·ecmascript·m3u8·m3u8在线
2601_962203519 小时前
Java进阶07集合(续)
java·开发语言
郑州光合科技余经理9 小时前
本地生活服务系统:模块边界与结算字段怎么拆
java·开发语言·前端·后端·系统架构·uni-app·php