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

相关推荐
兮动人4 分钟前
Java 单元测试中的 Mockito 使用详解与实战指南
java·开发语言·单元测试
武子康12 分钟前
Java-151 深入浅出 MongoDB 索引详解 性能优化:慢查询分析 索引调优 快速定位并解决慢查询
java·开发语言·数据库·sql·mongodb·性能优化·nosql
患得患失9491 小时前
【Turborepo】【Next】 Turborepo + Next.js 前后端精简搭建笔记(带官网)
开发语言·javascript·笔记
拓端研究室1 小时前
视频讲解|Python遗传算法GA在车辆路径规划VRP数据优化中的应用
开发语言·人工智能·r语言
xxxxxxllllllshi2 小时前
Cookie、Session、JWT、SSO,网站与 APP 登录持久化与缓存
java·开发语言·jvm·数据结构·缓存·面试
AI+程序员在路上2 小时前
QT6中QToolBox功能与应用
开发语言·qt
aerror2 小时前
json转excel xlsx文件
开发语言·python·json
workflower3 小时前
软件工程的知识领域
开发语言·算法·软件工程·个人开发·结对编程
JoannaJuanCV3 小时前
error: can‘t find Rust compiler
开发语言·后端·rust
郏国上3 小时前
node.js上传图片接口
开发语言·node.js