java解析Excel的方法(xls、xlsx两种格式)

时间:2021-12-16 03:30:52

一、需要导入的jar

  1.commons-collections4-4.1.jar

  2.poi-3.17-beta1.jar

  3.poi-ooxml-3.17-beta1.jar

  4.poi-ooxml-schemas-3.17-beta1.jar

  5.xmlbeans-2.6.0.jar

二、主要api

  1.import org.apache.poi.ss.usermodel.workbook,对应excel文档;

  2.import org.apache.poi.hssf.usermodel.hssfworkbook,对应xls格式的excel文档;

  3.import org.apache.poi.xssf.usermodel.xssfworkbook,对应xlsx格式的excel文档;

  4.import org.apache.poi.ss.usermodel.sheet,对应excel文档中的一个sheet;

  5.import org.apache.poi.ss.usermodel.row,对应一个sheet中的一行;

  6.import org.apache.poi.ss.usermodel.cell,对应一个单元格。

三、代码如下

java" id="highlighter_414647">
?
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package poi;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;
import java.io.inputstream;
import java.util.arraylist;
import java.util.linkedhashmap;
import java.util.list;
import java.util.map;
import java.util.map.entry;
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;
import org.apache.poi.ss.usermodel.sheet;
import org.apache.poi.ss.usermodel.workbook;
import org.apache.poi.xssf.usermodel.xssfworkbook;
public class testpoi {
  public static void main(string[] args) {
    workbook wb =null;
    sheet sheet = null;
    row row = null;
    list<map<string,string>> list = null;
    string celldata = null;
    string filepath = "d:\\test.xlsx";
    string columns[] = {"name","age","score"};
    wb = readexcel(filepath);
    if(wb != null){
      //用来存放表中数据
      list = new arraylist<map<string,string>>();
      //获取第一个sheet
      sheet = wb.getsheetat(0);
      //获取最大行数
      int rownum = sheet.getphysicalnumberofrows();
      //获取第一行
      row = sheet.getrow(0);
      //获取最大列数
      int colnum = row.getphysicalnumberofcells();
      for (int i = 1; i<rownum; i++) {
        map<string,string> map = new linkedhashmap<string,string>();
        row = sheet.getrow(i);
        if(row !=null){
          for (int j=0;j<colnum;j++){
            celldata = (string) getcellformatvalue(row.getcell(j));
            map.put(columns[j], celldata);
          }
        }else{
          break;
        }
        list.add(map);
      }
    }
    //遍历解析出来的list
    for (map<string,string> map : list) {
      for (entry<string,string> entry : map.entryset()) {
        system.out.print(entry.getkey()+":"+entry.getvalue()+",");
      }
      system.out.println();
    }
  }
  //读取excel
  public static workbook readexcel(string filepath){
    workbook wb = null;
    if(filepath==null){
      return null;
    }
    string extstring = filepath.substring(filepath.lastindexof("."));
    inputstream is = null;
    try {
      is = new fileinputstream(filepath);
      if(".xls".equals(extstring)){
        return wb = new hssfworkbook(is);
      }else if(".xlsx".equals(extstring)){
        return wb = new xssfworkbook(is);
      }else{
        return wb = null;
      }
    } catch (filenotfoundexception e) {
      e.printstacktrace();
    } catch (ioexception e) {
      e.printstacktrace();
    }
    return wb;
  }
  public static object getcellformatvalue(cell cell){
    object cellvalue = null;
    if(cell!=null){
      //判断cell类型
      switch(cell.getcelltype()){
      case cell.cell_type_numeric:{
        cellvalue = string.valueof(cell.getnumericcellvalue());
        break;
      }
      case cell.cell_type_formula:{
        //判断cell是否为日期格式
        if(dateutil.iscelldateformatted(cell)){
          //转换为日期格式yyyy-mm-dd
          cellvalue = cell.getdatecellvalue();
        }else{
          //数字
          cellvalue = string.valueof(cell.getnumericcellvalue());
        }
        break;
      }
      case cell.cell_type_string:{
        cellvalue = cell.getrichstringcellvalue().getstring();
        break;
      }
      default:
        cellvalue = "";
      }
    }else{
      cellvalue = "";
    }
    return cellvalue;
  }
}

四、运行结果

  代码运行前保证在d盘下有一个test.xlsx文档,不然报文件找不到异常;excel文档中的表头要和代码中的string columns[] = {"name","age","score"}对应起来。

java解析Excel的方法(xls、xlsx两种格式)

总结

以上所述是小编给大家介绍的java解析excel的方法(xls、xlsx两种格式),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://www.cnblogs.com/hhhshct/p/7255915.html