目的:重復性代碼盡量少寫或者不寫,復制粘貼即可
CSV文件讀取代碼
/** * 讀取csv文件數據到對象集合中 * @param inputStream 文件輸入流 * @param charset 文件字符集 * @param fields 對象的字段 * @param startRow 從第幾行開始讀 * @param clazz 對象類 * @param <T> * @return */ public static <T> List<T> readCsv(InputStream inputStream, String charset, String[] fields, int startRow, Class<T> clazz){ List<T> list = new ArrayList<>(); InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; try { if(StringUtils.isEmpty(charset)){ charset = SettingConstant.CHAR_SET_GBK; } inputStreamReader = new InputStreamReader(inputStream, charset); bufferedReader = new BufferedReader(inputStreamReader); String rowStr; int row = 0; while((rowStr=bufferedReader.readLine())!=null){ row++; if(row < startRow){ continue; } if (rowStr.contains(",")) { T obj = clazz.newInstance(); String[] datas = rowStr.split(","); Field rowNumField = clazz.getDeclaredField(ROW_NUM_FIELD); rowNumField.setAccessible(true); rowNumField.set(obj, row); // 循環次數:如果對象字段個數與csv的字段個數比較,取較小的數值 int cycleCount = fields.length > datas.length ? datas.length : fields.length; for (int i = 0; i < cycleCount; i++) { Field declaredField = clazz.getDeclaredField(fields[i]); declaredField.setAccessible(true); declaredField.set(obj, datas[i]); } list.add(obj); } else { log.error("第{}行的數據格式錯誤", row); } } } catch (IOException e) { log.error("文件讀寫異常!",e); throw new BusinessException("文件讀寫異常"); } catch (IllegalAccessException e) { log.error("對象屬性訪問異常!",e); throw new BusinessException("對象屬性訪問異常"); } catch (InstantiationException e) { log.error("對象實例化異常!",e); throw new BusinessException("對象實例化異常"); } catch (NoSuchFieldException e) { log.error("字段不存在!",e); throw new BusinessException("字段不存在"); } finally { try { if(inputStreamReader != null){ inputStreamReader.close(); } if(bufferedReader != null){ bufferedReader.close(); } } catch (IOException e) { log.error("文件流關閉異常!",e); throw new BusinessException("文件流關閉異常"); } } return list; }
CSV文件生成
/** * 根據傳入的字段順序生成csv文件 * @param fileName 文件名 * @param dataList 數據集合 * @param tableHeads csv表頭 * @param fieldNames 指定的字段名集合 * @return csv文件 */ public static File exportCsv(String fileName, List<?> dataList, String[] tableHeads, String[] fieldNames) { if (CollectionUtils.isEmpty(dataList) && Objects.isNull(tableHeads)) { log.info("數據集和表頭不能同時為空"); throw new RuntimeException("數據集和表頭不能同時為空"); } File file = new File(fileName); try (OutputStreamWriter streamWriter = new OutputStreamWriter(new FileOutputStream(file),"GBK"); BufferedWriter bufferedWriter = new BufferedWriter(streamWriter)) { // CSV表頭處理 if (tableHeads != null && tableHeads.length > 0) { StringBuffer headerBuffer = new StringBuffer(); for (int i = 0; i < tableHeads.length; i++) { // 拼接表頭字段 headerBuffer.append(doCsvCell(tableHeads[i])); } // 截掉最后一個逗號,並寫入文件流中 if (headerBuffer.length() > 1) { headerBuffer.substring(0, headerBuffer.length() - 1); } bufferedWriter.write(headerBuffer.toString()); bufferedWriter.newLine(); } // CSV數據處理 if (!CollectionUtils.isEmpty(dataList)) { for (int i = 0; i < dataList.size(); i++) { StringBuffer rowDataBuffer = new StringBuffer(); // 獲取list中的數據對象,根據傳如的字段順序進行寫入到CSV中 Object dataObj = dataList.get(i); for (int j = 0; j < fieldNames.length; j++) { // 獲取對象對應的字段值 Field field = dataObj.getClass().getDeclaredField(fieldNames[j]); field.setAccessible(true); Object fieldValue = field.get(dataObj); rowDataBuffer.append(doCsvCell(fieldValue)); } // 截取掉最后一個逗號,並寫入文件流中 if (rowDataBuffer.length() > 1) { rowDataBuffer.substring(0, rowDataBuffer.length() - 1); } bufferedWriter.write(rowDataBuffer.toString()); bufferedWriter.newLine(); } } bufferedWriter.flush(); } catch (IOException e) { log.error("文件操作異常",e); throw new RuntimeException("文件操作異常",e); } catch (IllegalAccessException e) { log.error("屬性無法訪問",e); throw new RuntimeException("屬性無法訪問",e); } catch (NoSuchFieldException e) { log.error("字段不存在",e); throw new RuntimeException("字段不存在",e); } return file; }