Java解析excel
2017-02-07 00:00
267 查看
/**
* 解析excel
* @param file
* @return
* @throws Exception
*
* @author : weihui
* @createTime : 2016年1月17日 下午3:48:50
*/
public static List<List<String>> parseXls(File file) throws Exception {
InputStream is = new FileInputStream(file);
//读取表格
Workbook wb = WorkbookFactory.create(is);
Sheet sheet = wb.getSheetAt(0);
List<List<String>> rows = new ArrayList<List<String>>();
for(Row r : sheet){
if(r.getRowNum() == 0) continue; //meta数据忽略
List<String> columns = new ArrayList<String>();
for(Cell c : r){
columns.add(getCellValue(c));
/*if(c.getColumnIndex() == 0){
dn.setIdCode(getCellValue(c));
}else if(c.getColumnIndex() == 1){
dn.setCaseName(getCellValue(c));
}*/
}
rows.add(columns);
}
return rows;
}
/**
* 得到每个单元格的值
* @param c
* @return
*/
private static String getCellValue(Cell c) {
String value = "";
switch (c.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = c.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(c)) {
value = "" + c.getDateCellValue();
} else {
value = "" + (int) c.getNumericCellValue();
}
break;
case Cell.CELL_TYPE_BOOLEAN:
value = "" + c.getBooleanCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
value = "" + c.getCellFormula();
break;
}
return value;
}
* 解析excel
* @param file
* @return
* @throws Exception
*
* @author : weihui
* @createTime : 2016年1月17日 下午3:48:50
*/
public static List<List<String>> parseXls(File file) throws Exception {
InputStream is = new FileInputStream(file);
//读取表格
Workbook wb = WorkbookFactory.create(is);
Sheet sheet = wb.getSheetAt(0);
List<List<String>> rows = new ArrayList<List<String>>();
for(Row r : sheet){
if(r.getRowNum() == 0) continue; //meta数据忽略
List<String> columns = new ArrayList<String>();
for(Cell c : r){
columns.add(getCellValue(c));
/*if(c.getColumnIndex() == 0){
dn.setIdCode(getCellValue(c));
}else if(c.getColumnIndex() == 1){
dn.setCaseName(getCellValue(c));
}*/
}
rows.add(columns);
}
return rows;
}
/**
* 得到每个单元格的值
* @param c
* @return
*/
private static String getCellValue(Cell c) {
String value = "";
switch (c.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = c.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(c)) {
value = "" + c.getDateCellValue();
} else {
value = "" + (int) c.getNumericCellValue();
}
break;
case Cell.CELL_TYPE_BOOLEAN:
value = "" + c.getBooleanCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
value = "" + c.getCellFormula();
break;
}
return value;
}
相关文章推荐
- java解析Excel存储-对单元格内容判断
- 如何用Java 实现 Excel 表达式的解析(摘自:http://topic.csdn.net/t/20030408/17/1634982.html#)
- Java解析Excel文件
- Java数据导入(读)Excel文件 解析
- java解析Excel
- java jxl 解析excel 中文乱码 setEncoding
- java解析excel代码编写过程
- Java+Flex+POI 解析Excel文件(读取文件中想要的内容)
- java poi解析Excel
- Java数据导入(读)Excel文件 解析
- 利用java JDBC解析excel文档
- JAVA JXL -----excel文件上传和解析
- java解析excel数据(转)
- java 解析Excel 与常用的xml解析方法
- java 解析 excel数据excel
- java解析excel方法
- java poi 创建XLS 用Jakarta POI操作Excel文件 POI创建 解析Excel
- JAVA解析文件pdf、word、excel
- java通过poi解析Excel文件,直接读取客户端文件
- Java解析 Excel