导入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 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1234 天前
matlab画图工具
开发语言·matlab
dustcell.4 天前
haproxy七层代理
java·开发语言·前端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054964 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月4 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237174 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian4 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript
雪人不是菜鸡4 天前
简单工厂模式
开发语言·算法·c#