您的位置:首页 > 编程语言 > Java开发

Java 使用poi导入excel,结合xml文件进行数据验证的例子

2014-09-16 15:31 621 查看

假设现在要做一个通用的导入方法:

要求:

1.xml的只定义数据库表中的column字段,字段类型,是否非空等条件。

2.excel定义成模板,里面只填写了所需要的数据,有可能数据有问题。

3.在导入的时候就需要对每个excel单元格的数据进行验证。

4.验证完之后,若所有数据正确,那么批量保存。若有一点点错误,就不执行保存操作,并提示错误原因。

思路:

1.完美使用了Map的功能,先将xml中的数据存入map中,怎么存呢?

下面我根据xml文件来具体分析:(为图方便,我只做了字段的非空验证)

user.xml

[html]view plaincopy
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <excel>
  3. <entityname="用户表"code="user">
  4. <columnname="状态"code="status"type="String"></column>
  5. <columnname="端口号"code="port"type="int">
  6. <rules>
  7. <rulename="nullable"message="端口号不允许为空"></rule>
  8. </rules>
  9. </column>
  10. <columnname="IP地址"code="ip"type="String">
  11. <rules>
  12. <rulename="nullable"message="IP地址不允许为空"></rule>
  13. </rules>
  14. </column>
  15. <columnname="密码"code="password"type="String">
  16. <rules>
  17. <rulename="nullable"message="密码不允许为空"></rule>
  18. </rules>
  19. </column>
  20. <columnname="用户名"code="username"type="String"></column>
  21. <columnname="员工号"code="no"type="String">
  22. <rules>
  23. <rulename="nullable"message="员工号不允许为空"></rule>
  24. <rulename="checkUnique"message="员工号已经存在"></rule>
  25. </rules>
  26. </column>
  27. <columnname="头像"code="userImage"type="BLOB"></column>
  28. </entity>
  29. </excel>
根据xml所做的准备:

准备4个Map:

(1),已知 <entity> 中的name="用户表" ,定义entityMap 来存放实体类的map对象

(2),已知 “用户表”和 某个字段名“员工号”,那么就可以存放每一列的map对象

(3),已知 “用户表”和 某个字段名“员工号”,可以找到该列下的所有验证规则存放到map中

(4),已知 “用户表” 和 “ 员工号”和验证规则name "nullable",那么可以找到每一列的某一个验证规则


2.读取excel数据时,需要一一对应xml map中的字段与验证规则。

下面是excel数据:标注红色 * 号的表示必填项。


接下来就要看具体的实现代码了:

东西很多,我只贴两个比较重要的java 类

1.ParseExcelUtil.java ,要试验代码,可以直接在工程里面单击右键--run as 运行这个类,不过前提是要导入这个测试项目,最后面我会上传。

[java]view plaincopy
  1. packagecom.karen.test2;
  2. importjava.beans.IntrospectionException;
  3. importjava.io.File;
  4. importjava.io.FileInputStream;
  5. importjava.io.FileNotFoundException;
  6. importjava.io.IOException;
  7. importjava.lang.reflect.InvocationTargetException;
  8. importjava.util.ArrayList;
  9. importjava.util.HashMap;
  10. importjava.util.List;
  11. importjava.util.Map;
  12. importorg.apache.poi.hssf.usermodel.HSSFCell;
  13. importorg.apache.poi.hssf.usermodel.HSSFDateUtil;
  14. importorg.apache.poi.hssf.usermodel.HSSFRow;
  15. importorg.apache.poi.hssf.usermodel.HSSFSheet;
  16. importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
  17. importcom.karen.database.Dao;
  18. /**
  19. *解析excel工具类
  20. *@authorPCCW
  21. *
  22. */
  23. @SuppressWarnings("rawtypes")
  24. publicclassParseExcelUtil{
  25. publicFileInputStreamfis;
  26. publicHSSFWorkbookworkBook;
  27. publicHSSFSheetsheet;
  28. publicParseXMLUtilparseXmlUtil;
  29. publicStringBuffererrorString;
  30. /**当前实体类的code**/
  31. publicStringcurEntityCode;
  32. /**表头map对象:key:entityCode,value:headMap(index,headTitle)**/
  33. publicMapcurEntityHeadMap;
  34. /**字段的必填:key:entityCode+headTitle,value:true(必填),false(不必填)**/
  35. publicMapcurEntityColRequired;
  36. /**存放每一行的数据**/
  37. publicListlistDatas;
  38. publicParseExcelUtil(FileexcelFile,FilexmlFile){
  39. try{
  40. if(excelFile==null){
  41. thrownewFileNotFoundException();
  42. }
  43. fis=newFileInputStream(excelFile);
  44. workBook=newHSSFWorkbook(fis);
  45. parseXmlUtil=newParseXMLUtil(xmlFile);
  46. errorString=newStringBuffer();
  47. readExcelData();
  48. }catch(FileNotFoundExceptione){
  49. e.printStackTrace();
  50. }catch(IOExceptione){
  51. e.printStackTrace();
  52. }
  53. }
  54. /**开始从excel读取数据**/
  55. publicvoidreadExcelData(){
  56. intsheetSize=workBook.getNumberOfSheets();
  57. for(inti=0;i<sheetSize;i++){
  58. sheet=workBook.getSheetAt(i);
  59. StringentityName=workBook.getSheetName(i);
  60. readSheetData(sheet,entityName);
  61. }
  62. }
  63. /**读每个sheet页的数据**/
  64. publicvoidreadSheetData(HSSFSheetsheet,StringentityName){
  65. introwNumbers=sheet.getPhysicalNumberOfRows();
  66. Mapent=(Map)parseXmlUtil.getEntityMap().get(entityName);
  67. this.setCurEntityCode((String)ent.get("code"));
  68. if(rowNumbers==0){
  69. System.out.println("================excel中数据为空!");
  70. errorString.append(ParseConstans.ERROR_EXCEL_NULL);
  71. }
  72. ListcolList=(List)parseXmlUtil.getColumnListMap().get(entityName);
  73. intxmlRowNum=colList.size();
  74. HSSFRowexcelRow=sheet.getRow(0);
  75. intexcelFirstRow=excelRow.getFirstCellNum();
  76. intexcelLastRow=excelRow.getLastCellNum();
  77. if(xmlRowNum!=(excelLastRow-excelFirstRow)){
  78. System.out.println("==================xml列数与excel列数不相符,请检查");
  79. errorString.append(ParseConstans.ERROR_EXCEL_COLUMN_NOT_EQUAL);
  80. }
  81. readSheetHeadData(sheet);
  82. readSheetColumnData(sheet,entityName);
  83. }
  84. /**读取sheet页中的表头信息**/
  85. @SuppressWarnings({"unchecked","static-access"})
  86. publicvoidreadSheetHeadData(HSSFSheetsheet){
  87. MapheadMap=newHashMap();
  88. curEntityHeadMap=newHashMap();
  89. curEntityColRequired=newHashMap();
  90. HSSFRowexcelheadRow=sheet.getRow(0);
  91. intexcelLastRow=excelheadRow.getLastCellNum();
  92. StringheadTitle="";
  93. for(inti=0;i<excelLastRow;i++){
  94. HSSFCellcell=excelheadRow.getCell(i);
  95. headTitle=this.getStringCellValue(cell).trim();
  96. if(headTitle.endsWith("*")){
  97. curEntityColRequired.put(this.getCurEntityCode()+"_"+headTitle,true);
  98. }else{
  99. curEntityColRequired.put(this.getCurEntityCode()+"_"+headTitle,false);
  100. }
  101. headMap.put(i,headTitle);
  102. }
  103. curEntityHeadMap.put(this.getCurEntityCode(),headMap);
  104. }
  105. /**读取sheet页里面的数据**/
  106. @SuppressWarnings({"unchecked","static-access"})
  107. publicvoidreadSheetColumnData(HSSFSheetsheet,StringentityName){
  108. HSSFRowexcelheadRow=sheet.getRow(0);
  109. intexcelLastcell=excelheadRow.getLastCellNum();//excel总列数
  110. intexcelRowNum=sheet.getLastRowNum();//excel总行数
  111. MapheadMap=(Map)this.getCurEntityHeadMap().get(this.getCurEntityCode());
  112. MapcolMap=parseXmlUtil.getColumnMap();
  113. listDatas=newArrayList();
  114. for(inti=1;i<excelRowNum+1;i++){//行循环
  115. HSSFRowcolumnRow=sheet.getRow(i);
  116. if(columnRow!=null){
  117. MapcurRowCellMap=newHashMap();
  118. for(intj=0;j<excelLastcell;j++){//列循环
  119. intcout=headMap.get(j).toString().indexOf("*");
  120. StringheadTitle="";
  121. if(cout==-1){
  122. headTitle=headMap.get(j).toString();
  123. }else{
  124. headTitle=headMap.get(j).toString().substring(0,cout);
  125. }
  126. MapcurColMap=(Map)colMap.get(entityName+"_"+headTitle);
  127. StringcurColCode=(String)curColMap.get("code");
  128. StringcurColType=(String)curColMap.get("type");
  129. HSSFCellcolCell=columnRow.getCell(j);
  130. Stringvalue=this.getStringCellValue(colCell);
  131. if(value!=null){
  132. value=value.trim();
  133. }
  134. StringxmlColType=(String)curColMap.get("type");
  135. if(xmlColType.equals("int")){
  136. intintVal=Integer.valueOf(value);
  137. curRowCellMap.put(curColCode,intVal);//将这一行的数据以code-value的形式存入map
  138. }else{
  139. curRowCellMap.put(curColCode,value);
  140. }
  141. /**验证cell数据**/
  142. validateCellData(i+1,j+1,colCell,entityName,headTitle,curColType);
  143. }
  144. listDatas.add(curRowCellMap);
  145. }
  146. }
  147. if(this.getErrorString().length()==0){//如果没有任何错误,就保存
  148. saveExcelData(entityName);
  149. System.out.println("导入数据成功!");
  150. }else{
  151. //清理所有的缓存clearMap();现在暂时未清理
  152. String[]strArr=errorString.toString().split("<br>");
  153. for(Strings:strArr){
  154. System.out.println(s);
  155. }
  156. }
  157. }
  158. /**验证单元格数据**/
  159. @SuppressWarnings("static-access")
  160. publicvoidvalidateCellData(intcurRow,intcurCol,HSSFCellcolCell,StringentityName,StringheadName,StringcurColType){
  161. ListrulList=(List)parseXmlUtil.getColumnRulesMap().get(entityName+"_"+headName);
  162. if(rulList!=null&&rulList.size()>0){
  163. for(inti=0;i<rulList.size();i++){
  164. MaprulM=(Map)rulList.get(i);
  165. StringrulName=(String)rulM.get("name");
  166. StringrulMsg=(String)rulM.get("message");
  167. StringcellValue=this.getStringCellValue(colCell).trim();
  168. if(rulName.equals(ParseConstans.RULE_NAME_NULLABLE)){
  169. if(cellValue.equals("")||cellValue==null){
  170. errorString.append("第"+curRow+"行,第"+curCol+"列:"+rulMsg+"<br>");
  171. }
  172. }else{
  173. //////这里写其他的验证规则。。。
  174. }
  175. }
  176. }
  177. }
  178. /**保存excel里面的数据**/
  179. @SuppressWarnings("unchecked")
  180. publicvoidsaveExcelData(StringentityName){
  181. List<User>users=newArrayList();
  182. for(inti=0;i<this.getListDatas().size();i++){
  183. MapexcelCol=(Map)this.getListDatas().get(i);//得到第i行的数据
  184. Useruser=newUser();
  185. try{
  186. Userobj=(User)BeanToMapUtil.convertMap(user.getClass(),excelCol);
  187. users.add(obj);
  188. }catch(IntrospectionExceptione){
  189. e.printStackTrace();
  190. }catch(IllegalAccessExceptione){
  191. e.printStackTrace();
  192. }catch(InstantiationExceptione){
  193. e.printStackTrace();
  194. }catch(InvocationTargetExceptione){
  195. e.printStackTrace();
  196. }
  197. }
  198. /**批量保存数据**/
  199. Daodao=newDao();
  200. for(inti=0;i<users.size();i++){
  201. try{
  202. dao.saveUser(users.get(i));
  203. }catch(Exceptione){
  204. e.printStackTrace();
  205. }
  206. }
  207. }
  208. /**
  209. *获得单元格字符串
  210. *@throwsUnSupportedCellTypeException
  211. */
  212. publicstaticStringgetStringCellValue(HSSFCellcell){
  213. if(cell==null){
  214. returnnull;
  215. }
  216. Stringresult="";
  217. switch(cell.getCellType()){
  218. caseHSSFCell.CELL_TYPE_BOOLEAN:
  219. result=String.valueOf(cell.getBooleanCellValue());
  220. break;
  221. caseHSSFCell.CELL_TYPE_NUMERIC:
  222. if(HSSFDateUtil.isCellDateFormatted(cell)){
  223. java.text.SimpleDateFormatTIME_FORMATTER=newjava.text.SimpleDateFormat(
  224. "yyyy-MM-dd");
  225. result=TIME_FORMATTER.format(cell.getDateCellValue());
  226. }
  227. else{
  228. doubledoubleValue=cell.getNumericCellValue();
  229. result=""+doubleValue;
  230. }
  231. break;
  232. caseHSSFCell.CELL_TYPE_STRING:
  233. if(cell.getRichStringCellValue()==null){
  234. result=null;
  235. }
  236. else{
  237. result=cell.getRichStringCellValue().getString();
  238. }
  239. break;
  240. caseHSSFCell.CELL_TYPE_BLANK:
  241. result=null;
  242. break;
  243. caseHSSFCell.CELL_TYPE_FORMULA:
  244. try{
  245. result=String.valueOf(cell.getNumericCellValue());
  246. }catch(Exceptione){
  247. result=cell.getRichStringCellValue().getString();
  248. }
  249. break;
  250. default:
  251. result="";
  252. }
  253. returnresult;
  254. }
  255. /**主方法**/
  256. publicstaticvoidmain(String[]args){
  257. FileexcelFile=newFile("src/user.xls");
  258. FilexmlFile=newFile("src/user.xml");
  259. newParseExcelUtil(excelFile,xmlFile);
  260. }
  261. publicStringgetCurEntityCode(){
  262. returncurEntityCode;
  263. }
  264. publicvoidsetCurEntityCode(StringcurEntityCode){
  265. this.curEntityCode=curEntityCode;
  266. }
  267. publicMapgetCurEntityHeadMap(){
  268. returncurEntityHeadMap;
  269. }
  270. publicvoidsetCurEntityHeadMap(MapcurEntityHeadMap){
  271. this.curEntityHeadMap=curEntityHeadMap;
  272. }
  273. publicParseXMLUtilgetParseXmlUtil(){
  274. returnparseXmlUtil;
  275. }
  276. publicvoidsetParseXmlUtil(ParseXMLUtilparseXmlUtil){
  277. this.parseXmlUtil=parseXmlUtil;
  278. }
  279. publicMapgetCurEntityColRequired(){
  280. returncurEntityColRequired;
  281. }
  282. publicvoidsetCurEntityColRequired(MapcurEntityColRequired){
  283. this.curEntityColRequired=curEntityColRequired;
  284. }
  285. publicListgetListDatas(){
  286. returnlistDatas;
  287. }
  288. publicvoidsetListDatas(ListlistDatas){
  289. this.listDatas=listDatas;
  290. }
  291. publicStringBuffergetErrorString(){
  292. returnerrorString;
  293. }
  294. publicvoidsetErrorString(StringBuffererrorString){
  295. this.errorString=errorString;
  296. }
  297. }

2.ParseXMLUtil.java

这个类是用来解析xml的,测试方法同样可以右键 run as 运行。可以把下面的一段注释放开,查看打印结果。

[java]view plaincopy
  1. packagecom.karen.test2;
  2. importjava.io.File;
  3. importjava.io.FileInputStream;
  4. importjava.io.FileNotFoundException;
  5. importjava.util.ArrayList;
  6. importjava.util.HashMap;
  7. importjava.util.Iterator;
  8. importjava.util.List;
  9. importjava.util.Map;
  10. importjava.util.Set;
  11. importorg.dom4j.Document;
  12. importorg.dom4j.Element;
  13. importorg.dom4j.io.SAXReader;
  14. /**
  15. *解析xml工具类
  16. *@authorPCCW-80352891
  17. *
  18. */
  19. @SuppressWarnings("rawtypes")
  20. publicclassParseXMLUtil{
  21. /**entitymap对象,key:name,value:entity的属性map集**/
  22. publicMapentityMap;
  23. /**columnmap对象,key:entityName_colName,value:column的属性map集**/
  24. publicMapcolumnMap;
  25. /**rulemap对象,key:entityName_colName_ruleName,value:rule的map集:找到一行rule**/
  26. publicMapruleMap;
  27. /**rulesmap对象,key:entityName_colName,value:rules的map集:找到该column下所有的rule**/
  28. publicMapcolumnRulesMap;
  29. /**entity--columnmap:key:entityName,value:columnlist:根据实体类名得到所有的列**/
  30. publicMapcolumnListMap;
  31. /**columnlist**/
  32. publicListcolumnList;
  33. /**开始解析xml文件**/
  34. publicParseXMLUtil(FilexmlFilePath){
  35. FileInputStreamin=null;
  36. try{
  37. if(xmlFilePath==null){
  38. thrownewFileNotFoundException();
  39. }
  40. SAXReaderreader=newSAXReader();
  41. in=newFileInputStream(xmlFilePath);
  42. Documentdoc=reader.read(in);
  43. Elementroot=doc.getRootElement();
  44. IteratoritEntity=root.elements("entity").iterator();
  45. while(itEntity.hasNext()){
  46. Elemententity=(Element)itEntity.next();
  47. parseEntity(entity);
  48. }
  49. /**测试entityMap是否正确**/
  50. MapenMap=(Map)this.getEntityMap().get("用户表");
  51. Set<?>set=enMap.keySet();
  52. Iteratorit=set.iterator();
  53. while(it.hasNext()){
  54. Stringuu=(String)it.next();
  55. System.out.println("entityproperties:"+uu+"="+enMap.get(uu));
  56. }
  57. /**//**测试columnlist是否正确**//*
  58. ListcolList=(List)this.getColumnListMap().get("用户表");
  59. System.out.println("columnsize:"+colList.size());
  60. *//**测试columnMap是否正确**//*
  61. MapcolMap=(Map)this.getColumnMap().get("用户表_员工号");
  62. Set<?>coListSet=colMap.keySet();
  63. IteratorcoListIt=coListSet.iterator();
  64. while(coListIt.hasNext()){
  65. StringcoListKey=(String)coListIt.next();
  66. System.out.println("columnproperties:"+coListKey+"="+colMap.get(coListKey));
  67. }
  68. *//**测试ruleMap是否正确**//*
  69. if(this.getColumnRulesMap()!=null){
  70. ListrulesValidList=(List)this.getColumnRulesMap().get("用户表_员工号");
  71. for(inti=0;i<rulesValidList.size();i++){
  72. MapcolRuleMap=(Map)rulesValidList.get(i);
  73. StringruleName=(String)colRuleMap.get("name");
  74. MapruleMa=(Map)this.getRuleMap().get("用户表_员工号_"+ruleName);//eg:用户表_用户名_nullable
  75. Stringmess=(String)ruleMa.get("message");
  76. System.out.println("ValidateRules"+i+":"+mess);
  77. }
  78. }*/
  79. }catch(Exceptione){
  80. e.printStackTrace();
  81. }
  82. }
  83. /**开始解析entity**/
  84. @SuppressWarnings("unchecked")
  85. publicvoidparseEntity(Elemententity){
  86. if(entity!=null){
  87. /**对数据进行初始化设置**/
  88. columnListMap=newHashMap();
  89. columnMap=newHashMap();
  90. entityMap=newHashMap();
  91. ruleMap=newHashMap();
  92. columnRulesMap=newHashMap();
  93. columnList=newArrayList();
  94. setEntityMap(entity);
  95. StringentityName=entity.attributeValue("name");
  96. IteratoritColumn=entity.elements("column").iterator();
  97. while(itColumn.hasNext()){
  98. Elementcolumn=(Element)itColumn.next();
  99. setColumnMap(entityName,column);
  100. }
  101. columnListMap.put(entityName,columnList);
  102. }
  103. }
  104. /**将entity放入entityMap中**/
  105. @SuppressWarnings("unchecked")
  106. publicvoidsetEntityMap(Elemententity){
  107. Mapent=newHashMap();
  108. Stringname=entity.attributeValue("name");
  109. Stringcode=entity.attributeValue("code");
  110. ent.put("name",name);
  111. ent.put("code",code);
  112. entityMap.put(name,ent);
  113. }
  114. /**将column放入columnMap中**/
  115. @SuppressWarnings("unchecked")
  116. publicvoidsetColumnMap(StringentityName,Elementcolumn){
  117. if(column!=null){
  118. Mapcol=newHashMap();
  119. Stringname=column.attributeValue("name");
  120. Stringcode=column.attributeValue("code");
  121. Stringtype=column.attributeValue("type");
  122. col.put("name",name);
  123. col.put("code",code);
  124. col.put("type",type);
  125. StringcolumnMapKey=entityName+"_"+name;//eg:用户表_用户名
  126. columnMap.put(columnMapKey,col);
  127. columnList.add(col);
  128. IteratorruleIt=column.elements("rules").iterator();//获得rules
  129. while(ruleIt.hasNext()){
  130. Elementrules=(Element)ruleIt.next();
  131. Iteratorrule=rules.elements("rule").iterator();//获得rule
  132. while(rule.hasNext()){
  133. ElementruleValid=(Element)rule.next();//获得每一行rule
  134. setRuleMap(entityName,name,ruleValid);
  135. }
  136. }
  137. }
  138. }
  139. /**将rule验证规则放入ruleMap中**/
  140. @SuppressWarnings("unchecked")
  141. publicvoidsetRuleMap(StringentityName,StringcolumnName,ElementruleValid){
  142. if(ruleValid!=null){
  143. StringruleName=ruleValid.attributeValue("name");
  144. StringruleMsg=ruleValid.attributeValue("message");
  145. MapruleValidMap=newHashMap();
  146. ruleValidMap.put("name",ruleName);
  147. ruleValidMap.put("message",ruleMsg);
  148. StringruleStrKey=entityName+"_"+columnName+"_"+ruleName;
  149. StringcolStrKey=entityName+"_"+columnName;
  150. if(this.getColumnRulesMap().containsKey(colStrKey)){
  151. Listvalids=(List)this.getColumnRulesMap().get(colStrKey);
  152. valids.add(ruleValidMap);
  153. }else{
  154. Listvalids=newArrayList();
  155. valids.add(ruleValidMap);
  156. this.columnRulesMap.put(colStrKey,valids);//将每个column下的所有rules存入该map中
  157. }
  158. ruleMap.put(ruleStrKey,ruleValidMap);//将每个column下的一条rule存入该map中
  159. }
  160. }
  161. /**主方法**/
  162. publicstaticvoidmain(String[]args){
  163. Filefile=newFile("src/user.xml");
  164. newParseXMLUtil(file);
  165. }
  166. /**所有的getset方法**/
  167. publicMapgetEntityMap(){
  168. returnentityMap;
  169. }
  170. publicvoidsetEntityMap(MapentityMap){
  171. this.entityMap=entityMap;
  172. }
  173. publicMapgetColumnMap(){
  174. returncolumnMap;
  175. }
  176. publicvoidsetColumnMap(MapcolumnMap){
  177. this.columnMap=columnMap;
  178. }
  179. publicMapgetRuleMap(){
  180. returnruleMap;
  181. }
  182. publicvoidsetRuleMap(MapruleMap){
  183. this.ruleMap=ruleMap;
  184. }
  185. publicMapgetColumnRulesMap(){
  186. returncolumnRulesMap;
  187. }
  188. publicvoidsetColumnRulesMap(MapcolumnRulesMap){
  189. this.columnRulesMap=columnRulesMap;
  190. }
  191. publicMapgetColumnListMap(){
  192. returncolumnListMap;
  193. }
  194. publicvoidsetColumnListMap(MapcolumnListMap){
  195. this.columnListMap=columnListMap;
  196. }
  197. }

3.既然做导入,当然需要连接数据库啦。只需要在mysql数据库中,建立一个 名为 chat 的数据库,然后导入下面的sql.来创建一张user表

[sql]view plaincopy
  1. CREATETABLE`user`(
  2. `status`varchar(20)defaultNULL,
  3. `port`int(10)NOTNULL,
  4. `ip`varchar(40)NOTNULL,
  5. `password`varchar(10)NOTNULL,
  6. `username`varchar(100)NOTNULL,
  7. `no`varchar(10)defaultNULL,
  8. `userImage`blob,
  9. PRIMARYKEY(`username`)
  10. )ENGINE=InnoDBDEFAULTCHARSET=utf8;

4.例子肯定需要很多jar包,比如poi啊,各种包。我就不在这里写出来了。

需要例子源码 请到这里下载:

http://download.csdn.net/detail/chenxuejiakaren/4439307

5.运行方法: 将例子导入到eclipse之中,然后可能会因为jdk版本不一样会有红色感叹号,没关系,改一下。单击项目右键--properties--java build path--libraries--找jdk啊。这个搞java的都会吧。

然后,单击右键 run as 运行ParseExcelUtil.java 就可以啦。


关于例子导入后会提示缺少包的问题:

我引入的相关jar包是在eclipse里面直接引入的,没有相对于的lib目录。主要是缺少了2个jar

poi-3.8-20120326.jar

mysql-connector-java-5.0.8-bin.jar

必须要在eclipse里引入他们。

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: