PageRenderTime 63ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/web/src/main/java/com/sishuok/es/showcase/excel/service/Excel2003ImportListener.java

https://github.com/kissys/es
Java | 116 lines | 77 code | 16 blank | 23 comment | 27 complexity | ae59df26890bbbe2087f556c2c4c51ca MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Copyright (c) 2005-2012 https://github.com/zhangkaitao
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. */
  6. package com.sishuok.es.showcase.excel.service;
  7. import com.sishuok.es.showcase.excel.entity.ExcelData;
  8. import org.apache.poi.hssf.eventusermodel.HSSFListener;
  9. import org.apache.poi.hssf.record.*;
  10. import java.util.List;
  11. /**
  12. * <p>User: Zhang Kaitao
  13. * <p>Date: 13-7-12 下午9:47
  14. * <p>Version: 1.0
  15. */
  16. class Excel2003ImportListener implements HSSFListener {
  17. int batchSize; //批处理大小
  18. int totalSize = 0; //总大小
  19. private SSTRecord sstrec;
  20. List<ExcelData> dataList;
  21. ExcelData current = null;
  22. private ExcelDataService excelDataService;
  23. Excel2003ImportListener(
  24. final ExcelDataService excelDataService, final List<ExcelData> dataList, final int batchSize) {
  25. this.excelDataService = excelDataService;
  26. this.dataList = dataList;
  27. this.batchSize = batchSize;
  28. }
  29. @Override
  30. public void processRecord(final Record record) {
  31. switch (record.getSid()) {
  32. case BOFRecord.sid:
  33. //开始解析到workboot sheet 等
  34. BOFRecord bof = (BOFRecord) record;
  35. if (bof.getType() == bof.TYPE_WORKBOOK) {
  36. //workbook
  37. } else if (bof.getType() == bof.TYPE_WORKSHEET) {
  38. //sheet
  39. }
  40. break;
  41. case BoundSheetRecord.sid:
  42. //开始解析BundleSheet
  43. BoundSheetRecord bsr = (BoundSheetRecord) record;
  44. //bsr.getSheetname() 得到sheet name
  45. break;
  46. case RowRecord.sid:
  47. //开始解析行
  48. RowRecord rowrec = (RowRecord) record;
  49. break;
  50. case SSTRecord.sid:
  51. // SSTRecords存储了在Excel中使用的所有唯一String的数组
  52. sstrec = (SSTRecord) record;
  53. break;
  54. case NumberRecord.sid:
  55. case LabelSSTRecord.sid:
  56. if(record instanceof NumberRecord) {
  57. //解析一个Number类型的单元格值
  58. NumberRecord numrec = (NumberRecord) record;
  59. //numrec.getRow() numrec.getColumn() numrec.getValue()
  60. if(numrec.getRow() == 0) {
  61. //第一行 跳过
  62. break;
  63. } else if(numrec.getColumn() == 0) { //第一列
  64. current = new ExcelData();
  65. current.setId(Double.valueOf(numrec.getValue()).longValue());
  66. } else if(numrec.getColumn() == 1) {//第二列
  67. current.setContent(String.valueOf(Double.valueOf(numrec.getValue()).longValue()));
  68. add(current);
  69. }
  70. break;
  71. } else if(record instanceof LabelSSTRecord) {
  72. //解析一个String类型的单元格值(存储在SSTRecord)
  73. LabelSSTRecord lrec = (LabelSSTRecord) record;
  74. if(lrec.getRow() == 0) {
  75. //第一行 跳过
  76. break;
  77. } else if(lrec.getColumn() == 0) { //第一列
  78. current = new ExcelData();
  79. String value = sstrec.getString(lrec.getSSTIndex()).getString();
  80. current.setId(Double.valueOf(value).longValue());
  81. } else if(lrec.getColumn() == 1) {//第二列
  82. String value = sstrec.getString(lrec.getSSTIndex()).getString();
  83. current.setContent(value);
  84. add(current);
  85. }
  86. break;
  87. }
  88. break;
  89. }
  90. }
  91. private void add(final ExcelData current) {
  92. dataList.add(current);
  93. totalSize++;
  94. //最后一个单元格时 判断是否该写了
  95. if (totalSize % batchSize == 0) {
  96. excelDataService.doBatchSave(dataList);
  97. dataList.clear();
  98. }
  99. }
  100. }