我们在用阿里巴巴的Excel组件解析excel的时候, 会出现表头和内容列数不一致的情况, 采用以下方式就可以了 public class EasyExcelDemo {
private static String getCellValue(final Object value){
if(value==null){
return "";
}
return value.toString();
};
public static void main(String[] args) {
ExcelListener excelListener = new ExcelListener();
InputStream inputStream = null;
try {
inputStream = new FileInputStream(new File("D:\\更新mingjian.xlsx"));
EasyExcelFactory.readBySax(inputStream, new Sheet(1, 0), excelListener);
inputStream.close();
List<List<String>> list = excelListener.list;
//获取表头的size
int size = list.get(0).size();
//移除表头
list.remove(0);
//声明一个数组, 是excel列, 要保持列数
Object[] cells = new Object[size];
Object cell = cells[5];
//解析内容
for (int i = 0; i < list.size(); i++) {
List<String> strings = list.get(i);
if(strings.size()!=size){
System.arraycopy(strings.toArray(),0,cells,0, strings.toArray().length);
}else{
cells = strings.toArray();
}
String dAttr = getCellValue(cells[0]);
String problem_code = getCellValue(cells[1]);
String key_problem = getCellValue(cells[2]);
String standard = getCellValue(cells[3]);
String check_time = getCellValue(cells[4]);
String dCode = getCellValue(cells[5]);
String is_score = getCellValue(cells[6]);
String loss_score_reason = getCellValue(cells[7]);
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}