-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df469a8
commit ec01375
Showing
97 changed files
with
1,427 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.nuc.device.base.poi; | ||
|
||
import com.nuc.device.base.poi.bean.ExcelColumnBean; | ||
import com.nuc.device.base.poi.util.PoiCommonUtils; | ||
import org.apache.poi.hssf.usermodel.HSSFRow; | ||
import org.apache.poi.hssf.usermodel.HSSFSheet; | ||
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | ||
import org.apache.poi.poifs.filesystem.POIFSFileSystem; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by IDEA | ||
* User:Leopold | ||
* Email:[email protected] | ||
* Date:2016/3/6 | ||
* Time:5:46 | ||
*/ | ||
public class ImportExcel<T> { | ||
private HSSFWorkbook workbook; | ||
private List<ExcelColumnBean> list; | ||
private Class beanClass; | ||
public ImportExcel(){} | ||
public ImportExcel(InputStream inputStream,Class beanClass){ | ||
try { | ||
this.workbook=new HSSFWorkbook(new POIFSFileSystem(inputStream)); | ||
this.list= PoiCommonUtils.getColumnBeanList(beanClass); | ||
this.beanClass=beanClass; | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
public String checkExcel(){ | ||
String result="success"; | ||
if(workbook==null){ | ||
return "上传文件为空!"; | ||
} | ||
HSSFSheet sheet=workbook.getSheetAt(0); | ||
HSSFRow row=sheet.getRow(0); | ||
for(int i=0;i<list.size();i++){ | ||
if(!row.getCell(i).getStringCellValue().equals(list.get(i).getColumnName())){ | ||
return "sheet中列名与要求不匹配!"; | ||
} | ||
} | ||
return result; | ||
} | ||
public List<T> excelToList(){ | ||
List reList=new ArrayList(); | ||
try { | ||
HSSFSheet sheet=workbook.getSheetAt(0); | ||
HSSFRow row; | ||
int count=1; | ||
boolean flag=sheet.getRow(count)==null?false:true; | ||
while (flag){ | ||
Object obj=beanClass.newInstance(); | ||
row=sheet.getRow(count); | ||
if(row!=null){ | ||
for(int i=0;i<list.size();i++){ | ||
ExcelColumnBean columnBean=list.get(i); | ||
Object value=PoiCommonUtils.parseType(columnBean.getColumnField().getType(), row.getCell(i).getStringCellValue()); | ||
columnBean.getSetterMethod().invoke(obj, value); | ||
} | ||
reList.add(obj); | ||
count++; | ||
}else { | ||
flag=false; | ||
} | ||
} | ||
} catch (InstantiationException e) { | ||
e.printStackTrace(); | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} catch (InvocationTargetException e) { | ||
e.printStackTrace(); | ||
} | ||
return reList; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.