Java 读取Excel文件

来源:互联网 发布:超级基因优化液txt下载全文下载 编辑:程序博客网 时间:2024/06/03 16:15

转自:http://blog.csdn.net/tkd03072010/article/details/6692366


需要用到的jar包: jxl-2.6.12.jar

如果使用的是Maven工程,pom配置如下:

<dependency><groupId>net.sourceforge.jexcelapi</groupId><artifactId>jxl</artifactId><version>2.6.12</version></dependency>

代码如下:

package ywzn.gl.ExeclToMongoDB;import java.io.File;  import java.io.IOException;    import jxl.Cell;  import jxl.Sheet;  import jxl.Workbook;  import jxl.read.biff.BiffException;    public class JavaReadExcel {      public static void main(String[] args) {              try {              String fileName = "/home/hadoop/javatoexcel.xls"; // Excel文件所在路径              File file = new File(fileName); // 创建文件对象              Workbook wb = Workbook.getWorkbook(file); // 从文件流中获取Excel工作区对象(WorkBook)              Sheet sheet = wb.getSheet(0); // 从工作区中取得页(Sheet)                            for (int i = 0; i < sheet.getRows(); i++) { // 循环打印Excel表中的内容                  for (int j = 0; j < sheet.getColumns(); j++) {                      Cell cell = sheet.getCell(j, i);                      System.out.println(cell.getContents());                  }                  System.out.println();              }          } catch (BiffException e) {              e.printStackTrace();          } catch (IOException e) {              e.printStackTrace();          }                }  }  


0 0