读取excel为 javabean

web_backend_develope
chenlw 9 years ago
parent 30892360c7
commit 978423610b

@ -3,6 +3,7 @@ package com.platform.utils.excelUtils;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -25,26 +26,23 @@ import com.base.Constant;
*/ */
public class ExcelOperation { public class ExcelOperation {
/** excel /** excel
* @param templateFilePath * @param templateFilePath
* @param list list * @param list list
* @param excelInitRow //从第excelInitRow行 开始填充数据,( Constant.EXCEL_TEMPLATE_INIT_ROW)
* @return HSSFWorkbook * @return HSSFWorkbook
* @throws FileNotFoundException * @throws FileNotFoundException
* @throws IOException * @throws IOException
* @throws IllegalAccessException
* @throws IllegalArgumentException * @throws IllegalArgumentException
*/ * @throws IllegalAccessException
public static XSSFWorkbook writeExcelTemplate(String templateFilePath, List list) throws FileNotFoundException, IOException, IllegalArgumentException, IllegalAccessException { *///从第几行 填充数据
public static XSSFWorkbook writeExcelTemplate(String templateFilePath,int excelInitRow, List list) throws FileNotFoundException, IOException, IllegalArgumentException, IllegalAccessException {
// InputStreamReader in= new InputStreamReader(Resource.class.getResourceAsStream(templateFilePath), "UTF-8"); // InputStreamReader in= new InputStreamReader(Resource.class.getResourceAsStream(templateFilePath), "UTF-8");
ClassPathResource res = new ClassPathResource(templateFilePath); ClassPathResource res = new ClassPathResource(templateFilePath);
//此处 耗时太久原因excel 2007 以后采用的方式不同造成的建议升级 到 poi 3.15以上) //此处 耗时太久原因excel 2007 以后采用的方式不同造成的建议升级 到 poi 3.15以上)
Date date = new Date();
XSSFWorkbook workbook = new XSSFWorkbook(res.getInputStream()); XSSFWorkbook workbook = new XSSFWorkbook(res.getInputStream());
Date date2 = new Date(); // System.out.println(" new XSSFWorkbook "+ (date2.getTime()-date.getTime()));
System.out.println(" new XSSFWorkbook "+ (date2.getTime()-date.getTime()));
//从第几行 填充数据
int excelInitRow = Constant.EXCEL_TEMPLATE_INIT_ROW;
//读取工作表 //读取工作表
XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1"); XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1");
XSSFRow row; XSSFRow row;
@ -61,9 +59,15 @@ public class ExcelOperation {
fields[j].setAccessible(true); fields[j].setAccessible(true);
cell = row.createCell((short) j,HSSFCellStyle.ALIGN_CENTER); cell = row.createCell((short) j,HSSFCellStyle.ALIGN_CENTER);
cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellType(HSSFCell.CELL_TYPE_STRING);
Object value = fields[j].get(obj); Object value = fields[j].get(obj);
if (null != value) { if (null != value) {
cell.setCellValue(value.toString()); //int型
if (fields[j].getType().toString().contains("Integer")) {
cell.setCellValue(String.valueOf(value));
}
else {
cell.setCellValue(value.toString());
}
} }
else { else {
cell.setCellValue(""); cell.setCellValue("");
@ -85,6 +89,10 @@ public class ExcelOperation {
return workbook; return workbook;
} }
/** excelcell
* @param workbook
* @return
*/
private static XSSFCellStyle getStyle(XSSFWorkbook workbook) { private static XSSFCellStyle getStyle(XSSFWorkbook workbook) {
// 设置字体; // 设置字体;
XSSFFont font = workbook.createFont(); XSSFFont font = workbook.createFont();
@ -123,4 +131,63 @@ public class ExcelOperation {
return style; return style;
} }
/** excel
* @param workbook excelXSSFWorkbook
* @param excelInitRow
* @param classz excel
* @return
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static List readExcelTemplate(XSSFWorkbook workbook, int excelInitRow, Class<?> classz) throws InstantiationException, IllegalAccessException {
List list = null;
//读取工作表
XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1");
XSSFRow row;
XSSFCell cell = null;
//excel总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
//excel总列数
int colNum = row.getPhysicalNumberOfCells();
// 填充数据的类的 属性个数
int objColl = classz.getDeclaredFields().length;
//如果有数据
if (rowNum > excelInitRow) {
list = new ArrayList<>();
//遍历每行数据
for (int i = excelInitRow; i < rowNum; i++) {
//获得当前行
row = sheet.getRow(i);
//实例化
Object newObj = classz.newInstance();
Field[] fields = newObj.getClass().getDeclaredFields();
boolean isAddObj = false;
//遍历每列的数据
for (int j = 0; j < objColl; j++) {
cell = row.getCell(j);
if (null != cell) {
fields[j].setAccessible(true);
String cellString = cell.getStringCellValue();
if (!"".equals(cellString)) {
isAddObj = true;
//int型
if (fields[j].getType().toString().contains("Integer")) {
fields[j].set(newObj, Integer.valueOf(cellString));
}
else {
fields[j].set(newObj, cellString);
}
}
}
}
if (isAddObj) {
list.add(newObj);
}
}
}
return list;
}
} }

Loading…
Cancel
Save