当前位置: 首页 > news >正文

java 读取excel/word存入mysql

引入依赖

<!--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高版本

 @Overridepublic 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使用

@Overridepublic 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://www.lryc.cn/news/226284.html

相关文章:

  • 11.(vue3.x+vite)组件间通信方式之ref与$parent、$children
  • [工业自动化-12]:西门子S7-15xxx编程 - PLC从站 - ET200 SP系列详解
  • 消息队列简介
  • SQL中实现汉字的拼音首字母查询
  • 今天知道LiveData的ktx是真的香
  • SpringBoot中的桥接模式
  • AI爆文变现脚本:易用且免费的自动写作脚本更新了
  • 代码随想录算法训练营Day 49 || 123.买卖股票的最佳时机III 、188.买卖股票的最佳时机IV
  • threejs(11)-精通着色器编程(难点)2
  • 配置cuda和cudnn出现 libcudnn.so.8 is not a symbolic link问题
  • “目标值排列匹配“和“背包组合问题“的区别和leetcode例题详解
  • 火星加载WMTS服务
  • 为什么要学习去使用云服务器,外网 IP能干什么,MAC使用Termius连接阿里云服务器。保姆级教学
  • VS c++多文件编译
  • JVM关键指标监控(调优)
  • 【Proteus仿真】【Arduino单片机】LCD1602-IIC液晶显示
  • skynet学习笔记03— 服务
  • 34 Feign最佳实践
  • 软文推广中如何搭建媒体矩阵
  • Unity地面交互效果——5、角色足迹的制作
  • Centos8安装出错问题
  • 计算机网络技术
  • 当电脑桌面黑屏,而你只有一个鼠标该怎么办(重启方法的平替)
  • Leetcode2833. 距离原点最远的点
  • chrome 的vue3的开发者devtool不起作用
  • Redis数据结构七之listpack和quicklist
  • 单词规律问题
  • 蓝桥杯每日一题2023.11.8
  • 高级PHP应用程序漏洞审核技术【一】
  • 适用于4D毫米波雷达的目标矩形框聚类