当前位置: 移动技术网 > IT编程>开发语言>Java > java读取excel文件的两种方法

java读取excel文件的两种方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下

方式一:

借用 

package com.ij34.util;
/** 
* @author admin
* @date 创建时间:2017年8月29日 下午2:07:59 
* @version 1.0
*@type_name myclass
*/
import java.io.file; 
import java.io.ioexception; 
import jxl.cell; 
import jxl.sheet; 
import jxl.workbook; 
import jxl.read.biff.biffexception; 
 
public class test05 { 
public static void main(string args[]){ 
 file f=new file("table01.xls"); 
 try { 
  workbook book=workbook.getworkbook(f);// 
  sheet sheet=book.getsheet(0); //获得第一个工作表对象 
  for(int i=0;i<sheet.getrows();i++){ 
   for(int j=0;j<sheet.getcolumns();j++){ 
    cell cell=sheet.getcell(j, i); //获得单元格 
    system.out.print(cell.getcontents()+" "); 
   } 
   system.out.print("\n"); 
  } 
 } catch (biffexception e) { 
  // todo auto-generated catch block 
  e.printstacktrace(); 
 } catch (ioexception e) { 
  // todo auto-generated catch block 
  e.printstacktrace(); 
 } 
} 
} 

 方式二:

package com.ij34.util;

import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;

import org.apache.poi.hssf.usermodel.hssfsheet;
import org.apache.poi.hssf.usermodel.hssfworkbook;
import org.apache.poi.ss.usermodel.cell;
import org.apache.poi.ss.usermodel.dateutil;
import org.apache.poi.ss.usermodel.row;

/** 
* @author admin
* @date 创建时间:2017年8月29日 下午4:01:06 
* @version 1.0
*@type_name test02
*读取xls
*/
public class test02 {
 public static void main(string[] args) throws filenotfoundexception, ioexception {
  file excelfile = new file("table01.xls");
  hssfworkbook wb = new hssfworkbook(new fileinputstream(excelfile));
  hssfsheet sheet = wb.getsheetat(0);
  
  for (row row : sheet) {
   for (cell cell : row) {
    switch (cell.getcelltype()) {
    case cell.cell_type_string://字符串
     system.out.print(cell.getrichstringcellvalue().getstring());
     system.out.print(" ");
     break;
    case cell.cell_type_numeric://数值与日期
     if (dateutil.iscelldateformatted(cell)) {
      system.out.print(string.valueof(cell.getdatecellvalue()));
     } else {
      system.out.print(cell.getnumericcellvalue());
     }
     system.out.print(" ");
     break;
    case cell.cell_type_boolean://boolean类型
     system.out.print(cell.getbooleancellvalue());
     system.out.print(" ");
     break;
    default:
    }
   }
   system.out.println();
  }
}
}

附jar包

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网