java excel上传--poi
生活随笔
收集整理的這篇文章主要介紹了
java excel上传--poi
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
工作中很多批量上傳需求不同,每個(gè)需求都要寫一次批量上傳代碼,太煩。。決定寫一個(gè)通用工具,此代碼復(fù)制即用。需要改進(jìn)的地方請(qǐng)?jiān)u論區(qū)留言
方法調(diào)用傳參例子:
上傳文件中列的字段對(duì)應(yīng)的是對(duì)象的屬性
String[] fieldsName = new String[]{"carModelName","salePrice"};
此參數(shù)是上傳文件中列必傳字段對(duì)應(yīng)對(duì)象的屬性
String[] fieldsIsNull = new String[]{"carModelName"};
注:數(shù)組屬性順序必須和文件中順序一樣,解析文件時(shí)賦值不會(huì)賦錯(cuò)字段
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 = ".";/
**
/**
* 本段代碼解析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文件 =========================");//流讀取文件//創(chuàng)建文件XSSFWorkbook wb = null;try {wb = new XSSFWorkbook(OPCPackage.open(in));//讀取頁(yè)數(shù)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創(chuàng)建文件失敗 ====");e.printStackTrace();}finally {try {in.close();if(null != wb){wb.close();}} catch (IOException e) {e.printStackTrace();}}return list; }/**
*此段代碼區(qū)分e'xcel版本,分別調(diào)用哪個(gè)方法* @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 ""; }總結(jié)
以上是生活随笔為你收集整理的java excel上传--poi的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Bootstrap link 引入3文件
- 下一篇: js省份城市初始化