您的位置:首页 > 编程语言 > Java开发

Java读取Excel文件

2017-03-28 14:04 155 查看
/**
 * 读取的EXCEL的格式为
 * 姓名     年级        性别
 * 张三     研一        女
 * 李四
 * @param filepath
 * @return List<List<Map<String, String>>>  第一个list存sheet,第二个list存列。按照map取关键字
 * 如:map=姓名 ,输出为张三、李四
 * @throws Exception
 */

public List<List<Map<String, String>>> readExcelWithTitle(String filepath) throws Exception
{
String fileType = filepath.substring(filepath.lastIndexOf(".") + 1, filepath.length());
InputStream is = null;
Workbook wb = null;
try {
is = new FileInputStream(filepath);

    if (fileType.equals("xls")) {
     wb = new HSSFWorkbook(is);
    } else if (fileType.equals("xlsx")) {
     wb = new XSSFWorkbook(is);
    } else {
     throw new Exception("读取的不是excel文件");
    }
    
    List<List<Map<String, String>>> result = new ArrayList<List<Map<String,String>>>();//对应excel文件
    
    int sheetSize = wb.getNumberOfSheets();
    for (int i = 0; i < sheetSize; i++) {//遍历sheet页
     Sheet sheet = wb.getSheetAt(i);
     List<Map<String, String>> sheetList = new ArrayList<Map<String, String>>();//对应sheet页
    
     List<String> titles = new ArrayList<String>();//放置所有的标题
    
     int rowSize = sheet.getLastRowNum() + 1;
     for (int j = 0; j < rowSize; j++) {//遍历行
     Row row = sheet.getRow(j);
     if (row == null) {//略过空行
continue;
}
     int cellSize = row.getLastCellNum();//行中有多少个单元格,也就是有多少列
     if (j == 0) {//第一行是标题行
     for (int k = 0; k < cellSize; k++) {
     Cell cell = row.getCell(k);
     titles.add(cell.toString());
     }
     } else {//其他行是数据行
     Map<String, String> rowMap = new HashMap<String, String>();//对应一个数据行
     for (int k = 0; k < titles.size(); k++) {
     Cell cell = row.getCell(k);
     String key = titles.get(k);
     String value = null;
     if (cell != null) {
value = cell.toString();
}
     rowMap.put(key, value);
     }
     sheetList.add(rowMap);
     }
     }
     result.add(sheetList);
    }
    
    return result;
} catch (FileNotFoundException e) {
throw e;
} finally {
if (wb != null) {
wb.close();
}
if (is != null) {
is.close();
}
}
}

        /**
 * 读取excel文件,将excel文件转换为list。限定一个excel文件只能拥有一个sheet。
 * 
 * @param filePath
 *            完全限定文件名
 * @return List<List<String>>
 */

public static List<List<String>> read(String filePath) {
List<List<String>> content = null;
InputStream is = null;
Workbook wb = null;
try {
// 判断文件是否为excel格式的文件
is = new FileInputStream(filePath);
if (filePath.endsWith(".xls")) {
wb = new HSSFWorkbook(is);
} else if (filePath.endsWith(".xlsx")) {
wb = new XSSFWorkbook(is);
} else {
throw new Exception("读取的不是excel文件");
}

// 遍历excel,将结果存储在content中
content = new ArrayList<List<String>>();
Sheet sheet = wb.getSheetAt(0);
List<String> rowList = null; 
for (int i = 0; i < sheet.getLastRowNum() + 1; i++) { //遍历行
Row row = sheet.getRow(i);
rowList = new ArrayList<String>();
if (row != null) {
for (int j = 0; j < row.getLastCellNum(); j++) {
if (row.getCell(j) == null) { //第i行j列为空
rowList.add("");  
} else {
try{
rowList.add(row.getCell(j).getStringCellValue());
}catch(Exception e){
continue;
}
}
}
}
content.add(rowList);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (wb != null) {
wb.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

return content;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: