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

相关推荐
愿你天黑有灯下雨有伞16 小时前
Java使用FastExcel实现Excel文件导入
java·excel
爆爆凯16 小时前
Excel 导入导出工具类文档
java·excel
凌康ACG1 天前
springboot打包二次压缩Excel导致损坏
spring boot·后端·excel
诸葛大钢铁2 天前
Excel转PDF的三种方法
笔记·职场和发展·pdf·excel
小小薛定谔2 天前
java操作Excel两种方式EasyExcel 和POI
java·python·excel
CodeCraft Studio2 天前
DHTMLX Suite 9.2 重磅发布:支持历史记录、类Excel交互、剪贴板、拖放增强等多项升级
javascript·excel·交互·表格·dhtmlx·grid·网格
小阳睡不醒2 天前
小白成长之路-Elasticsearch 7.0 配置
大数据·elasticsearch·excel
奋进的孤狼2 天前
【Excel】使用vlookup函数快速找出两列数据的差异项
excel
不讲废话的小白2 天前
解锁高效Excel技能:摆脱鼠标,快速编辑单元格
计算机外设·excel
CodeCraft Studio2 天前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中创建甘特图
python·excel·项目管理·甘特图·aspose·aspose.cells