文件导入导出必须代码
Exportexcel.java
/*** حقوق الطبع والنشر © 2012-2014 <a href = "https://github.com/thinkgem/jeesite"> jeSite </a> جميع الحقوق محفوظة. */package com.thinkgem.jesite.common.Utils.excel ؛ استيراد java.io.filenotfoundException ؛ استيراد java.io.fileOutputStream ؛ استيراد java.io.ioException ؛ استيراد java.io.outputstream ؛ استيراد java.lang.reflect.field ؛ استيراد java.lang.reflect.method ؛ استيراد java.text.decimalformat ؛ java.util.comparator ؛ import java.util.date ؛ import java.util.hashmap ؛ import java.util.linkedhashmap ؛ import java.util.list ؛ import java.util.map ؛ استيراد javax.servlet.http.httpservletresponse ؛ استيراد org.apache.commons.lang3.stringUtils ؛ استيراد org.apache.poi.ss.usermodel.cell ؛ استيراد org.apache.poi.ss.usermodel.cellstyle ؛ import org.apache.poi.ss.usermodel.comment ؛ import org.apoi.ss.usermoDel.datormodel.s.poatormodel.datormodel.dataRaMoDel.dataRaModel.dataRaModel.DataRaMODEL.DATARATORMATER ؛ org.apache.poi.ss.usermodel.font ؛ import org.apache.poi.ss.usermodel.indexedcolors ؛ import org.apache.poi.ss.usermodel org.apache.poi.ss.util.cellrangeaddress ؛ استيراد org.apache.poi.xssf.streaming.sxssfworkbook ؛ import org.apache.poi.xssf.usermodel.xssfclientanchor ؛ import org.apache.poi.xsssf.usermodel org.slf4j.logger ؛ استيراد org.slf4j.loggerfactory ؛ استيراد com.google.common.collect.lists ؛ استيراد com.thinkgem.jeesite.common.Utils.dateutils ؛ استيراد com.thinkgem.jeSite.Common.Utils.encodes ؛ استيراد com.thinkgem.jeesite.common.Utils.refles ؛ com.thinkgem.jesite.common.Utils.excel.annotation.excelfield ؛ استيراد com.thinkgem.jesite.modules.sys.utils.dictutils ؛ /** * 导出 excel 文件 (导出 导出 "xlsx" 格式 , 支持大数据量导出 see org.apache.poi.ss.spreadsheetVersion) * @Author inchectgem * version 2013-04-21 */public class orportexcel / ** * 工作薄对象 */ private SXSSFWorkbook WB ؛ / ** * 工作表对象 */ ورقة الورقة الخاصة ؛ / ** * 样式列表 */ map private <string ، cellstyle> styles ؛ / ** * 当前行号 */ private int rownum ؛ /** * 注解列表 (object [] {exceld ، field/method}) */list <object []> enronotationlist = lists.newarrayList () ؛ / ** * 字段 map */ linkedHashMap <string ، string> fieldMap ؛ hashmap <string ، string> dicttypes ؛ / ** * 构造函数 * param title 表格标题 , 传 传 "空值" , 表示无标题 * param cls 实体对象 , 通过 通过 in enotation.exportfield 获取标题 */ public orportexcel (عنوان السلسلة ، الفئة <؟> cls) {this (title ، cls ، 1) ؛ } / ** * 构造函数 * param title 表格标题 , 传 "空值" , 表示无标题 * param fieldmap , 获取要导出的字段和字段标题 * / public orportexcel (عنوان السلسلة ، linkedhashmap <string ، string> fieldMap) {this.fieldmap = fieldMap ؛ dicttypes = new hashmap <string ، string> () ؛ // تهيئة int colunm = 0 ؛ قائمة <Tring> headerlist = lists.newarrayList () ؛ لـ (مفتاح السلسلة: fieldmap.keyset ()) {string t = fieldmap.get (key) ؛ hashmap <string ، string> map = com.thinkgem.jesite.common.utils.stringutils.tomap (t ، "؛" ، "=" ، false) ؛ if (map.get ("name")! = null) {t = map.get ("name") ؛ } if (map.get ("dicttype")! = null) {dicttypes.put (""+(colunm) ، map.get ("dicttype")) ؛ } colunm ++ ؛ headerlist.add (t) ؛ } تهيئة (العنوان ، قائمة الرأس) ؛ } / ** * 构造函数 * param title 表格标题 , 传 传 "空值" , , 表示无标题 * param cls 实体对象 , 通过 通过 通过 通过 exportfield 获取标题 * @param type 导出类型 (1: 导出数据; 2 : cls.getDeclaredFields () ؛ لـ (Field F: FS) {Excelfield ef = f.getAnnotation (excelfield.class) ؛ if (ef! = null && (ef.type () == 0 || ef.type () == type)) {if (groups! = null && groups.length> 0) {boolean ingroup = false ؛ لـ (int g: المجموعات) {if (ingroup) {break ؛ } لـ (int efg: ef.groups ()) {if (g == efg) {ingroup = true ؛ incenotationlist.add (كائن جديد [] {ef ، f}) ؛ استراحة؛ }}}} آخر {incenotationList.add (كائن جديد [] {ef ، f}) ؛ }}} // الحصول على طريقة التعليقات التوضيحية [] ms = cls.getDeclaredMethods () ؛ لـ (method m: ms) {excelfield ef = m.getAnnotation (excelfield.class) ؛ if (ef! = null && (ef.type () == 0 || ef.type () == type)) {if (groups! = null && groups.length> 0) {boolean ingroup = false ؛ لـ (int g: المجموعات) {if (ingroup) {break ؛ } لـ (int efg: ef.groups ()) {if (g == efg) {ingroup = true ؛ incenotationlist.add (كائن جديد [] {ef ، m}) ؛ استراحة؛ }}}} آخر {incenotationList.add (كائن جديد [] {ef ، m}) ؛ }}} // مجموعة فرز الحقل // تهيئة قائمة <Tring> headerlist = lists.newarrayList () ؛ لـ (Object [] OS: annotationList) {String t = ((excelfield) OS [0]). title () ؛ // 如果是导出 , 则去掉注释 if (type == 1) {string [] ss = stringUtils.split (t ، "**" ، 2) ؛ if (ss.length == 2) {t = ss [0] ؛ }} headerlist.add (t) ؛ } تهيئة (العنوان ، قائمة الرأس) ؛ } / ** * 构造函数 * @param title 表格标题 , 传 "空值" , 表示无标题 * @param headers 表头数组 * / public orportexcel (عنوان السلسلة ، السلسلة [] رؤوس) {initial (title ، lists.newarraylist (headers)) ؛ } / ** * 构造函数 * param title 表格标题 , 传 "空值" , 表示无标题 * param headerlist 表头列表 * / public esportexcel (عنوان السلسلة ، القائمة <string> headerlist) {initialize (title ، headerlist) ؛ } / ** * 初始化函数 * param title 表格标题 , 传 "空值" , 表示无标题 * param headerlist 表头列表 * / private void تهيئة (عنوان السلسلة ، القائمة <string> headerlist) {this.wb = new sxssfworkbook (500) ؛ this.sheet = wb.createseet ("Export") ؛ this.styles = createStyles (wb) ؛ // إنشاء عنوان if (stringUtils.isnotBlank (title)) {row titlerow = sheet.createrow (rownum ++) ؛ Titlerow.SetheightInpoints (30) ؛ الخلية titlecell = titlerow.createcell (0) ؛ TitleCell.setCellStyle (Styles.get ("title")) ؛ titlecell.setCellValue (العنوان) ؛ sheet.addmergedregion (New CellRangeadDress (titlerow.getRownum () ، titlerow.getRownum () ، titlerow.getRownum () ، headerlist.size ()-1)) ؛ } // إنشاء header if (headerlist == null) {رمي new runTimeException ("headerlist not null!") ؛ } raw headerrow = sheet.createrw (rownum ++) ؛ headerrow.Setheightinpoints (16) ؛ لـ (int i = 0 ؛ i <headerlist.size () ؛ i ++) {cell cell = headerrow.createcell (i) ؛ cell.setCellStyle (Styles.get ("header")) ؛ String [] ss = stringUtils.split (headerlist.get (i) ، "**" ، 2) ؛ if (ss.length == 2) {cell.setCellValue (ss [0]) ؛ تعليق التعليق = this.sheet.createdRawingPatriarch (). CreateCellcomment (New XSSfClientanchor (0 ، 0 ، 0 ، 0 ، (قصيرة) 3 ، 3 ، (قصيرة) 5 ، 6)) ؛ Comment.SetString (XSSfrichtextString (SS [1])) ؛ cell.setCellcomment (تعليق) ؛ } آخر {cell.setCellValue (headerlist.get (i)) ؛ } sheet.autosizeColumn (i) ؛ } لـ (int i = 0 ؛ i <headerlist.size () ؛ i ++) {int colwidth = sheet.getColumnWidth (i)*2 ؛ sheet.setColumnWidth (i ، colwidth <3000؟ 3000: colwidth) ؛ } log.debug ("تهيئة النجاح.") ؛ } / ** * 创建表格样式 * param wb 工作薄对象 * return 样式列表 * / map private <string ، cellstyle> createStyles (Workbook wb) {map <string ، cellstyle> styles = new hashmap <string ، cellstyle> () ؛ نمط cellstyle = wb.createcellstyle () ؛ style.setalignment (cellstyle.align_center) ؛ style.setverticalAlignment (cellstyle.vertical_center) ؛ font titlefont = wb.createfont () ؛ titlefont.setfontname ("arial") ؛ titlefont.setfontheightinpoints ((قصيرة) 16) ؛ titlefont.setBoldweight (font.boldweight_bold) ؛ style.setfont (titlefont) ؛ Styles.put ("title" ، style) ؛ style = wb.createcellstyle () ؛ style.setverticalAlignment (cellstyle.vertical_center) ؛ style.setBorderRight (cellstyle.border_thin) ؛ style.setRightBorderColor (INSEDCOLORS.GREY_50_PERCENT.GETINDEX ()) ؛ style.setborderleft (cellstyle.border_thin) ؛ style.setLeftBorderColor (INSEDCOLORS.GREY_50_PERCENT.GETINDEX ()) ؛ style.setBorderTop (cellstyle.border_thin) ؛ style.settopBorderColor (INSEDCOLORS.GREY_50_PERCENT.GETINDEX ()) ؛ style.setBorderBottom (cellstyle.border_thin) ؛ style.setbottomborderColor (INSEDCOLORS.GREY_50_PERCENT.GETINDEX ()) ؛ font datafont = wb.createfont () ؛ DataFont.setFontName ("arial") ؛ datafont.setfontheightinpoints ((قصيرة) 10) ؛ style.setfont (dataFont) ؛ Styles.put ("البيانات" ، النمط) ؛ style = wb.createcellstyle () ؛ style.clonestylefrom (styles.get ("data")) ؛ style.setalignment (cellstyle.align_left) ؛ Styles.put ("data1" ، style) ؛ style = wb.createcellstyle () ؛ style.clonestylefrom (styles.get ("data")) ؛ style.setalignment (cellstyle.align_center) ؛ Styles.put ("data2" ، style) ؛ style = wb.createcellstyle () ؛ style.clonestylefrom (styles.get ("data")) ؛ style.setalignment (cellstyle.align_right) ؛ Styles.put ("data3" ، style) ؛ style = wb.createcellstyle () ؛ style.clonestylefrom (styles.get ("data")) ؛ // style.setWrapText (true) ؛ style.setalignment (cellstyle.align_center) ؛ style.setFillForeGroundColor (INSTEDCOLORS.GREY_50_PERCENT.GETINDEX ()) ؛ style.setFillPattern (cellstyle.solid_foreground) ؛ font headerfont = wb.createfont () ؛ headerfont.setfontname ("arial") ؛ headerfont.setfontheightpoints ((قصيرة) 10) ؛ HeaderFont.setBoldweight (font.boldweight_bold) ؛ Headerfont.setColor (INSEDCOLORS.WHITE.GETINDEX ()) ؛ style.setfont (headerfont) ؛ Styles.put ("header" ، style) ؛ أنماط العودة } / ** * 添加一行 * return 行对象 * / public Row addRow () {return sheet.createrow (rownum ++) ؛ } / ** * 添加一个单元格 * param row 添加的行 * param column 添加列号 * param val 添加值 * return 单元格对象 * / public cell addcell (صف صف ، عمود int ، كائن val) {return this.addcell (row ، colum ، val ، 0 ، class.class) ؛ } / *: نمط cellstyle = styles.get ("البيانات"+(محاذاة> = 1 && align <= 3؟ align: "")) ؛ حاول {if (val == null) {cell.setCellValue ("") ؛ } آخر إذا (Val eastyof string) {cell.setCellValue ((string) val) ؛ } آخر إذا (val intoryof integer) {cell.setCellValue ((integer) val) ؛ } آخر إذا (val eastyof long) {cell.setCellValue ((long) val) ؛ } آخر إذا (val eastyof double) {cell.setCellValue (new decimalformat (". #####"). format (val)) ؛ } آخر إذا (val eastyof float) {cell.setCellValue ((float) val) ؛ } آخر إذا (val eastyof date) {dataFormat Format = wb.createdataformat () ؛ style.setDataFormat (format.getFormat ("Yyyy-MM-DD")) ؛ cell.setCellValue (dateUtils.FormatDatetEtime ((Date) Val)) ؛ } آخر {if (fieldType! = class.class) {cell.setCellValue ((string) fieldType.getMethod ("setValue" ، object.class) .invoke (null ، val)) ؛ } else {cell.setCellValue ((string) class.forname (this.getClass (). }}} catch (استثناء ex) {log.info ("set value cell ["+row.getRownum ()+"،"+column+"] خطأ:"+ex.toString ()) ؛ cell.setCellValue (Val.ToString ()) ؛ } cell.setCellStyle (style) ؛ خلية العودة. } / ** * 添加数据 (通过 inceration.exportfield 添加数据) * regurn 数据列表 * / public <e> exportexcel setDatalist (list <e> list) {for (e e: list) {int colunm = 0 ؛ صف الصف = this.addrow () ؛ StringBuilder sb = new StringBuilder () ؛ if (e eastyof map) {suppressWarnings ("unchected") map <string ، Object> map = (map <string ، Object>) e ؛ لـ (مفتاح السلسلة: fieldmap.keyset ()) {componse value = map.get (key) ؛ string columnDictType = dictTypes.get (colunm+"") ؛ if (stringUtils.isNotBlank (columnDictType)) {value = dictutils.getDictLabel (value == null؟ } this.addcell (row ، colunm ++ ، value == null؟ "": value.toString () ، 0 ، string.class) ؛ sb.append (value + "،") ؛ }} else {for (object [] OS: annotationList) {Excelfield ef = (excelfield) OS [0] ؛ كائن val = null ؛ // الحصول على قيمة الكيان جرب {if (stringUtils.isNotBlank (ef.value ())) {val = Reflections.invokegetter (e ، ef.value ()) ؛ } آخر {if (os [1] easuleof field) {val = Reflections.Invokegetter (e ، ((field) os [1]). getName ()) ؛ } if if (OS [1] easuleof method) {val = Reflections.InvokeMethod (e ، (method) os [1]). }} // إذا كان dict ، احصل على تسمية dict if (stringUtils.isNotBlank (ef.dicttype ())) {val = dictutils.getDictLabel (val == null؟ "": val.ToString () ، ef.dicttype () }} catch (استثناء ex) {// الفشل في تجاهل log.info (ex.toString ()) ؛ val = "" ؛ } this.addcell (row ، colunm ++ ، val ، ef.align () ، ef.fieldtype ()) ؛ sb.append (val + "،") ؛ } log.debug ("اكتب النجاح: ["+row.getRownum ()+"]"+sb.toString ()) ؛ }} إرجاع هذا ؛ } / ** * 输出数据流 * param os 输出数据流 * / public orportexcel write (OutputStream OS) يلقي ioException {wb.write (OS) ؛ إرجاع هذا ؛ } / ** * 输出到客户端 * param filename 输出文件名 * / public orportexcel write (httpservletresponse response ، string filename) يلقي ioException {reponse.Reset () ؛ استجابة. استجابة. اكتب (reponse.getOutputStream ()) ؛ إرجاع هذا ؛ } / ** * 输出到文件 * param filename 输出文件名 * / public orportexcel writefile (اسم السلسلة) يلقي fileNotfoundException ، ioException {fileOutputStream OS = جديد fileOutputStream (name) ؛ this.write (OS) ؛ إرجاع هذا ؛ } / ** * 清理临时文件 * / public orportexcel dispose () {wb.dispose () ؛ إرجاع هذا ؛ }}导出测试
Public Static Void Main (String [] args) رمي {list <string> headerlist = lists.newarraylist () ؛ لـ (int i = 1 ؛ i <= 10 ؛ i ++) {headerlist.add ("表头"+i) ؛ } قائمة <string> datarowlist = lists.newarrayList () ؛ لـ (int i = 1 ؛ i <= headerlist.size () ؛ i ++) {datarowlist.add ("数据"+i) ؛ } List <list <string >> datalist = lists.newarrayList () ؛ لـ (int i = 1 ؛ i <= 1000000 ؛ i ++) {datalist.add (datarowlist) ؛ } exportexcel ee = new esportexcel ("表格标题" ، headerlist) ؛ لـ (int i = 0 ؛ i <datalist.size () ؛ i ++) {row row = ee.addrow () ؛ لـ (int j = 0 ؛ j <datalist.get (i) .size () ؛ j ++) {ee.addcell (row ، j ، datalist.get (i) .get (j)) ؛ }} ee.writeFile ("Target/export.xlsx") ؛ ee.dispose () ؛ log.debug ("نجاح التصدير.") ؛ }ImportExcel.java
/*** حقوق الطبع والنشر © 2012-2014 <a href = "https://github.com/thinkgem/jeesite"> jeSite </a> جميع الحقوق محفوظة. */package com.thinkgem.jesite.common.Utils.excel ؛ استيراد java.io.file ؛ import java.io.fileinputStream ؛ استيراد java.io.ioException ؛ استيراد java.io.inputstream ؛ import java.lang.reflect.field ؛ import java.lang.reflect.method ؛ import java.util.collection ؛ java.util.list ؛ استيراد org.apache.commons.lang3.stringUtils ؛ استيراد org.apache.poi.hssf.usermodel.hssfworkbook ؛ استيراد org.apache.poi.openxml4j.exceptions.invalidformatexception ؛ import org.apache.poi.ss.usermodel.cell ؛ org.apache.poi.ss.usermodel.dateutil ؛ import org.apache.poi.ss.usermodel.row ؛ استيراد org.apache.poi.ss.usermodel.sheet ؛ import org.apache.poi.ss.usermodel.workbook ؛ import org.poi.xssssf.ussploy. org.slf4j.logger ؛ استيراد org.slf4j.loggerfactory ؛ استيراد org.springframework.web.multipart.multipartfile ؛ استيراد com.google.common.collect.lists ؛ import com.thinkgem.jeesite.common.Utils.Replections ؛ import com.thinkgem.jesite.common.Utils.excel.annotation.excelfield ؛ import com.thinkgem.jesite.modules.sys.tils.dictils ؛ /** * 导入 excel 文件 (支持 "XLS" 和 "XLSX" 格式 格式 * Author Thinkgem * @Varvent 2013-03-10 */public class importexcel {private static logger = loggerfactory.getlogger (isportexcel.class) ؛ / ** * 工作薄对象 */ Private Workbook WB ؛ / ** * 工作表对象 */ ورقة الورقة الخاصة ؛ / ** * 标题行号 */ private int headernum ؛ / ** * 构造函数 * param path 导入文件 , 读取第一个工作表 读取第一个工作表 * param headernum 标题行号 , 数据行号 数据行号 = 标题行号 +1 * athrows invalidformatexception * throws ioException */ public ImportExcel (اسم ملف السلسلة ، int headernum) يلقي alinalidformatexception ، ioexception {this (ملف جديد) ، headernum) ؛ } / ** * 构造函数 * param path 导入文件对象 , 读取第一个工作表 * param headernum 标题行号 , 数据行号 数据行号 = 标题行号 +1 * athrows invalidformatexception * throws ioException * / public importexcel (ملف الملف ، int headernum) يلقي alwaredformatexception ، ioexception {this (file ، headernum ، 0) ؛ } / ** * 构造函数 * param path 导入文件 * param headernum 标题行号 , 数据行号 数据行号 = 标题行号 +1 * param sheetIndex 工作表编号 * throws invalidformatexception * throws ioException * / public importExcel (filenment string ، int headernum ، int spootex) inlydformatexception ، io. } / ** * 构造函数 * param path 导入文件对象 * param headernum 标题行号 , 数据行号 数据行号 = 标题行号 +1 * param sheetIndex 工作表编号 * throws invalidformatexception * throws ioException * / public importexcel headernum ، sheetIndex) ؛ } / ** * 构造函数 * param file 导入文件对象 * param headernum 标题行号 , 数据行号 数据行号 = 标题行号 +1 * param sheetIndex 工作表编号 * throws invalidformatexception * throws ioException * / public Importexcel (multipartfile multipartfile ، int intindex) throws throws inflidatexception ، ioexception { هذا (multipartfile.getoriginalfilename () ، multipartfile.getInputStream () ، headernum ، sheetIndex) ؛ } / ** * 构造函数 * param path 导入文件对象 * param headernum 标题行号 , 数据行号 数据行号 = 标题行号 +1 * param sheetIndex 工作表编号 * throws invalidformatexception * athrows ioException (StringUtils.isblank (filename)) {رمي new runTimeException ("导入文档为空!") ؛ } if if (filename.toLowerCase (). endswith ("XLS")) {this.wb = new HSSfworkbook (IS) ؛ } آخر إذا (filename.toLowerCase (). endswith ("xlsx")) {this.wb = new xssfworkbook (is) ؛ } else {رمي new runTimeException ("文档格式不正确!") ؛ } if (this.wb.getNumberOfSheets () <sheetIndex) {رمي new runTimeException ("文档中没有工作表!") ؛ } this.sheet = this.wb.getsheetat (seindIndex) ؛ this.headernum = headernum ؛ log.debug ("تهيئة النجاح.") ؛ } / ** * 获取行对象 * param rownum * return * / public row getRow (int rownum) {return this.sheet.getRow (rownum) ؛ } / ** * 获取数据行号 * return * / public int getDatarownum () {return headernum+1 ؛ } / ** * 获取最后一个数据行号 * regurn * / public int getLastDatarOwnum () {return this.sheet.getLastRownum ()+headernum ؛ } / ** * 获取最后一个列号 * return * / public int getLastCellNum () {return this.getRow (headernum) .getlastcellnum () ؛ } / ** * 获取单元格值 * param row 获取的行 * param column 获取单元格列号 * return 单元格值 * / public object getCellValue (صف صف ، عمود int) {object val = "" ؛ حاول {cell cell = row.getCell (column) ؛ if (cell! = null) {if (cell.getCellType () == cell.cell_type_numeric) {val = cell.getNumericCellValue () ؛ } آخر إذا (cell.getCellType () == cell.cell_type_string) {val = cell.getStringCellValue () ؛ } آخر إذا (cell.getCellType () == cell.cell_type_formula) {val = cell.getCellFormula () ؛ } آخر إذا (cell.getCellType () == cell.cell_type_boolean) {val = cell.getBooleanCellValue () ؛ } آخر إذا (cell.getCellType () == cell.cell_type_error) {val = cell.geterrorcellvalue () ؛ }}} catch (استثناء e) {return val ؛ } إرجاع فال ؛ } / ** * 获取导入数据列表 * param cls 导入对象类型 * param مجموعات 导入分组 * / public <e> قائمة <e> getDatalist (class <e> cls ، int ... مجموعات) يلقي instantiationException ، alfictalAccessceception {list <object []> enoTationList = lists.newArayList () ؛ // الحصول على حقل التعليقات التوضيحية [] fs = cls.getDeclaredFields () ؛ لـ (Field F: FS) {Excelfield ef = f.getAnnotation (excelfield.class) ؛ if (ef! = null && (ef.type () == 0 || ef.type () == 2)) {if (المجموعات! = null && groups.length> 0) {boolean ingroup = false ؛ لـ (int g: المجموعات) {if (ingroup) {break ؛ } لـ (int efg: ef.groups ()) {if (g == efg) {ingroup = true ؛ incenotationlist.add (كائن جديد [] {ef ، f}) ؛ استراحة؛ }}}} آخر {incenotationList.add (كائن جديد [] {ef ، f}) ؛ }}} // الحصول على طريقة التعليقات التوضيحية [] ms = cls.getDeclaredMethods () ؛ لـ (method m: ms) {excelfield ef = m.getAnnotation (excelfield.class) ؛ if (ef! = null && (ef.type () == 0 || ef.type () == 2)) {if (المجموعات! = null && groups.length> 0) {boolean ingroup = false ؛ لـ (int g: المجموعات) {if (ingroup) {break ؛ } لـ (int efg: ef.groups ()) {if (g == efg) {ingroup = true ؛ incenotationlist.add (كائن جديد [] {ef ، m}) ؛ استراحة؛ }}}} آخر {incenotationList.add (كائن جديد [] {ef ، m}) ؛ }}} // مجموعة فرز الحقل //log.debug("Import count: "+anotationlist.size ()) ؛ // الحصول على قائمة بيانات Excel <E> Datalist = Lists.NewArrayList () ؛ لـ (int i = this.getDatarownum () ؛ i <this.getlastdatarownum () ؛ i ++) {e e = (e) cls.newinstance () ؛ عمود int = 0 ؛ صف الصف = this.getRow (i) ؛ StringBuilder sb = new StringBuilder () ؛ لـ (Object [] OS: enronotationList) {object val = this.getCellValue (row ، column ++) ؛ if (val! = null) {Excelfield ef = (excelfield) OS [0] ؛ // إذا كان نوع dict ، احصل على قيمة dict if (stringUtils.isNotBlank (ef.dicttype ())) {val = dictutils.getDictValue (val.toString () ، ef.dicttype () ، ") ؛ //log.debug("dictionary القيمة: ["+i+" ، "+colunm+"] "+val) ؛ } // احصل على نوع param ونوع الفئة المصبوب <؟> valtype = class.class ؛ if (OS [1] extryof field) {valtype = ((field) os [1]). gettype () ؛ } if if (OS [1] methodof method) {method method = ((method) os [1]) ؛ if ("get" .equals (method.getName (). } آخر إذا ("set" .equals (method.getName (). substring (0 ، 3))) {valtype = ((method) os [1]). getParameterTypes () [0] ؛ }} //log.debug("Import type: ["+i+" ، "+column+"] "+valtype) ؛ حاول {if (valtype == string.class) {string s = string.valueof (val.toString ()) ؛ if (stringUtils.endswith (s ، ".0")) {val = stringUtils.subStringBefore (s ، ".0") ؛ } آخر {val = string.valueof (val.toString ()) ؛ }} آخر إذا (valtype == integer.class) {val = double.valueof (val.toString ()). Intvalue () ؛ } آخر إذا (valtype == long.class) {val = double.valueof (val.toString ()). longvalue () ؛ } آخر إذا (valtype == double.class) {val = double.valueof (val.toString ()) ؛ } آخر إذا (valtype == float.class) {val = float.valueof (val.toString ()) ؛ } آخر إذا (valtype == date.class) {val = dateutil.getjavadate ((double) val) ؛ } آخر {if (ef.fieldtype ()! = class.class) {val = ef.fieldtype (). } else {val = class.forname (this.getClass (). }}} catch (استثناء ex) {log.info ("الحصول على قيمة الخلية ["+i+"،"+column+"] خطأ:"+ex.toString ()) ؛ val = null ؛ }. } آخر إذا (OS [1] methodof method) {string mthodname = ((method) os [1]). getName () ؛ if ("get" .equals (mthodname.substring (0 ، 3))) {mthodname = "set"+stringUtils.subStringAfter (mthodname ، "get") ؛ } Reflections.invokemethod (E ، Mthodname ، فئة جديدة [] {valtype} ، كائن جديد [] {val}) ؛ }} sb.append (val+"،") ؛ } datalist.add (e) ؛ log.debug ("اقرأ النجاح: ["+i+"]"+sb.toString ()) ؛ } عائد Datalist ؛ }}导入测试
الفراغ الثابت العام (سلسلة [] args) يلقي رمي {importexcel ei = new ImportExcel ("Target/export.xlsx" ، 1) ؛ لـ (int i = ei.getDatarOwnum () ؛ i <ei.getlastdatarownum () ؛ i ++) {row row = ei.getRow (i) ؛ لـ (int j = 0 ؛ j <ei.getlastcellnum () ؛ j ++) {object val = ei.getCellValue (row ، j) ؛ System.out.print (Val+"،") ؛ } system.out.print ("/n") ؛ }}