1.读取EXECL文件类
[java]
package hzdracom.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.ss.usermodel.WorkbookFactory;
/*
* 说明:本demo读取本地EXECL的前两列内容
* 需要下面两个架包
*1.poi-3.6-20091214.jar *2.poi-ooxml-3.6-20091214.jar *
*
* @author ZhuangZi
* @version $Id: EXCEL.java,v 0.1 2013-4-3 下午03:30:39 ZhuangZi Exp $
*/
@SuppressWarnings( { "unchecked", })
public class EXCEL {
/***
*
* @author ZhuangZi
* @class hzdracom.test.EXCEL
* @method readExcelList
* @Directions 读execl文件的前两列
* @date 2013-4-2下午05:15:25
* @param file
* @param args
* @return
* @throws IOException List
*/
public static List readExcelList(File filepath ) throws IOException{
List list = new ArrayList();
Workbook rwb = null;
InputStream stream = new FileInputStream(filepath);
try {
rwb = WorkbookFactory.create(stream);
} catch (InvalidFormatException e) {
e.printStackTrace();
}
Sheet sheet=null;
if (rwb!=null&&rwb.getSheetAt(0)!=null) {
sheet = rwb.getSheetAt(0);
}
//行数(表头的目录不需要,从1开始)
int rowCount = sheet.getLastRowNum();
for(int i=0; i<=rowCount; i++){
Row row = sheet.getRow(i);
if(row!=null){
Cell cell = row.getCell(0);
Cell cell2= row.getCell(1);
String c = cell.toString();
String c2 = cell2.toString();
if(isNotEmpty(c)&&isNotEmpty(c2)){
//读取单元格的数据
DecimalFormat df = new DecimalFormat("##");
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
list.add(String.valueOf(cell.getRichStringCellValue()));
break;
case Cell.CELL_TYPE_NUMERIC:
list.add(df.format(cell.getNumericCellValue()));
break;
default:
break;
}
switch (cell2.getCellType()) {
case Cell.CELL_TYPE_STRING:
list.add(String.valueOf(cell2.getRichStringCellValue()));
break;
case Cell.CELL_TYPE_NUMERIC:
list