java 读取excel/word存入mysql

引入依赖

XML 复制代码
<!--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高版本

java 复制代码
 @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低版本

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

word使用

java 复制代码
@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

相关推荐
Full Stack Developme1 天前
Java后台生成多个Excel并用Zip打包下载
java·开发语言·excel
芦骁骏2 天前
自动处理考勤表——如何使用Power Query,步步为营,一点点探索自定义函数
数据分析·excel·powerbi
用户8356290780513 天前
使用 C# 将 DataTable 写入 Excel(基于 Spire.XLS for .NET)
excel
迪尔~3 天前
Apache POI中通过WorkBook写入图片后出现导出PDF文件时在不同页重复写入该图片问题,如何在通过sheet获取绘图对象清除该图片
java·pdf·excel
瓶子xf5 天前
使用Excel制作甘特图
excel·甘特图
战族狼魂5 天前
Excel 连接阿里云 RDS MySQL
mysql·阿里云·云计算·excel
cypking5 天前
vue excel转json功能 xlsx
vue.js·json·excel
专注VB编程开发20年5 天前
C#教程之NPOI读写excel文件XLS,XLSX格式
数据库·microsoft·c#·excel·xlsx·xls
YC运维5 天前
WEB虚拟主机3种部署方式全解析
excel
Dxy12393102168 天前
Python如何合并两个Excel文件
爬虫·python·excel