java 读取excel/word存入mysql

news/2024/7/2 23:44:06

引入依赖

<!--poi-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.0.1</version>
        </dependency>
        <!--poi-->

excel 分为两个版本,一个是xlsx一个是xls

xlsx为高版本,xls为低版本

xlsx高版本

 @Override
    public void uploadExcel(MultipartFile file) throws IOException {

//    Workbook workbook = new XSSFWorkbook(new FileInputStream("D:\\xxx\\xxx.xlsx"));

        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        //获取excel中的指定表单,两种方法都可以
        // Sheet sheet =  workbook.getSheetAt(4);
        Sheet sheet = workbook.getSheet("工程项目投标报价汇总表");
        int lastRowNum = sheet.getLastRowNum();//当前sheet的最后一行的索引值

        //读取工作表的内容
        Row row = null;

        for (int i = 3; i <= lastRowNum; i++) {
            row = sheet.getRow(i);
            Bidding bidding = new Bidding();

            String tableId = row.getCell(0).getStringCellValue(); //序号
            bidding.setTableId(tableId);
            String projectName = row.getCell(1).getStringCellValue(); //项目或费用名称
            bidding.setProjectName(projectName);
            String amountM = row.getCell(2).getStringCellValue(); //金额
            bidding.setAmountM(amountM);
            String comment = row.getCell(3).getStringCellValue();  //备注
            bidding.setComment(comment);

            elemapper.uploadExcel(bidding);
        }
    }

xls低版本

//唯一不同为要使用HSSF创建
 Workbook workbook1 = new HSSFWorkbook(new FileInputStream(fullAddress));

word使用

@Override
    public void uploadWord(MultipartFile file) throws IOException {
        //读取文本
        XWPFDocument document = new XWPFDocument(file.getInputStream());

        List<XWPFTable> tables = document.getTables();

        List<XWPFTableRow> rows = null;
        List<XWPFTableCell> cells;
        List list = new ArrayList();
        //起始打印cell;
        int w = 17;
        //14一循环
        int l = 14;
        Construction construction = new Construction();
        for (XWPFTable table : tables) {
            rows = table.getRows();
            for (XWPFTableRow row : rows) {
                cells = row.getTableCells();
                for (XWPFTableCell cell : cells) {
                    list.add(cell.getText());
                }
            }
        }
        String packageNum = null;
        for (int i = w; i < list.size(); i += 14) {


            //下标写死就是一直取第一个值
            String submarkNum = (String) list.get(16);
            construction.setSubmarkNum(submarkNum);

            //写逻辑判断,把空值填上上一个


            if (!((String) list.get(i)).isEmpty()) {
                packageNum = (String) list.get(i);
                construction.setPackageNum(packageNum);

                while (((String) list.get(i)).isEmpty()) {

                    packageNum = (String) list.get(i - l);
                    construction.setPackageNum(packageNum);
                }

            }


            String projectCom = (String) list.get(i + 1);
            construction.setProjectCom(projectCom);


            String projectName = (String) list.get(i + 2);
            construction.setProjectName(projectName);


            String projectAbs = (String) list.get(i + 3);
            construction.setProjectAbs(projectAbs);


            String eleLevel = (String) list.get(i + 4);
            construction.setEleLevel(eleLevel);


            String projectPlan = (String) list.get(i + 5);
            construction.setProjectPlan(projectPlan);


            String projectNature = (String) list.get(i + 6);
            construction.setProjectNature(projectNature);


            String projectScale = (String) list.get(i + 7);
            construction.setProjectScale(projectScale);


            String methods = (String) list.get(i + 8);
            construction.setMethods(methods);


            String limitPrice = (String) list.get(i + 9);
            construction.setLimitPrice(limitPrice);


            String technologyId = (String) list.get(i + 10);
            construction.setTechnologyId(technologyId);


            String biddingFee = (String) list.get(i + 11);
            construction.setBiddingFee(biddingFee);


            String requestId = (String) list.get(i + 12);
            construction.setRequestId(requestId);


            elemapper.uploadWord(construction);
        }
    }

参考项目名elezip111


http://lihuaxi.xjx100.cn/news/1776479.html

相关文章

有关自动化的脚本思考 python 按键 javascript

start 说来其实挺巧的&#xff0c;去年年中的时候&#xff0c;有一个同组的同事&#xff0c;由于工作流程需要&#xff0c;经常会打开某一网页&#xff0c;填写某些信息&#xff0c;然后上传特定的代码。 他有一次和我闲聊&#xff0c;他吐槽说&#xff0c;他每天的时间会被这…

『MySQL快速上手』-⑦-基本查询

文章目录 1.Create 创建1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更新1.4 替换2. Retrieve 读取2.1 select 列2.1.1 全列查询2.1.2 指定列查询2.1.3 查询字段为表达式2.1.4 为查询结果指定别名2.1.5 结果去重2.2 where 条件2.2.1 英语不及格的同学及英语成…

2023.11.10 hadoop,hive框架概念,基础组件

目录 分布式和集群的概念: hadoop架构的三大组件:Hdfs,MapReduce,Yarn 1.hdfs 分布式文件存储系统 Hadoop Distributed File System 2.MapReduce 分布式计算框架 3.Yarn 资源调度管理框架 三个组件的依赖关系是: hive数据仓库处理工具 hive的大体流程: Apache hive的…

MATLAB算法实战应用案例精讲-【目标检测】机器视觉-工业相机参数

目录 前言 知识储备 相机镜头工作原理 算法原理 相机模型 坐标系

vue实战——路由访问权限【详解】

路由访问权限逻辑 有的页面无需用户登录即可查看&#xff0c;如登录页、注册页、找回密码页、主页、对外公开的其他页面等。有的页面需要用户登录后才能查看&#xff0c;如用户中心、博客编辑页等。 核心技术 Vue Router 路由守卫 https://blog.csdn.net/weixin_41192489/artic…

平安人寿基于 Apache Doris 统一 OLAP 技术栈实践

导读&#xff1a;平安人寿作为保险行业领军企业&#xff0c;坚持技术创新&#xff0c;以数据业务双轮驱动的理念和更加开放的思路来应对不断增长的数据分析和应用需求&#xff1b;以深挖数据价值、保障业务用数效率为目标持续升级大数据产品体系。自 2022 年起平安人寿开始引入…

LeetCode(4)删除有序数组中的重复项 II【数组/字符串】【中等】

目录 1.题目2.答案3.提交结果截图 链接&#xff1a; 80. 删除有序数组中的重复项 II 1.题目 给你一个有序数组 nums &#xff0c;请你** 原地** 删除重复出现的元素&#xff0c;使得出现次数超过两次的元素只出现两次 &#xff0c;返回删除后数组的新长度。 不要使用额外的数…

媒体聚焦丨四维图新旗下杰发科技王璐:设计决定芯片质量

编者按&#xff1a;新四化、软件定义汽车使汽车芯片成为了最新的半导体增长极&#xff0c;催生了汽车芯片的数量呈倍速增长&#xff0c;汽车芯片功能越来越复杂&#xff0c;迭代速度也越来越快。汽车芯片厂商从最初的设计开始&#xff0c;就要按照车规级芯片的要求对芯片进行全…