Java读取xls文件

2014-11-24 08:36:56 · 作者: · 浏览: 0
1. Java读取xls文件可以利用jsl.jar包来进行解析,详细参考:http://xuexin0714.iteye.com/blog/457966。不过这个对于后缀名为xlsx的文件会出错。
在CODE上查看代码片派生到我的代码片
public static List parse(File file) {    
    List excelValueList = new ArrayList();    
    if (file.exists() && file.canRead()    
            && (file.getName().lastIndexOf(".xls") >= 1)) {    
        Workbook workbook = null;    
        try {    
            workbook = Workbook.getWorkbook(file);    
            Sheet sheet = workbook.getSheet(0);    
            int row = sheet.getRows();    
            int col = sheet.getColumns();    
            for (int r = 0; r < row; r++) {    
                String[] rowValue = new String[col];    
                for (int c = 0; c < col; c++) {    
                    rowValue[c] = sheet.getCell(c, r).getContents() != null   sheet    
                            .getCell(c, r).getContents()    
                            : "";    
                }    
                excelValueList.add(rowValue);    
            }    
        } catch (BiffException e) {    
            // TODO Auto-generated catch block    
            e.printStackTrace();    
        } catch (IOException e) {    
            // TODO Auto-generated catch block    
            e.printStackTrace();    
        } finally {    
            if (workbook != null) {    
                workbook.close();    
            }    
        }    
    }    
    return excelValueList;    
}    

2. Java读取xls文件可以当做是访问 数据库,利用jdbc driver (例如:http://blog.csdn.net/54powerman/article/details/5787402)
出现错误:[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
尝试Create a System DSN:http://docs.oracle.com/cd/E18283_01/owb.112/e10582/loading_ms_data.htm#BGBFEGIB
可以用的driver貌似有很多,比如:
sqlsheet: https://code.google.com/p/sqlsheet/
3. 使用jxls或jexcelapi等api来操作,这样可以访问类型更丰富的、布局更复杂的excel。
jxls:
http://jxls.sourceforge.net
jexcelapi:
http://jexcelapi.sourceforge.net/