展开全部
//用jxl读取读取完是[{A=1,B=0},{A=2,B=0},{A=3,B=0}] 这种格式e69da5e887aa3231313335323631343130323136353331333361313936
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class ReadExcel {
public static void main(String[] args) {
File excel = new File("F:/data.xls");
List> list =transList(excel);
System.out.println(list);
}
public static List> transList(File file) {
int errrow = 0;
List> list = new ArrayList>();//数据集合
Workbook book = null;
try {
book = Workbook.getWorkbook(file);
Sheet sheet = book.getSheet(0);
int rows = getRightRows(sheet);//非空行数
int columns = sheet.getColumns();
if(rows <= 1){
return null;
}
for(int i = 0;i
errrow = i;
Map map = new HashMap();
for(int j = 0;j
Cell cell = sheet.getCell(j, i);
String text = cell.getContents();
switch(j){
case 0: map.put("A", Integer.valueOf(text));break;
case 1: map.put("B", Integer.valueOf(text));break;
}
}
list.add(map);
}
return list;
} catch (Exception e) {
e.printStackTrace();
System.out.println("rows : " + errrow);
return null; //数据文件格式错误
}finally{
if(book != null){
book.close();
}
}
}
/**
* 去掉下端空行
* @param sheet
* @return
* @author chenyy
*/
private static int getRightRows(Sheet sheet) {
int rsCols = sheet.getColumns(); //列数
int rsRows = sheet.getRows(); //行数
int nullCellNum;
int afterRows = rsRows;
for (int i = 1; i
nullCellNum = 0;
for (int j = 0; j
String val = sheet.getCell(j, i).getContents();
val = StringUtils.trimToEmpty(val);
if (StringUtils.isBlank(val)){
nullCellNum++;
}
}
if (nullCellNum >= rsCols) {
afterRows--;
}
}
return afterRows;
}
}