当前位置:   article > 正文

java 读取excel/word存入mysql_java如何批量将woord文件自动生成mysql表

java如何批量将woord文件自动生成mysql表

引入依赖

  1. <!--poi-->
  2. <dependency>
  3. <groupId>org.apache.poi</groupId>
  4. <artifactId>poi</artifactId>
  5. <version>4.0.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.poi</groupId>
  9. <artifactId>poi-ooxml</artifactId>
  10. <version>4.0.1</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.poi</groupId>
  14. <artifactId>poi-ooxml-schemas</artifactId>
  15. <version>4.0.1</version>
  16. </dependency>
  17. <!--poi-->

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

xlsx为高版本,xls为低版本

xlsx高版本

  1. @Override
  2. public void uploadExcel(MultipartFile file) throws IOException {
  3. // Workbook workbook = new XSSFWorkbook(new FileInputStream("D:\\xxx\\xxx.xlsx"));
  4. Workbook workbook = new XSSFWorkbook(file.getInputStream());
  5. //获取excel中的指定表单,两种方法都可以
  6. // Sheet sheet = workbook.getSheetAt(4);
  7. Sheet sheet = workbook.getSheet("工程项目投标报价汇总表");
  8. int lastRowNum = sheet.getLastRowNum();//当前sheet的最后一行的索引值
  9. //读取工作表的内容
  10. Row row = null;
  11. for (int i = 3; i <= lastRowNum; i++) {
  12. row = sheet.getRow(i);
  13. Bidding bidding = new Bidding();
  14. String tableId = row.getCell(0).getStringCellValue(); //序号
  15. bidding.setTableId(tableId);
  16. String projectName = row.getCell(1).getStringCellValue(); //项目或费用名称
  17. bidding.setProjectName(projectName);
  18. String amountM = row.getCell(2).getStringCellValue(); //金额
  19. bidding.setAmountM(amountM);
  20. String comment = row.getCell(3).getStringCellValue(); //备注
  21. bidding.setComment(comment);
  22. elemapper.uploadExcel(bidding);
  23. }
  24. }

xls低版本

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

word使用

  1. @Override
  2. public void uploadWord(MultipartFile file) throws IOException {
  3. //读取文本
  4. XWPFDocument document = new XWPFDocument(file.getInputStream());
  5. List<XWPFTable> tables = document.getTables();
  6. List<XWPFTableRow> rows = null;
  7. List<XWPFTableCell> cells;
  8. List list = new ArrayList();
  9. //起始打印cell;
  10. int w = 17;
  11. //14一循环
  12. int l = 14;
  13. Construction construction = new Construction();
  14. for (XWPFTable table : tables) {
  15. rows = table.getRows();
  16. for (XWPFTableRow row : rows) {
  17. cells = row.getTableCells();
  18. for (XWPFTableCell cell : cells) {
  19. list.add(cell.getText());
  20. }
  21. }
  22. }
  23. String packageNum = null;
  24. for (int i = w; i < list.size(); i += 14) {
  25. //下标写死就是一直取第一个值
  26. String submarkNum = (String) list.get(16);
  27. construction.setSubmarkNum(submarkNum);
  28. //写逻辑判断,把空值填上上一个
  29. if (!((String) list.get(i)).isEmpty()) {
  30. packageNum = (String) list.get(i);
  31. construction.setPackageNum(packageNum);
  32. while (((String) list.get(i)).isEmpty()) {
  33. packageNum = (String) list.get(i - l);
  34. construction.setPackageNum(packageNum);
  35. }
  36. }
  37. String projectCom = (String) list.get(i + 1);
  38. construction.setProjectCom(projectCom);
  39. String projectName = (String) list.get(i + 2);
  40. construction.setProjectName(projectName);
  41. String projectAbs = (String) list.get(i + 3);
  42. construction.setProjectAbs(projectAbs);
  43. String eleLevel = (String) list.get(i + 4);
  44. construction.setEleLevel(eleLevel);
  45. String projectPlan = (String) list.get(i + 5);
  46. construction.setProjectPlan(projectPlan);
  47. String projectNature = (String) list.get(i + 6);
  48. construction.setProjectNature(projectNature);
  49. String projectScale = (String) list.get(i + 7);
  50. construction.setProjectScale(projectScale);
  51. String methods = (String) list.get(i + 8);
  52. construction.setMethods(methods);
  53. String limitPrice = (String) list.get(i + 9);
  54. construction.setLimitPrice(limitPrice);
  55. String technologyId = (String) list.get(i + 10);
  56. construction.setTechnologyId(technologyId);
  57. String biddingFee = (String) list.get(i + 11);
  58. construction.setBiddingFee(biddingFee);
  59. String requestId = (String) list.get(i + 12);
  60. construction.setRequestId(requestId);
  61. elemapper.uploadWord(construction);
  62. }
  63. }

参考项目名elezip111

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/391366
推荐阅读
相关标签
  

闽ICP备14008679号