日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java excel上传--poi

發布時間:2025/3/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java excel上传--poi 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

工作中很多批量上傳需求不同,每個需求都要寫一次批量上傳代碼,太煩。。決定寫一個通用工具,此代碼復制即用。需要改進的地方請評論區留言

方法調用傳參例子:
上傳文件中列的字段對應的是對象的屬性
String[] fieldsName = new String[]{"carModelName","salePrice"};

此參數是上傳文件中列必傳字段對應對象的屬性
String[] fieldsIsNull = new String[]{"carModelName"};

注:數組屬性順序必須和文件中順序一樣,解析文件時賦值不會賦錯字段

public static final String OFFICE_EXCEL_2003_POSTFIX = "xls"; public static final String OFFICE_EXCEL_2007_POSTFIX = "xlsx"; public static final String EMPTY = ""; public static final String POINT = ".";

/
**

* 通過反射處理以獲得的數據* @param*/ public T reflectDeal(T className, Cell cell, String fieldName){Class aClass = null;try {aClass = className.getClass();Field[] declaredFields = aClass.getDeclaredFields();for (Field field:declaredFields) {field.setAccessible(true);String names = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length());if(field.getName().equals(fieldName) && field.getType().getName().equals("java.math.BigDecimal")){aClass.getMethod("set"+names,BigDecimal.class).invoke(className,new BigDecimal(""+cell));}else if(field.getName().equals(fieldName) && field.getType().getName().equals("java.lang.String")){aClass.getMethod("set"+names,String.class).invoke(className,cell.getStringCellValue().toString());}else if(field.getName().equals(fieldName) && field.getType().getName().equals("java.lang.Integer")){aClass.getMethod("set"+names,Integer.class).invoke(className,cell);}else if(field.getName().equals(fieldName) && field.getType().getName().equals("java.lang.Double")){aClass.getMethod("set"+names,Double.class).invoke(className,cell);}}} catch (IllegalAccessException e) {e.printStackTrace();} catch (NoSuchMethodException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}return className; }/*** 本段代碼解析xls文件* @param*/ public List<T> analysisXls(InputStream in,Class<?> clazz,String[] fieldsName,String[] fieldsIsNull) {int totalrows = 0;int totalCell = 0;List<T> list = new ArrayList();logger.info("==================== 開始解析xls文件 =========================");//流讀取文件//創建文件HSSFWorkbook wb = null;try {wb = new HSSFWorkbook(in);//讀取頁數for(int num = 0 ; num < wb.getNumberOfSheets() ; num ++){Sheet xs = wb.getSheetAt(num);if(xs == null){continue;}totalrows = xs.getLastRowNum()+1;for (int rnum = 1 ; rnum < totalrows ; rnum ++){boolean flag = false;T o = null;try {o = (T)clazz.newInstance();} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}Row row = xs.getRow(rnum);if(row != null){totalCell = row.getLastCellNum();int nullCellNumb = 0;for (int cnum = 0 ; cnum < totalCell ; cnum ++){Cell cell = row.getCell(cnum);if(null == cell){nullCellNumb++;if(nullCellNumb>=10){break;}continue;}if( cell != null && !"".equals(cell.toString().trim())){this.reflectDeal(o, cell, fieldsName[cnum]);}else {for (int x=0;x<fieldsIsNull.length;x++){if(fieldsIsNull[x].equals(fieldsName[cnum])){flag = true;break;}}}}if (flag){continue;}list.add(o);}else {return list;}}}} catch (IOException e) {e.printStackTrace();}finally {try {in.close();if(null != wb){wb.close();}} catch (IOException e) {e.printStackTrace();}}return list; }

/**

* 本段代碼解析xlsx文件* @param* @param clazz* @param fieldsName* @return*/ public List<T> analysisXlsx(InputStream in,Class<?> clazz,String[] fieldsName,String[] fieldsIsNull){int totalrows = 0;int totalCell = 0;List<T> list = new ArrayList();logger.info("==================== 開始解析xlsx文件 =========================");//流讀取文件//創建文件XSSFWorkbook wb = null;try {wb = new XSSFWorkbook(OPCPackage.open(in));//讀取頁數for(int num = 0 ; num < wb.getNumberOfSheets() ; num ++){Sheet xs = wb.getSheetAt(num);if(xs == null){continue;}totalrows = xs.getLastRowNum()+1;for (int rnum = 1 ; rnum < totalrows ; rnum ++){boolean flag = false;T o = (T)clazz.newInstance();Row row = xs.getRow(rnum);if(row != null){totalCell = row.getLastCellNum();int nullCellNumb = 0;for (int cnum = 0 ; cnum < totalCell ; cnum ++){Cell cell = row.getCell(cnum);if(null == cell){nullCellNumb++;if(nullCellNumb>=10){break;}continue;}if( cell != null && !"".equals(cell.toString().trim())){nullCellNumb = 0;this.reflectDeal(o, cell, fieldsName[cnum]);}else {for (int x=0;x<fieldsIsNull.length;x++){if(fieldsIsNull[x].equals(fieldsName[cnum])){flag = true;break;}}}}if (flag){continue;}list.add(o);}else {return list;}}}} catch (IOException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();} catch (InvalidFormatException e) {logger.info("======= XSSF創建文件失敗 ====");e.printStackTrace();}finally {try {in.close();if(null != wb){wb.close();}} catch (IOException e) {e.printStackTrace();}}return list; }

/**

*此段代碼區分e'xcel版本,分別調用哪個方法* @param* @param clazz* @param fieldsName* @return* @throws IOException*/ public List<T> readxlsAndXlsx(InputStream in,Class<?> clazz,String[] fieldsName,String fileName,String[] fieldsIsNull) {if(null != in ){String postfix = ExcelImport.getpostfix(fileName);if(OFFICE_EXCEL_2003_POSTFIX.equals(postfix)){return this.analysisXls(in,clazz,fieldsName,fieldsIsNull);}else if(OFFICE_EXCEL_2007_POSTFIX.equals(postfix)){return this.analysisXlsx(in,clazz,fieldsName,fieldsIsNull);}else {return null;}}return null; }

/**

* 獲取文件后綴名* @param path* @return*/ public static String getpostfix(String path){if(path == null || EMPTY.equals(path.trim())){return "";}if(path.contains(POINT)){return path.substring(path.lastIndexOf(POINT)+1,path.length());}return ""; }

總結

以上是生活随笔為你收集整理的java excel上传--poi的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。