接上一篇寫了使用poi導出excel,今天把讀取excel的方法補上,核心類以下:java
package cn.qazit.common.utils;web
import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Iterator; import java.util.List;數據庫
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; 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.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.internal.runners.statements.ExpectException;apache
import cn.qazit.app.core.charge.model.ZywsptGfxwjb; import cn.qazit.common.bean.ExportFiledsBean;app
/**xss
導出的公共類.net
@ClassName:ExportUtil.javaexcel
@Title:ExportUtil.javacode
@Description:orm
@CopyRight:CopyRight(c)2016
@Company:www.qazit.cn
@author
@date 2016年10月11日 下午7:14:05
@version:v1.0 */ public class ExportUtil {
/**
導出的通用方法
@param sheet1
@param title
@param clazz
@param list
@return
@throws IOException
@throws ClassNotFoundException
@throws SecurityException
@throws NoSuchMethodException
@throws IllegalArgumentException
@throws Exception */ public static byte[] export(String sheet1, String title, Class<?> clazz,List<?> list) throws IOException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, Exception{ ByteArrayOutputStream out = new ByteArrayOutputStream(); // 第一步,建立一個webbook,對應一個Excel文件
HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一個sheet,對應Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheet1); // 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制short
HSSFRow row = sheet.createRow((int) 0); // 第四步,建立單元格,並設置值表頭 設置表頭居中
HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 建立一個居中格式
//設置表頭
List<ExportFiledsBean> excelHead = getExcelHead(clazz); Class _class = createClazz(clazz);
HSSFCell cell = null; // excel頭
for (int i = 0; i < excelHead.size(); i++) {
ExportFiledsBean exportFiledsBean = excelHead.get(i); cell = row.createCell(i);
cell.setCellValue(exportFiledsBean.getName());
cell.setCellStyle(style); }
for (int i = 0;i<list.size();i++) { row = sheet.createRow((int) i + 1); Object obj = (Object) list.get(i); for(int j=0;j<excelHead.size();j++) { ExportFiledsBean exportFiledsBean = excelHead.get(j); String getFuncName = exportFiledsBean.getGetFuncName();
Method addMethod = _class.getMethod(getFuncName); Object result = addMethod.invoke(obj); if(result==null) { row.createCell((int) j).setCellValue(""); }else{ row.createCell((int) j).setCellValue(result.toString()); } }
} wb.write(out); // 第五步,寫入實體數據 實際應用中這些數據從數據庫獲得 return out.toByteArray(); }
private static List<ExportFiledsBean> getExcelHead(Class<?> clazz){ return AunotationUtil.getExportFileds(clazz); }
private void insertCell(HSSFRow row,int i,Object object){
if(object==null){
row.createCell(i).setCellValue("");
}else{
row.createCell(i).setCellValue(object.toString());
}
}
/**
讀取excel
@param fileName
@param clazz
@return
@throws ClassNotFoundException
@throws SecurityException
@throws NoSuchMethodException
@throws IllegalArgumentException
@throws Exception
@throws InvocationTargetException */ public static List<?> readExport(String fileName,Class<?> clazz) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, Exception, InvocationTargetException{ List<Object> list = new ArrayList<Object>(); List<ExportFiledsBean> excelHead = getExcelHead(clazz);
Class _class = createClazz(clazz);
boolean isE2007 = false; //判斷是不是excel2007格式
if(fileName.endsWith("xlsx"))
isE2007 = true;
try {
InputStream input = new FileInputStream(fileName); //創建輸入流 Workbook wb = creatWorkbook(isE2007,input);
//根據文件格式(2003或者2007)來初始化
Sheet sheet = wb.getSheetAt(0); //得到第一個表單
Iterator<Row> rows = sheet.rowIterator(); //得到第一個表單的迭代器
while (rows.hasNext()) {
Row row = rows.next(); //得到行數據
int rowNum = row.getRowNum(); if(rowNum>0) { Object obj = _class.newInstance(); Iterator<Cell> cells = row.cellIterator(); //得到第一行的迭代器
while (cells.hasNext()) {
Cell cell = cells.next();
int cellColumnIndex = cell.getColumnIndex(); for(int i=0;i<excelHead.size();i++) { ExportFiledsBean exportFiledsBean = excelHead.get(i); int order = exportFiledsBean.getOrder(); if(order==cellColumnIndex) { String fitFuncName = exportFiledsBean.getFitFuncName(); obj = addObjctList(cell,fitFuncName,obj,_class); } } } list.add(obj); } }
} catch (IOException ex) {
ex.printStackTrace();
}
return list; }
private static Workbook creatWorkbook(boolean isE2007,InputStream input) throws IOException { Workbook wb = null; if(isE2007)
wb = new XSSFWorkbook(input);
else
wb = new HSSFWorkbook(input);
return wb;
} /**
/**
}