已解决
java 读取excel/word存入mysql
来自网友在路上 170870提问 提问时间:2023-11-12 00:08:06阅读次数: 70
最佳答案 问答题库708位专家为你答疑解惑
引入依赖
<!--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
查看全文
99%的人还看了
相似问题
- flink1.13.6版本的应用程序(maven版)
- 技术分享| gcc版本升级到5.2
- 基于 Glibc 版本升级的 DolphinDB 数据查询性能优化实践
- 解决requests 2.28.x版本SSL错误:证书验证失败
- UE5和UE4版本更新重大改变汇总。
- Docker build报错总结,版本过新大避雷!
- K8S1.23.5部署(此前1.17版本步骤囊括)及问题记录
- Mongodb3.4升级高版本mongoTemplate.executeCommand报错The cursor option is required
- onnx模型转换opset版本和固定动态输入尺寸
- centos安装指定版本docker
猜你感兴趣
版权申明
本文"java 读取excel/word存入mysql":http://eshow365.cn/6-38029-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: Redis的内存淘汰策略分析
- 下一篇: 笔记:AI量化策略开发流程-基于BigQuant平台(一)