ExcelUtils.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package com.ydtech.modules.nai.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.ydtech.modules.nai.excelvo.OfflineExcelVO;
  4. import com.ydtech.modules.nai.excelvo.SettlementVO;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.poi.ss.usermodel.*;
  7. import org.apache.poi.xssf.usermodel.XSSFCell;
  8. import org.springframework.web.multipart.MultipartFile;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.lang.reflect.Field;
  12. import java.lang.reflect.InvocationTargetException;
  13. import java.math.BigDecimal;
  14. import java.text.DecimalFormat;
  15. import java.text.ParseException;
  16. import java.text.SimpleDateFormat;
  17. import java.time.format.DateTimeFormatter;
  18. import java.util.*;
  19. /**
  20. * @ClassName ExcelUtils
  21. * @Description: TODO
  22. * @Author
  23. * @Date 2021/7/6
  24. **/
  25. @Slf4j
  26. public class ExcelUtils {
  27. static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  28. /**
  29. * @param <T>
  30. * @param multipartFile
  31. * @param clz VO对象,对应Excel表头
  32. * @return
  33. * @throws IOException
  34. * @throws NoSuchMethodException
  35. * @throws IllegalAccessException
  36. * @throws InvocationTargetException
  37. * @throws InstantiationException
  38. */
  39. public static <T> List<SettlementVO> importExcel(MultipartFile multipartFile, Class<T> clz) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ParseException {
  40. if (null == multipartFile) {
  41. throw new NullPointerException("请选择文件");
  42. }
  43. log.info(multipartFile.getName());
  44. log.info("文件类型:{}", multipartFile.getContentType());
  45. String fileName = multipartFile.getOriginalFilename();
  46. log.info("文件名:{}", fileName);
  47. System.out.println("文件类型" + multipartFile.getContentType());
  48. /* if(!"application/vnd.ms-excel".equals(multipartFile.getContentType())) {
  49. throw new RuntimeException("请选择正确的文件类型与文件!");
  50. }
  51. */
  52. // 返回数据
  53. List<SettlementVO> list = new ArrayList<>();
  54. InputStream inputStream = multipartFile.getInputStream();
  55. Workbook wb = WorkbookFactory.create(inputStream);
  56. // 读取第一个sheet
  57. Sheet sheet = wb.getSheetAt(0);
  58. // 获取最大行数(或者sheet.getLastRowNum())
  59. int rownum = sheet.getPhysicalNumberOfRows();
  60. // 反射获取字段
  61. Field[] fields = clz.getDeclaredFields();
  62. // 获取第一行(表头)
  63. Row row = sheet.getRow(0);
  64. // 获取最大列数
  65. int column = row.getPhysicalNumberOfCells();
  66. for (int s = 0; s < column; s++) {
  67. Cell cell = row.getCell(s);
  68. // 遇到空列则结束
  69. if (cell == null) {
  70. break;
  71. }
  72. }
  73. // 处理行数据
  74. for (int i = 1; i < rownum; i++) {
  75. row = sheet.getRow(i);
  76. // 遇到空行则结束
  77. if (row == null) {
  78. break;
  79. }
  80. SettlementVO info = new SettlementVO();
  81. int c = row.getFirstCellNum();
  82. Cell cell = row.getCell(c);
  83. if (cell != null && cell.getCellType() != CellType.BLANK) {
  84. info.setPolicyNo(row.getCell(9).getStringCellValue());
  85. info.setLicenseNo(row.getCell(19).getStringCellValue());
  86. info.setRiskCode(row.getCell(94).getStringCellValue());
  87. DecimalFormat df = new DecimalFormat("0");
  88. info.setBillPrice(String.format("%.0f", row.getCell(29).getNumericCellValue()));
  89. info.setTaxAmountPrice(String.format("%.1f", row.getCell(84).getNumericCellValue()));
  90. info.setRate(df.format(row.getCell(68).getNumericCellValue()));
  91. info.setMount(String.format("%.2f", row.getCell(78).getNumericCellValue()));
  92. String stringCellValue = row.getCell(32).getStringCellValue();
  93. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  94. info.setApprovaldate(row.getCell(33).getStringCellValue());
  95. info.setInsuredage(row.getCell(110).getStringCellValue());
  96. info.setBirthday(stringCellValue);
  97. //是否认领
  98. info.setIsClaim("0");
  99. String s = JSON.toJSONString(info);
  100. list.add(info);
  101. }
  102. }
  103. System.out.println("获取到的条数" + list.size());
  104. return list;
  105. }
  106. public static <T> List<SettlementVO> importExcel2(MultipartFile multipartFile, Class<T> clz) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ParseException {
  107. if (null == multipartFile) {
  108. throw new NullPointerException("请选择文件");
  109. }
  110. log.info(multipartFile.getName());
  111. log.info("文件类型:{}", multipartFile.getContentType());
  112. String fileName = multipartFile.getOriginalFilename();
  113. log.info("文件名:{}", fileName);
  114. System.out.println("文件类型" + multipartFile.getContentType());
  115. /* if(!"application/vnd.ms-excel".equals(multipartFile.getContentType())) {
  116. throw new RuntimeException("请选择正确的文件类型与文件!");
  117. }
  118. */
  119. // 返回数据
  120. List<SettlementVO> list = new ArrayList<>();
  121. InputStream inputStream = multipartFile.getInputStream();
  122. Workbook wb = WorkbookFactory.create(inputStream);
  123. // 读取第一个sheet
  124. Sheet sheet = wb.getSheetAt(0);
  125. // 获取最大行数(或者sheet.getLastRowNum())
  126. int rownum = sheet.getPhysicalNumberOfRows();
  127. // 反射获取字段
  128. Field[] fields = clz.getDeclaredFields();
  129. // 获取第一行(表头)
  130. Row row = sheet.getRow(0);
  131. // 获取最大列数
  132. int column = row.getPhysicalNumberOfCells();
  133. for (int s = 0; s < column; s++) {
  134. Cell cell = row.getCell(s);
  135. // 遇到空列则结束
  136. if (cell == null) {
  137. break;
  138. }
  139. }
  140. // 处理行数据
  141. for (int i = 1; i < rownum; i++) {
  142. row = sheet.getRow(i);
  143. // 遇到空行则结束
  144. if (row == null) {
  145. break;
  146. }
  147. SettlementVO info = new SettlementVO();
  148. int c = row.getFirstCellNum();
  149. Cell cell = row.getCell(c);
  150. if (cell != null && cell.getCellType() != CellType.BLANK) {
  151. try {
  152. info.setPolicyNo(row.getCell(0).getStringCellValue());
  153. info.setLicenseNo(row.getCell(1).getStringCellValue());
  154. /* info.setRiskCode(row.getCell(3).getStringCellValue());
  155. DecimalFormat df = new DecimalFormat("0");
  156. info.setBillPrice(String.format("%.0f", row.getCell(4).getNumericCellValue()));
  157. info.setTaxAmountPrice(String.format("%.1f", row.getCell(5).getNumericCellValue()));
  158. info.setRate(df.format(row.getCell(68).getNumericCellValue()));
  159. info.setMount(String.format("%.2f", row.getCell(78).getNumericCellValue()));
  160. */
  161. String stringCellValue = row.getCell(2).getStringCellValue();
  162. /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  163. info.setApprovaldate(row.getCell(33).getStringCellValue());
  164. info.setInsuredage(row.getCell(110).getStringCellValue());*/
  165. info.setBirthday(stringCellValue);
  166. //是否认领
  167. list.add(info);
  168. } catch (Exception exception) {
  169. return null;
  170. }
  171. }
  172. }
  173. System.out.println("获取到的条数" + list.size());
  174. return list;
  175. }
  176. public static List<OfflineExcelVO> importOfflineExcel(MultipartFile multipartFile, Class<OfflineExcelVO> clz) {
  177. if (null == multipartFile) {
  178. throw new NullPointerException("请选择文件");
  179. }
  180. log.info(multipartFile.getName());
  181. log.info("文件类型:{}", multipartFile.getContentType());
  182. String fileName = multipartFile.getOriginalFilename();
  183. log.info("文件名:{}", fileName);
  184. System.out.println("文件类型" + multipartFile.getContentType());
  185. /* if(!"application/vnd.ms-excel".equals(multipartFile.getContentType())) {
  186. throw new RuntimeException("请选择正确的文件类型与文件!");
  187. }
  188. */
  189. // 返回数据
  190. List<OfflineExcelVO> list = new ArrayList<>();
  191. InputStream inputStream = null;
  192. try {
  193. inputStream = multipartFile.getInputStream();
  194. Workbook wb = WorkbookFactory.create(inputStream);
  195. // 读取第一个sheet
  196. Sheet sheet = wb.getSheetAt(0);
  197. // 获取最大行数(或者sheet.getLastRowNum())
  198. int rownum = sheet.getPhysicalNumberOfRows();
  199. // 反射获取字段clz.getDeclaredFields
  200. Field[] fields = clz.getDeclaredFields();
  201. // 获取第一行(表头)
  202. Row row = sheet.getRow(0);
  203. // 获取最大列数
  204. int column = row.getPhysicalNumberOfCells();
  205. for (int s = 0; s < column; s++) {
  206. XSSFCell cell = (XSSFCell) row.getCell(s);
  207. cell.setCellType(CellType.STRING);
  208. // 遇到空列则结束
  209. if (cell == null) {
  210. break;
  211. }
  212. }
  213. // 处理行数据
  214. for (int i = 1; i < rownum; i++) {
  215. row = sheet.getRow(i);
  216. // 遇到空行则结束
  217. if (row == null) {
  218. break;
  219. }
  220. OfflineExcelVO info = new OfflineExcelVO();
  221. int c = row.getFirstCellNum();
  222. Cell cell = row.getCell(c);
  223. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  224. if (row.getCell(0) == null || row.getCell(0).getCellType() == CellType.BLANK) {
  225. break;
  226. }
  227. if (cell != null && cell.getCellType() != CellType.BLANK
  228. ) {
  229. info.setFieldName1(String.valueOf(sdf.format(row.getCell(0).getDateCellValue())));
  230. info.setFieldName2(String.valueOf(row.getCell(1).getStringCellValue()));
  231. info.setFieldName3(String.valueOf(row.getCell(2).getStringCellValue()));
  232. info.setFieldName4(String.valueOf(row.getCell(3).getStringCellValue()));
  233. info.setFieldName5(String.valueOf(row.getCell(4).getStringCellValue()));
  234. info.setFieldName6(String.valueOf(row.getCell(5).getStringCellValue()));
  235. info.setFieldName7(String.valueOf(row.getCell(6).getStringCellValue()));
  236. info.setFieldName8(String.valueOf(row.getCell(7).getStringCellValue()));
  237. info.setFieldName9(String.valueOf(row.getCell(8).getStringCellValue()));
  238. info.setFieldName10(row.getCell(9).getNumericCellValue());//交强保费
  239. info.setFieldName11(row.getCell(10).getNumericCellValue());//交强手续费比例(入口)
  240. info.setFieldName12(row.getCell(11).getNumericCellValue());//交强手续费比例(出口)
  241. BigDecimal jnumb = new BigDecimal(row.getCell(9).getNumericCellValue());
  242. BigDecimal jnumr = new BigDecimal(row.getCell(10).getNumericCellValue());
  243. BigDecimal jnumc = new BigDecimal(row.getCell(11).getNumericCellValue());
  244. info.setFieldName13(jnumb.multiply(jnumr).doubleValue());//交强手续费金额(入口)
  245. info.setFieldName14(jnumb.multiply(jnumc).doubleValue());//交强手续费金额(出口)
  246. info.setFieldName15(row.getCell(14).getNumericCellValue());
  247. info.setFieldName16(row.getCell(15).getNumericCellValue());
  248. info.setFieldName17(row.getCell(16).getNumericCellValue());
  249. info.setFieldName18(row.getCell(17).getNumericCellValue());
  250. BigDecimal snumb = new BigDecimal(String.valueOf(row.getCell(15).getNumericCellValue()));
  251. BigDecimal snumr = new BigDecimal(String.valueOf(row.getCell(16).getNumericCellValue()));
  252. BigDecimal snumc = new BigDecimal(String.valueOf(row.getCell(17).getNumericCellValue()));
  253. info.setFieldName19(snumb.multiply(snumr).doubleValue());
  254. info.setFieldName20(snumb.multiply(snumc).doubleValue());
  255. info.setFieldName21(jnumb.multiply(jnumr).add(snumb.multiply(snumr)).doubleValue());
  256. info.setFieldName22(jnumb.multiply(jnumc).add(snumb.multiply(snumc)).doubleValue());
  257. info.setFieldName23(String.valueOf(row.getCell(22).getStringCellValue()));
  258. info.setFieldName24("0");
  259. info.setFieldName25(sdf.format(new Date()));
  260. info.setFieldName26("0");
  261. list.add(info);
  262. }
  263. }
  264. } catch (IOException e) {
  265. e.printStackTrace();
  266. }
  267. System.out.println("获取到的条数" + list.size());
  268. return list;
  269. }
  270. public static List<OfflineExcelVO> importOfflineExcel_query(MultipartFile multipartFile, Class<OfflineExcelVO> clz) {
  271. if (null == multipartFile) {
  272. throw new NullPointerException("请选择文件");
  273. }
  274. log.info(multipartFile.getName());
  275. log.info("文件类型:{}", multipartFile.getContentType());
  276. String fileName = multipartFile.getOriginalFilename();
  277. log.info("文件名:{}", fileName);
  278. System.out.println("文件类型" + multipartFile.getContentType());
  279. /* if(!"application/vnd.ms-excel".equals(multipartFile.getContentType())) {
  280. throw new RuntimeException("请选择正确的文件类型与文件!");
  281. }
  282. */
  283. // 返回数据
  284. List<OfflineExcelVO> list = new ArrayList<>();
  285. InputStream inputStream = null;
  286. try {
  287. inputStream = multipartFile.getInputStream();
  288. Workbook wb = WorkbookFactory.create(inputStream);
  289. // 读取第一个sheet
  290. Sheet sheet = wb.getSheetAt(0);
  291. // 获取最大行数(或者sheet.getLastRowNum())
  292. int rownum = sheet.getPhysicalNumberOfRows();
  293. // 反射获取字段clz.getDeclaredFields
  294. Field[] fields = clz.getDeclaredFields();
  295. // 获取第一行(表头)
  296. Row row = sheet.getRow(0);
  297. // 获取最大列数
  298. int column = row.getPhysicalNumberOfCells();
  299. for (int s = 0; s < column; s++) {
  300. XSSFCell cell = (XSSFCell) row.getCell(s);
  301. cell.setCellType(CellType.STRING);
  302. // 遇到空列则结束
  303. if (cell == null) {
  304. break;
  305. }
  306. }
  307. // 处理行数据
  308. for (int i = 1; i < rownum; i++) {
  309. row = sheet.getRow(i);
  310. // 遇到空行则结束
  311. if (row == null) {
  312. break;
  313. }
  314. OfflineExcelVO info = new OfflineExcelVO();
  315. int c = row.getFirstCellNum();
  316. Cell cell = row.getCell(c);
  317. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  318. if (row.getCell(0) == null || row.getCell(0).getCellType() == CellType.BLANK) {
  319. break;
  320. }
  321. if (cell != null && cell.getCellType() != CellType.BLANK
  322. ) {
  323. info.setFieldName5(String.valueOf(row.getCell(0).getStringCellValue()));
  324. info.setFieldName7(String.valueOf(row.getCell(1).getStringCellValue()));
  325. list.add(info);
  326. }
  327. }
  328. } catch (IOException e) {
  329. e.printStackTrace();
  330. }
  331. System.out.println("获取到的条数" + list.size());
  332. return list;
  333. }
  334. }