设为首页 加入收藏

TOP

Java中操作Excel表格
2014-11-16 18:20:34 来源: 作者: 【 】 浏览:20
Tags:Java 操作 Excel 表格

  利用Java Excel API ,下载地址:jexcelapi.rar


  下面给出一段读取数据的例子代码:


  /*


  * To change this template, choose Tools | Templates


  * and open the template in the editor.


  */


  package excel;


  import java.io.FileInputStream;


  import java.io.FileNotFoundException;


  import java.io.IOException;


  import java.io.InputStream;


  import java.util.logging.Level;


  import java.util.logging.Logger;


  import jxl.Cell;


  import jxl.Sheet;


  import jxl.Workbook;


  import jxl.read.biff.BiffException;


  /**


  *


  * @author Wei.Liu


  */


  public class Main {


  /**


  * @param args the command line arguments


  */


  public static void main(String[] args) {


  try {


  InputStream is = new FileInputStream("d:\\test.xls");


  jxl.Workbook rwb = Workbook.getWorkbook(is);


  Sheet rs = rwb.getSheet(0);


  //取第一行,第一列的元素


  Cell c00 = rs.getCell(0, 0);


  String strc00 = c00.getContents();


  //取第一行,第二列的元素


  Cell c10 = rs.getCell(1,0);


  String strc10= c10.getContents();


  System.out.println(strc00+" "+c00.getType().toString());


  System.out.println(strc10+" "+c10.getType().toString());


  //获取sheet的个数


  System.out.println(rwb.getNumberOfSheets());


  Sheet [] sheets =rwb.getSheets();


  for(int i=0;i


  System.out.println(rwb.getSheet(i).getName());


  }


  //获得列数


  System.out.println(rwb.getSheet(0).getColumns());


  //获得每列的元素


  Cell [] cell = rwb.getSheet(0).getColumn(0);


  //获得总行数


  System.out.println(rwb.getSheet(0).getRows());


  //获得行元素


  Cell [] cell2 = rwb.getSheet(0).getRow(0);


  //关闭对象


  rwb.close();


  } catch (Exception ex) {


  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);


  }


  }


  }


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java中ArrayList或Collection的co.. 下一篇关于JAVA中回车符的认识

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: