文件导入导出必须代码
Exportexcel.java
/ ** * Copyright © 2012-2014 <a href = "https://github.com/thinkgem/jeeesite"> Jeesite </a> Tous droits réservés. * / package com.thinkgem.jeeesite.common.utils.excel; import java.io.filenotfoundException; import java.io.fileoutputStream; import java.io.ioexception; import java.io.outputstream; import java.lang.reflect.field; import java.lang.reflect.method; import java.text.decimalformat; import java.uthod.collections; import; java.util.comparator; import java.util.date; import java.util.hashmap; import java.util.linkedhashmap; import java.util.list; import java.util.map; import javax.servlet.http.httpservletResponse; import org.apache.commons.lang3.stringutils; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.Cellstyle; import org.apache.poi.ss.susermodel.comment; import org.apache.poi.ss.Usmermodel.Dataformat; import; org.apache.poi.ssi org.apache.poi.ss.util.cellRangeaddress; import org.apache.poi.xssf.streaming.sxssfworkBook; import org.apache.poi.xssf.usermodel.xssfclientanchor; import org.apache.poi.xssf.UserModel.xssfrichThext org.slf4j.logger; import org.slf4j.loggerfactory; import com.google.common.collect.lists; import com.thinkgem.jeeesite.common.utils.dateutils; import com.thinkgem.jeeesite.common.utils.encodes; import com.thinkgem.jeeesite.common.utils.reflections; import com.thinkgem.jeeesite.common.utils.excel.annotation.excelfield; import com.thinkgem.jeeesite.modules.sys.utils.dictutiles; / ** * 导出 Excel 文件 (导出 导出 导出 导出 导出 导出 导出 `` xlsx "格式 , 支持大数据量导出 @see org.apache.poi.ss.spreadsheetversion) * @author thinkGem * @version 2013-04-21 * / public class exportexcel {private static logger log = loggerfactory.getlogger (exportExcelcel.class); / ** * 工作薄对象 * / / private sxssfworkbook wb; / ** * 工作表对象 * / feuille de feuille privée; / ** * 样式列表 * / Map privé <String, CellStyle> Styles; / ** * 当前行号 * / private int Rownum; / ** * 注解列表 (Object [] {ExcelField, champ / méthode}) * / list <objet []> annotationList = lists.newArrayList (); / ** * 字段 Map * / LinkedHashmap <String, String> fieldMap; Hashmap <string, string> dictTypes; / ** * 构造函数 * @param title 表格标题 , 传 “空值” , 表示无标题 * @param cls 实体对象 , 通过 annotation.exportfield 获取标题 * / public exportexcel (titre de chaîne, classe <?> CLS) {this (title, cls, 1); } / ** * 构造函数 * @param title 表格标题 , 传 ‘空值” , 表示无标题 * @param fieldmap , 获取要导出的字段和字段标题 * / public exportexcel (title de chaîne, liendhashmap <string, string> fieldmap) {this.fieldmap = fieldmap; dictTypes = new HashMap <String, String> (); // initialiser int colunm = 0; List <string> henterList = lists.newArrayList (); pour (String Key: fieldmap.KeySet ()) {String t = fieldmap.get (key); Hashmap <string, string> map = com.thinkgem.jeeesite.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 ++; headList.add (t); } initialize (titre, liste d'en-tête); } / ** * 构造函数 * @param title 表格标题 , 传 “空值” , 表示无标题 * @param cls 实体对象 , 通过 annotation.exportfield 获取标题 * @param type 导出类型 (1: 导出数据; 2 : 导出模板) * @param Groupes 导入分组 * * / public ExportExcel (Get Titre, Class <?> CLS, int, int ... cls.getDeclaredFields (); pour (champ f: fs) {ExcelField ef = f.getAnnotation (ExceLfield.class); if (ef! = null && (ef.type () == 0 || ef.type () == type)) {if (groupes! = null && groupes.length> 0) {boolean ingroup = false; pour (int g: groupes) {if (ingroup) {break; } pour (int efg: ef.groups ()) {if (g == efg) {ingroup = true; annotationList.add (nouvel objet [] {ef, f}); casser; }}}} else {annotationList.add (nouvel objet [] {ef, f}); }}} // Méthode de la méthode d'annotation [] ms = cls.getDeclaredMethods (); pour (méthode m: ms) {excifield ef = m.getAnnotation (excifield.class); if (ef! = null && (ef.type () == 0 || ef.type () == type)) {if (groupes! = null && groupes.length> 0) {boolean ingroup = false; pour (int g: groupes) {if (ingroup) {break; } pour (int efg: ef.groups ()) {if (g == efg) {ingroup = true; annotationList.add (nouvel objet [] {ef, m}); casser; }}}} else {annotationList.add (nouvel objet [] {ef, m}); }}} // Collection de tri de champs.sort (AnnotationList, nouveau comparateur <objet []> () {public int compare (objet [] o1, objet [] o2) {return nouvel entier (((excifield) o1 [0]). Sort ()). Compareto (nouvel int // Initialize list <string> henterList = lists.newArrayList (); for (object [] os: annotationList) {String t = ((excifield) os [0]). title (); // 如果是导出 , 则去掉注释 if (type == 1) {string [] ss = stringUtils.split (t, "**", 2); if (ss.length == 2) {t = ss [0]; }} headList.add (t); } initialize (titre, liste d'en-tête); } / ** * 构造函数 * @param title 表格标题 , 传 ‘空值” , 表示无标题 * @param en-têtes 表头数组 * / public Exportexcel (title de chaîne, chaîne [] en-têtes) {initialize (title, listS.NewArrayList (Headers)); } / ** * 构造函数 * @param title 表格标题 , 传 ‘空值” , 表示无标题 * @param la liste d'en-tête 表头列表 * / public exportexcel (title de chaîne, liste <string> en tête) {initialize (title, en tête); } / ** * 初始化函数 * @param title 表格标题 , 传 “空值” , 表示无标题 * @param la liste d'en-tête 表头列表 * / private void initialize (string title, list <string> headlist) {this.wb = new sxssfworkbook (500); this.sheet = wb.createSheet ("export"); this.styles = création (wb); // Créer un titre if (stringUtils.isnotblank (title)) {row titlerrow = sheet.createrow (Rownum ++); Titlerow.Setheightinpoints (30); Cell titleCell = titlerow.createCell (0); titlecell.setCellStyle (Styles.get ("Title")); titlecell.setCellValue (titre); sheet.addmergedRegion (new CellRangeAddress (titlerow.getRownum (), titlerow.getRownum (), titlerrow.getRownum (), headList.size () - 1)); } // Créer un en-tête if (headlist == null) {throw new RuntimeException ("HeaderList Not Null!"); } Row headerrow = sheet.createrow (Rownum ++); Headerrow.Setheightinpoints (16); for (int i = 0; i <headlist.size (); i ++) {Cell cell = headerrow.createCell (i); Cell.setCellStyle (Styles.get ("En-tête")); String [] ss = stringUtils.split (HeaderList.get (i), "**", 2); if (ss.length == 2) {Cell.SetCellValue (ss [0]); Commentaire commentaire = this.sheet.createdrawingpatriarch (). CreateCellComment (nouveau xssfclientanchor (0, 0, 0, 0, (court) 3, 3, (court) 5, 6)); comment.SetString (nouveau xssfrichTextString (ss [1])); Cell.SetCellComment (commentaire); } else {Cell.SetCellValue (HeaderList.get (i)); } sheet.autoSizEColumn (i); } pour (int i = 0; i <headList.size (); i ++) {int colwidth = sheet.getColumnwidth (i) * 2; sheet.setColumnWidth (i, colwidth <3000? 3000: colwidth); } log.debug ("Initialiser le succès."); } / ** * 创建表格样式 * @param wb 工作薄对象 * @return 样式列表 * / map private <String, CellStyle> Createstyles (Workbook wb) {map <string, cellStyle> Styles = new HashMap <String, CellStyle> (); CellStyle Style = wb.CreateCellStyle (); style.setAlignment (cellstyle.align_center); style.setverticalAdignment (cellstyle.vertical_center); Font titlefont = wb.createfont (); titlefont.setFontName ("Arial"); TitleFont.setFontheightInpoints ((court) 16); titlefont.setboldweight (font.boldweight_bold); style.setfont (titlefont); Styles.put ("titre", style); style = wb.createCellStyle (); style.setverticalAdignment (cellstyle.vertical_center); style.setborderright (cellstyle.border_thin); style.setrightborderColor (indededColors.grey_50_percent.getIndex ()); style.setborderleft (cellstyle.border_thin); style.setLeftborDerColor (indededColors.grey_50_percent.getIndex ()); style.setbordertop (cellstyle.border_thin); style.setTopBorderColor (indededColors.grey_50_percent.getIndex ()); style.setborderbottom (cellstyle.border_thin); style.setbottomberColor (indededColors.grey_50_percent.getIndex ()); FONT DATAFONT = WB.CREATEFONT (); datafont.setFontName ("Arial"); datafont.setFontheightInpoints ((court) 10); style.setfont (datafont); Styles.put ("données", style); 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 (indexedColors.grey_50_percent.getIndex ()); style.setFillPattern (cellStyle.solid_foreground); FONT HEADERFONT = WB.CREATEFONT (); headerFont.setFontName ("Arial"); headerFont.setFontheightInpoints ((court) 10); headerfont.setboldweight (font.boldweight_bold); headerfont.setColor (indededColors.white.getIndex ()); style.setfont (headerFont); Styles.put ("En-tête", style); Styles de retour; } / ** * 添加一行 * @return 行对象 * / public row addRow () {return sheet.createrow (rownum ++); } / ** * 添加一个单元格 * @param row 添加的行 * @param colonne 添加列号 * @param val 添加值 * @return 单元格对象 * / public cellule addcell (ligne de ligne, colonne int, objet val) {return this.addcell (row, colonne, val, 0, class.class); } / ** * 添加一个单元格 * @param row 添加的行 * @param colonne 添加列号 * @param val 添加值 * @param align 对齐方式 (1 : 靠左; 2 : 居中; 3 : 靠右) * @return 单元格对象 * / public cellule addcell (ligne de ligne, intrance int, objet val, int align, class <> fieldType) {cell cell = row..createCell (colonne); CellStyle Style = Styles.get ("Data" + (align> = 1 && align <= 3? Align: "")); essayez {if (val == null) {cell.setCellValue (""); } else if (val instanceof string) {Cell.setCellValue ((String) val); } else if (Val instanceof Integer) {Cell.SetCellValue ((Integer) Val); } else if (val instanceof long) {Cell.setCellValue ((long) val); } else if (Val instanceof double) {cell.setCellValue (new Decimalformat (". #####"). Format (Val)); } else if (Val instanceof float) {cell.setCellValue ((float) val); } else if (Val instanceof date) {dataformat format = wb.createdataformat (); style.setDataFormat (format.getFormat ("yyyy-mm-dd")); Cell.SetCellValue (dateUtils.formatDateTime ((Date) Val)); } else {if (fieldType! = class.class) {Cell.SetCellValue ((String) fieldType.getMethod ("setValue", object.class) .invoke (null, val)); } else {Cell.SetCellValue ((String) class.forname (this.getClass (). getName (). RempaceALL (this.getClass (). getImPlenName (), "FieldType." + Val.getClass (). GetImPlename () + "Type")). GetMethod); }}} catch (exception ex) {log.info ("set la valeur de la cellule [" + row.getRownum () + "," + colonne + "] Erreur:" + ex.ToString ()); Cell.SetCellValue (val.toString ()); } Cell.SetCellStyle (style); cellule de retour; } / ** * 添加数据 (通过 annotation.exportfield 添加数据) * @return list 数据列表 * / public <e> Exportexcel SetDatalist (list <e> list) {for (e e: list) {int colunm = 0; Row Row = this.AdDrow (); StringBuilder sb = new StringBuilder (); if (e instanceOf map) {@SuppressWarnings ("Unchecked") map <string, object> map = (map <string, object>) e; For (String Key: fieldmap.KeySet ()) {objet Value = map.get (key); String ColumnDictType = DictTypes.get (ColunM + ""); if (stringUtils.isnotblank (ColumnDictType)) {value = dictUtils.getDictLabel (valeur == null? "": value.toString (), ColumnDictType, ""); } this.addcell (row, colunm ++, value == null? "": value.toString (), 0, string.class); sb.append (valeur + ","); }} else {for (object [] os: annotationList) {excifield ef = (excifield) os [0]; Objet val = null; // Obtenez la valeur de l'entité try {if (stringUtils.isnotblank (ef.value ())) {val = réflexe.InvokeGetter (e, ef.value ()); } else {if (os [1] instanceof field) {val = réflexes.InvokeGetter (e, ((champ) os [1]). getName ()); } else if (os [1] instanceof méthode) {val = réflexes.Invokemethod (e, ((méthode) os [1]). getName (), new class [] {}, nouvel objet [] {}); }} // Si est dict, obtenez un étiquette dict if (stringUtils.isnotblank (ef.dictType ())) {val = dictUtils.getDictLabel (val == null? "": Val.toString (), ef.dicttype (), "); }} catch (exception ex) {// défaut d'ignorer log.info (ex.ToString ()); val = ""; } this.addcell (row, colunm ++, val, ef.align (), ef.fieldType ()); SB.APPEND (Val + ","); } log.debug ("Écrire un succès: [" + row.getRownum () + "]" + sb.toString ()); }} renvoie ceci; } / ** * 输出数据流 * @param OS 输出数据流 * / public Exportexcel Write (OutputStream OS) lève ioException {wb.write (OS); retourner ceci; } / ** * 输出到客户端 * @param nom de fichier 输出文件名 * / public exportexcel write (httpServletResponse réponse, string filename) lève ioException {réponse.reset (); Response.SetContentType ("Application / OCTET-Stream; charSet = UTF-8"); Response.sethEader ("Content-Disposition", "attachement; filename =" + Encodes.Urlencode (FileName)); écriture (réponse.getOutputStream ()); retourner ceci; } / ** * 输出到文件 * @param nom de fichier 输出文件名 * / public exportexcel writeFile (String name) lève filenotfoundException, ioException {fileOutputStream os = new FileOutputStream (name); this.write (os); retourner ceci; } / ** * 清理临时文件 * / public exportexcel dissose () {wb.dispose (); retourner ceci; }}导出测试
public static void main (string [] args) lève le throwable {list <string> henterList = lists.newArrayList (); for (int i = 1; i <= 10; i ++) {headList.add ("表头" + i); } List <string> datarowlist = lists.newArrayList (); for (int i = 1; i <= headList.size (); i ++) {datarowlist.add ("数据" + i); } List <list <string>> datalist = lists.newArrayList (); pour (int i = 1; i <= 1000000; i ++) {datalist.add (datarowlist); } Exportexcel ee = new exportexcel ("表格标题", en tête); pour (int i = 0; i <datalist.size (); i ++) {row row = ee.addrow (); pour (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 ("Export Success."); }ImportExcel.java
/ ** * Copyright © 2012-2014 <a href = "https://github.com/thinkgem/jeeesite"> Jeesite </a> Tous droits réservés. * / package com.thinkgem.jeeesite.common.utils.excel; Importer java.io.file; import java.io.fileInputStream; Importer java.io.ioException; import java.io.inputstream; import java.lang.reflect.field; import java.lang.reflect.method; import java.util.date; import java.util.comParator; import java.util.date; import; java.util.list; import org.apache.commons.lang3.stringutils; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.openxml4j.exceptions.invalidformatexception; import org.apache.poi.ss.userModel.Cell; Import org.apache.poi.ss.usermodel.dateutil; import org.apache.poi.ss.usermodel.row; import org.apache.poi.ss.usermodel.sheet; import org.apache.poi.ss.usermodel.workbook; import org.apache. org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.web.multiplart.multipartfile; Import com.google.common.collect.lists; import com.thinkgem.jeeesite.common.utils.reflections; import com.thinkgem.jeeesite.common.utils.excel.annotation.excellifield; import com.thinkgem.jeesesi.modules.sys.utils.Dictutiles; / ** * 导入 Excel 文件 (支持 支持 支持 支持 支持 支持 支持 `` xls "和 和` `xlsx" 格式) * @author thinkgem * @version 2013-03-10 * / classe publique importExcel {private static logger log = loggerfactory.getLogger (importExcel.class); / ** * 工作薄对象 * / classeur privé wb; / ** * 工作表对象 * / feuille de feuille privée; / ** * 标题行号 * / private int Headernum; / ** * 构造函数 * @param chemin 导入文件 , 读取第一个工作表 * @param Headernum 标题行号 , 数据行号 = 标题行号 +1 * @throws invalidformatexception * @throws ioexception * / public importExcel (string filename, int Headhernum) lance invalidformatexception, ioException {this (nouveau fichier (fichier), Headermum); } / ** * 构造函数 * @param chemin 导入文件对象 , 读取第一个工作表 * @param Headernum 标题行号 , 数据行号 = 标题行号 +1 * @throws invalidformatexception * @throws ioException * / public importExcel (fichier de fichier, int headhernum) lance invalidformatexception, ioException {this (fichier, headernum, 0); } / ** * 构造函数 * @param path 导入文件 * @param headhernum 标题行号 , 数据行号 = 标题行号 +1 * @param sheetIndex 工作表编号 * @throws invalidformatexception * @throws ioException * / public ImportExcel (String FileName, int headnum, int sheetIndEx) lance Invalidformatexception, ioException {this (nouveau file (filename), invalidformuner, ioexception {this (nouveau file (filename), invalidformum, ioexception {this (nouveau file (filename), invalidformuner); } / ** * 构造函数 * @param chemin 导入文件对象 * @param Headernum 标题行号 , 数据行号 = 标题行号 +1 * @param sheetIndex 工作表编号 * @throws invalidformatexception * @throws ioException * / public importExcel (fichier file, int Headernum, inthestindex) Headernum, SheetIndex); } / ** * 构造函数 * @param fichier 导入文件对象 * @param headhernum 标题行号 , 数据行号 = 标题行号 +1 * @param sheetIndex 工作表编号 * @throws invalidformatexception * @throws ioException * / public importExcel (multipartfile multipartfile, Inthernum, int sheetIndex) lance invalidformatexception, ioexception { this (multipartFile.getoriginalFileName (), multipartFile.getInputStream (), Headernum, SheetIndex); } / ** * 构造函数 * @param path 导入文件对象 * @param headhernum 标题行号 , 数据行号 = 标题行号 +1 * @param sheetIndex 工作表编号 * @throws invalidformatexception * @throws ioException * / public importExcel (String filename, inputStream is, int headnum, inthened) throws invalidformatexception, ioexception { (StringUtils.isblank (nom de fichier)) {lancez new RuntimeException ("导入文档为空!"); } else if (filename.tolowercase (). Endswith ("xls")) {this.wb = new hssfworkbook (is); } else if (filename.tolowercase (). Endswith ("xlsx")) {this.wb = new xssfworkbook (is); } else {lancer une nouvelle RuntimeException ("文档格式不正确!"); } if (this.wb.getNumberOfSheets () <egeDIndex) {Throw New RuntimeException ("文档中没有工作表!"); } this.sheet = this.wb.getSheetAt (sheetIndex); this.henernum = headhernum; Log.Debug ("Initialiser le succès."); } / ** * 获取行对象 * @param Rownum * @return * / public Row getrow (int rownum) {return this.sheet.getRow (rownum); } / ** * 获取数据行号 * @return * / public int getDatarown () {return Headernum + 1; } / ** * 获取最后一个数据行号 * @return * / public int getlastDatarown () {return this.sheet.getSastRowm () + Headernum; } / ** * 获取最后一个列号 * @return * / public int getlastCellnum () {return this.getRow (headernum) .getLastCellnum (); } / ** * 获取单元格值 * @param row 获取的行 * @param colonne 获取单元格列号 * @return 单元格值 * / objet public getCellValue (Row Row, int colonne) {objet val = ""; try {Cell Cell = row.getCell (colonne); if (cell! = null) {if (cell.getCellType () == Cell.Cell_Type_Numeric) {val = cell.getNumericCellValue (); } else if (Cell.getCellType () == Cell.Cell_Type_String) {val = cell.getStringCellValue (); } else if (Cell.getCellType () == Cell.Cell_Type_Formula) {val = cell.getCellFormula (); } else if (Cell.getCellType () == Cell.Cell_Type_Boolean) {val = cell.getBooleanCellValue (); } else if (Cell.getCellType () == Cell.Cell_Type_error) {val = cell.getErrEllValue (); }}} catch (exception e) {return val; } return val; } / ** * 获取导入数据列表 * @param CLS 导入对象类型 * @param Groupes 导入分组 * / public <e> list <e> getDatalist (class <e> cls, int ... groupes) lève InstantiationException, illégalaccessException {list <objet []> annotationList = listS.NewarRayList (); // Get Annotation Field Field [] fs = cls.getDeclaredFields (); pour (champ f: fs) {ExcelField ef = f.getAnnotation (ExceLfield.class); if (ef! = null && (ef.type () == 0 || ef.type () == 2)) {if (groupes! = null && groupes.length> 0) {boolean ingroup = false; pour (int g: groupes) {if (ingroup) {break; } pour (int efg: ef.groups ()) {if (g == efg) {ingroup = true; annotationList.add (nouvel objet [] {ef, f}); casser; }}}} else {annotationList.add (nouvel objet [] {ef, f}); }}} // Méthode de la méthode d'annotation [] ms = cls.getDeclaredMethods (); pour (méthode m: ms) {excifield ef = m.getAnnotation (excifield.class); if (ef! = null && (ef.type () == 0 || ef.type () == 2)) {if (groupes! = null && groupes.length> 0) {boolean ingroup = false; pour (int g: groupes) {if (ingroup) {break; } pour (int efg: ef.groups ()) {if (g == efg) {ingroup = true; annotationList.add (nouvel objet [] {ef, m}); casser; }}}} else {annotationList.add (nouvel objet [] {ef, m}); }}} // Collection de tri de champs.sort (AnnotationList, nouveau comparateur <objet []> () {public int compare (objet [] o1, objet [] o2) {return nouvel entier (((excifield) o1 [0]). Sort ()). Compareto (nouvel int //log.debug("Import Colonne Count: "+ annotationList.size ()); // obtient Excel Data List <E> datalist = listS.NewArrayList (); for (int i = this.getDatarownm (); i <this.getlastDatarown (); i ++) {e e = (e) cls.newinstance (); int colonne = 0; Row Row = this.getRow (i); StringBuilder sb = new StringBuilder (); for (object [] os: annotationList) {object val = this.getCellValue (row, colonne ++); if (val! = null) {ExcelField ef = (excifield) os [0]; // Si est un type de dict, obtenez une valeur dict if (stringUtils.isnotblank (ef.dictType ())) {val = dictUtils.getDictValue (val.toString (), ef.dictType (), ""); //log.debug("dictionary Type Value: ["+ i +", "+ colunm +"] "+ val); } // Get Param Type et Type Cast Class <?> Valtype = class.class; if (os [1] InstanceOf field) {valtype = ((champ) os [1]). getType (); } else if (os [1] instanceof méthode) {méthode méthode = ((méthode) os [1]); if ("get" .equals (method.getName (). substring (0, 3))) {valtype = méthode.getReturnType (); } else if ("set" .equals (method.getName (). substring (0, 3))) {valtype = ((méthode) os [1]). getParameterTypes () [0]; }} //log.debug("Import Value Type: ["+ i +", "+ colonne +"] "+ valtype); try {if (valtype == string.class) {String s = string.valueof (val.toString ()); if (stringUtils.endswith (s, ".0")) {val = stringUtils.substringBefore (s, ".0"); } else {val = string.valueof (val.toString ()); }} else if (valtype == Integer.class) {val = double.valueof (val.toString ()). intValue (); } else if (valtype == long.class) {val = double.valueof (val.toString ()). longValue (); } else if (valtype == double.class) {val = double.valueof (val.toString ()); } else if (valtype == float.class) {val = float.valueof (val.toString ()); } else if (valtype == date.class) {val = dateUtil.getjavadate ((double) val); } else {if (ef.fieldType ()! = class.class) {val = ef.fieldType (). getMethod ("getValue", string.class) .invoke (null, val.toString ()); } else {val = class.forname (this.getClass (). getName (). RempaceALL (this.getClass (). getImpLename (), "fieldType." + Valtype.getSimplename () + "type")). GetMethod ("getValue", string.class) .invoke (null, val.tosTring ()); }}} catch (exception ex) {log.info ("Get Cell Value [" + i + "," + colonne + "] Erreur:" + ex.ToString ()); val = null; } // Définir la valeur de l'entité if (OS [1] InstanceOf Field) {Reflections.InvokeSetter (E, ((Field) OS [1]). getName (), Val); } else if (os [1] instanceof méthode) {String mThodName = ((méthode) os [1]). getName (); if ("get" .equals (MTHODNAME.SUBSTRING (0, 3))) {MTHODNAME = "Set" + StringUtils.SubStringafter (MTHODNAME, "Get"); } Réflexes.InvokeMethod (e, mthodname, new class [] {valtype}, nouvel objet [] {val}); }} sb.append (val + ","); } datalist.add (e); log.debug ("lire le succès: [" + i + "]" + sb.toString ()); } return datalist; }}导入测试
public static void main (String [] args) lève lance-throws {importExcel ei = new ImportExcel ("cible / export.xlsx", 1); for (int i = ei.getDatarownm (); i <ei.getlastDatarown (); i ++) {row row = ei.getRow (i); pour (int j = 0; j <ei.getlastCellnum (); j ++) {objet val = ei.getCellValue (row, j); System.out.print (Val + ","); } System.out.print ("/ n"); }}