Java读取Excel文件

来源:互联网 发布:mac 中文输入法 编辑:程序博客网 时间:2024/05/17 01:09

首先下载jxl.jar包,下载地址:http://download.csdn.net/detail/prstaxy/4469935

然后在工程文件上右键选Built Path—Configure Built Path切换到Libraries导入jxl.jar包。
读取Excel方法示例:
写入Excel见文章:http://blog.csdn.net/prstaxy/article/details/7815834

 public static void main(String[] args) {Workbook book=null;try { InputStream is = new FileInputStream("E:\\GetInfo\\测试.xls"); book = Workbook.getWorkbook(is); //Sheet(术语:工作表)就是Excel表格左下角的Sheet1,Sheet2,Sheet3但在程序中 //Sheet的下标是从0开始的 //获取第一张Sheet表 Sheet sheet = book.getSheet(0); Cell cell = null;//就是单个单元格    //开始循环,取得 cell 里的内容,这里都是按String来取,为了省事,具体可按实际类型来取。或者都按String来取,然后根据你需要强制转换一下。for (int i = 0; i < sheet.getRows(); i++) {StringBuffer sb = new StringBuffer();for (int j = 0; j < sheet.getColumns(); j++) {cell = sheet.getCell(j,i);sb.append(cell.getContents());sb.append(" ");//将单元格的每行内容用逗号隔开}System.out.println(sb);//将每行的字符串用一个String类型的集合保存。}}catch(Exception e) { System.out.println(e); }finally{ book.close(); }}


原创粉丝点击