GetExcelInfoUtil.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package com.allpayx.atsmpservice.common;
  2. import com.allpayx.atsmpservice.param.ResponseParam.RecoredConfirmInfoResponse;
  3. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  4. import org.apache.poi.ss.usermodel.Cell;
  5. import org.apache.poi.ss.usermodel.Row;
  6. import org.apache.poi.ss.usermodel.Sheet;
  7. import org.apache.poi.ss.usermodel.Workbook;
  8. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.bind.annotation.RequestParam;
  12. import org.springframework.web.bind.annotation.ResponseBody;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.text.DecimalFormat;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. public class GetExcelInfoUtil {
  20. /** 读取Excel信息
  21. * @Company : AllPay
  22. * @author : lei.chen
  23. * @CreateDate : 2019/2/28 14:11
  24. */
  25. public static List<RecoredConfirmInfoResponse> getExcelInfo( MultipartFile mFile) {
  26. String fileName = mFile.getOriginalFilename();// 获取文件名
  27. List<RecoredConfirmInfoResponse> list = new ArrayList<>();
  28. List<String> error=null;
  29. try {
  30. if (!validateExcel(fileName)) {// 验证文件名是否合格
  31. return null;
  32. }
  33. boolean isExcel2003 = true;// 根据文件名判断文件是2003版本还是2007版本
  34. if (isExcel2007(fileName)) {
  35. isExcel2003 = false;
  36. }
  37. list = createExcel(mFile.getInputStream(), isExcel2003,error);
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. return list;
  42. }
  43. /**
  44. *
  45. * @Title: validateExcel
  46. * @Description: 验证EXCEL文件
  47. * @return boolean
  48. * @author lei
  49. * @date 2019/2/28 14:11
  50. */
  51. public static boolean validateExcel(String filePath) {
  52. if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {
  53. String errorMsg = "文件名不是excel格式";
  54. return false;
  55. }
  56. return true;
  57. }
  58. // 是否是2003的excel,返回true是2003
  59. public static boolean isExcel2003(String filePath) {
  60. return filePath.matches("^.+\\.(?i)(xls)$");
  61. }
  62. // 是否是2007的excel,返回true是2007
  63. public static boolean isExcel2007(String filePath) {
  64. return filePath.matches("^.+\\.(?i)(xlsx)$");
  65. }
  66. /**
  67. *
  68. * @Title: createExcel
  69. * @Description: 根据excel里面的内容读取信息
  70. * @return List<T>
  71. * @author lei
  72. * @date 2019/2/28 14:11
  73. */
  74. public static List<RecoredConfirmInfoResponse> createExcel(InputStream is, boolean isExcel2003, List<String> error) {
  75. List<RecoredConfirmInfoResponse> list = new ArrayList<>();
  76. try {
  77. Workbook wb = null;
  78. if (isExcel2003) {// 当excel是2003时,创建excel2003
  79. wb = new HSSFWorkbook(is);
  80. } else {// 当excel是2007时,创建excel2007
  81. wb = new XSSFWorkbook(is);
  82. }
  83. list = readExcelValue(wb, error);// 读取Excel里面客户的信息
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. return list;
  88. }
  89. /**
  90. *
  91. * @Title: readExcelValue
  92. * @Description: 读取Excel信息
  93. * @return List<T>
  94. * @author lei
  95. * @date 2019/2/28 14:11
  96. */
  97. public static List<RecoredConfirmInfoResponse> readExcelValue(Workbook wb, List<String> error) {
  98. int totalRows =0;
  99. int totalCells =0;
  100. // 得到第一个shell
  101. Sheet sheet = wb.getSheetAt(0);
  102. // 得到Excel的行数
  103. totalRows = sheet.getPhysicalNumberOfRows();
  104. // 得到Excel的列数(前提是有行数)
  105. if (totalRows > 1 && sheet.getRow(0) != null) {
  106. totalCells = sheet.getRow(1).getPhysicalNumberOfCells();
  107. }
  108. // 循环Excel行数
  109. List<RecoredConfirmInfoResponse> list = new ArrayList<>();
  110. for (int r = 1; r < totalRows; r++) {
  111. Row row = sheet.getRow(r);
  112. if (row == null) {
  113. continue;
  114. }
  115. // T model = new T();
  116. RecoredConfirmInfoResponse model = new RecoredConfirmInfoResponse();
  117. // 循环Excel的列
  118. for (int c = 0; c < totalCells; c++) {
  119. Cell cell = row.getCell(c);
  120. if (null != cell) {
  121. if (c == 0) {
  122. System.out.println(cell.getCellType());
  123. if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
  124. model.setCreate_time(cell.getStringCellValue());
  125. }
  126. } else if (c == 1) {
  127. if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
  128. model.setDone_time(cell.getStringCellValue());
  129. }
  130. } else if (c == 2) {
  131. if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
  132. model.setBatch_no(new DecimalFormat("0").format(cell.getNumericCellValue()));
  133. }
  134. } else if (c == 3) {
  135. if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
  136. model.setUser_id(cell.getStringCellValue());
  137. }
  138. } else if (c == 4) {
  139. if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
  140. model.setEcp_code(cell.getStringCellValue());
  141. }
  142. }
  143. else if (c == 5) {
  144. if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
  145. model.setMID(cell.getStringCellValue());
  146. }
  147. }
  148. else if (c == 6) {
  149. if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
  150. model.setMPID(cell.getStringCellValue());
  151. }
  152. }
  153. else if (c == 7) {
  154. if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
  155. model.setVoucher_id(cell.getStringCellValue());
  156. }
  157. }
  158. else if (c == 8) {
  159. if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
  160. model.setAmount(new DecimalFormat("0").format(cell.getNumericCellValue()));
  161. }
  162. }
  163. else if (c == 9) {
  164. if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
  165. model.setCurrency(cell.getStringCellValue());
  166. }
  167. }
  168. else if (c == 10) {
  169. if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
  170. model.setProcess_status(cell.getStringCellValue());
  171. }
  172. }
  173. }
  174. }
  175. list.add(model);
  176. }
  177. return list;
  178. }
  179. }