服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|

服务器之家 - 编程语言 - JAVA教程 - java读取excel文件的两种方法

java读取excel文件的两种方法

2020-12-23 13:29仰望天空 JAVA教程

这篇文章主要为大家详细介绍了java读取excel文件的两种方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

方式一:

借用 

java读取excel文件的两种方法

java" id="highlighter_958911">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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();
 }
}
}

java读取excel文件的两种方法

 方式二:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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();
  }
}
}

java读取excel文件的两种方法

附jar包

java读取excel文件的两种方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。 

原文链接:http://www.cnblogs.com/tk55/archive/2017/08/30/7453024.html

延伸 · 阅读

精彩推荐