激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - java使用POI批量導(dǎo)入excel數(shù)據(jù)的方法

java使用POI批量導(dǎo)入excel數(shù)據(jù)的方法

2020-12-04 08:51WhyWin Java教程

這篇文章主要為大家詳細(xì)介紹了java使用POI批量導(dǎo)入excel數(shù)據(jù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、定義

  apache poi是apache軟件基金會的開放源碼函式庫,poi提供api給java程序?qū)icrosoft office格式檔案讀和寫的功能。

二、所需jar包:

java使用POI批量導(dǎo)入excel數(shù)據(jù)的方法

三、簡單的一個讀取excel的demo

1、讀取文件方法

?
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
/**
 * 讀取出filepath中的所有數(shù)據(jù)信息
 * @param filepath excel文件的絕對路徑
 *
 */
 
public static void getdatafromexcel(string filepath)
{
  //string filepath = "e:\\123.xlsx";
  
  //判斷是否為excel類型文件
  if(!filepath.endswith(".xls")&&!filepath.endswith(".xlsx"))
  {
    system.out.println("文件不是excel類型");
  }
  
  fileinputstream fis =null;
  workbook wookbook = null;
  
  try
  {
    //獲取一個絕對地址的流
     fis = new fileinputstream(filepath);
  }
  catch(exception e)
  {
    e.printstacktrace();
  }
  
  try
  {
    //2003版本的excel,用.xls結(jié)尾
    wookbook = new hssfworkbook(fis);//得到工作簿
     
  }
  catch (exception ex)
  {
    //ex.printstacktrace();
    try
    {
      //2007版本的excel,用.xlsx結(jié)尾
      
      wookbook = new xssfworkbook(fis);//得到工作簿
    } catch (ioexception e)
    {
      // todo auto-generated catch block
      e.printstacktrace();
    }
  }
  
  //得到一個工作表
  sheet sheet = wookbook.getsheetat(0);
  
  //獲得表頭
  row rowhead = sheet.getrow(0);
  
  //判斷表頭是否正確
  if(rowhead.getphysicalnumberofcells() != 3)
  {
    system.out.println("表頭的數(shù)量不對!");
  }
  
  //獲得數(shù)據(jù)的總行數(shù)
  int totalrownum = sheet.getlastrownum();
  
  //要獲得屬性
  string name = "";
  int latitude = 0;
  
  //獲得所有數(shù)據(jù)
  for(int i = 1 ; i <= totalrownum ; i++)
  {
    //獲得第i行對象
    row row = sheet.getrow(i);
    
    //獲得獲得第i行第0列的 string類型對象
    cell cell = row.getcell((short)0);
    name = cell.getstringcellvalue().tostring();
    
    //獲得一個數(shù)字類型的數(shù)據(jù)
    cell = row.getcell((short)1);
    latitude = (int) cell.getnumericcellvalue();
    
    system.out.println("名字:"+name+",經(jīng)緯度:"+latitude);
    
  }
}

2、測試

?
1
2
3
4
public static void main(string[] args)
  {   
    getdatafromexcel("e:"+ file.separator +"123.xlsx");
  }

3、原始數(shù)據(jù)

java使用POI批量導(dǎo)入excel數(shù)據(jù)的方法

4、結(jié)果

?
1
2
3
4
5
6
7
8
9
10
11
名字:a1,經(jīng)緯度:1
名字:a2,經(jīng)緯度:2
名字:a3,經(jīng)緯度:3
名字:a4,經(jīng)緯度:4
名字:a5,經(jīng)緯度:5
名字:a6,經(jīng)緯度:6
名字:a7,經(jīng)緯度:7
名字:a8,經(jīng)緯度:8
名字:a9,經(jīng)緯度:9
名字:a10,經(jīng)緯度:10
名字:a11,經(jīng)緯度:11

四、注意事項

1、運用多態(tài),excel主要有.xls結(jié)尾(2003版本)和. xlsx(2007版本)兩種類型結(jié)尾的文件,分別需要用hssfworkbook對象對.xls文件進(jìn)行讀取,用xssfworkbook對象對.xlsx文件進(jìn)行讀取,直接使用他們共同的父類workbook進(jìn)行初始化對象有利于代碼的易用性。

2、通過流的方式初始化工作簿對象(workbook),可以通過new xssfworkbook(文件絕對路徑)和new xssfworkbook(輸入流)兩種方式初始化對象,但是假如我們只是通過修改.xls文件的后綴名為.xlsx,這樣子當(dāng)我們用new xssfworkbook(文件絕對路徑)來讀取文件的時候就會報錯,因為他本身就不是一個2007版本的excel類型的文件,讀取會報錯;假如我們是通過流的方式的話,可以避免這種情況,我們即使你修改了文件的后綴名,我們依然在初始化的時候能獲取到該對象是.xls類型文件,使用hssfworkbook對象進(jìn)行處理,即能得出正確的結(jié)果。 

 五、增強(qiáng)版

添加了判斷表頭是否符合規(guī)范,允許表頭對象的位置不同。進(jìn)行了一定的解耦合。

?
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
/**
   *  
   * @param cell 一個單元格的對象
   * @return 返回該單元格相應(yīng)的類型的值
   */
  public static object getrighttypecell(cell cell){
  
    object object = null;
    switch(cell.getcelltype())
    {
      case cell.cell_type_string :
      {
        object=cell.getstringcellvalue();
        break;
      }
      case cell.cell_type_numeric :
      {
        cell.setcelltype(cell.cell_type_numeric);
        object=cell.getnumericcellvalue();
        break;
      }
        
      case cell.cell_type_formula :
      {
        cell.setcelltype(cell.cell_type_numeric);
        object=cell.getnumericcellvalue();
        break;
      }
      
      case cell.cell_type_blank :
      {
        cell.setcelltype(cell.cell_type_blank);
        object=cell.getstringcellvalue();
        break;
      }
    }
    return object;
  
?
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
   * 讀取出filepath中的所有數(shù)據(jù)信息
   * @param filepath excel文件的絕對路徑
   *
   */
  
  public static void getdatafromexcel2(string filepath)
  {
    list<map<string,integer>> list = new arraylist<map<string, integer>>();
    //判斷是否為excel類型文件
    if(!filepath.endswith(".xls")&&!filepath.endswith(".xlsx"))
    {
      system.out.println("文件不是excel類型");
    }
    
    fileinputstream fis =null;
    workbook wookbook = null;
    int flag = 0;
    
    try
    {
      //獲取一個絕對地址的流
       fis = new fileinputstream(filepath);
    }
    catch(exception e)
    {
      e.printstacktrace();
    }
    
    try
    {
      //2003版本的excel,用.xls結(jié)尾
      wookbook = new hssfworkbook(fis);//得到工作簿
       
    }
    catch (exception ex)
    {
      //ex.printstacktrace();
      try
      {
        //2007版本的excel,用.xlsx結(jié)尾
        
        wookbook = new xssfworkbook(filepath);//得到工作簿
      } catch (ioexception e)
      {
        // todo auto-generated catch block
        e.printstacktrace();
      }
    }
    
    //得到一個工作表
    sheet sheet = wookbook.getsheetat(0);
    
    //獲得表頭
    row rowhead = sheet.getrow(0);
    
   //根據(jù)不同的data放置不同的表頭
    map<object,integer> headmap = new hashmap<object, integer>();
    
    
    //判斷表頭是否合格 ------------------------這里看你有多少列
    if(rowhead.getphysicalnumberofcells() != 2)
    {
      system.out.println("表頭列數(shù)與要導(dǎo)入的數(shù)據(jù)庫不對應(yīng)");
    }
    
    try
    {
      //----------------這里根據(jù)你的表格有多少列
      while (flag < 2)
      {
        cell cell = rowhead.getcell(flag);
        if (getrighttypecell(cell).tostring().equals("基站名"))
        {
          headmap.put("jizhan", flag);
        }
        if (getrighttypecell(cell).tostring().equals("經(jīng)緯度"))
        {
          headmap.put("jingweidu", flag);
        }
        flag++;
      }
    } catch (exception e)
    {
      e.printstacktrace();
      system.out.println("表頭不合規(guī)范,請修改后重新導(dǎo)入");
    }
    
    
    //獲得數(shù)據(jù)的總行數(shù)
    int totalrownum = sheet.getlastrownum();
    
    
    
    //要獲得屬性
    string name = "";
    double latitude = 0;
    
    if(0 == totalrownum)
    {
      system.out.println("excel內(nèi)沒有數(shù)據(jù)!");
    }
    
    cell cell_1 = null,cell_2 = null;
    
    //獲得所有數(shù)據(jù)
    for(int i = 1 ; i <= totalrownum ; i++)
    {
      //獲得第i行對象
      row row = sheet.getrow(i);
      
      try
      {
        cell_1 = row.getcell(headmap.get("jizhan"));
        cell_2 = row.getcell(headmap.get("jingweidu"));
      } catch (exception e)
      {
        e.printstacktrace();
        system.out.println("獲取單元格錯誤");
      }
      
      try
      {
        //基站
        name = (string) getrighttypecell(cell_1);
        //經(jīng)緯度
        latitude = (double) getrighttypecell(cell_2);
      } catch (classcastexception e)
      {
        e.printstacktrace();
        system.out.println("數(shù)據(jù)不全是數(shù)字或全部是文字!");
      }
      system.out.println("名字:"+name+",經(jīng)緯度:"+latitude);
      
    }
  }
 

異常情況:

應(yīng)將下面這段代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
try
    {
      //2003版本的excel,用.xls結(jié)尾
      wookbook = new hssfworkbook(fis);//得到工作簿
       
    }
    catch (exception ex)
    {
      //ex.printstacktrace();
      try
      {
        //2007版本的excel,用.xlsx結(jié)尾
        
        wookbook = new xssfworkbook(fis);//得到工作簿
      } catch (ioexception e)
      {
        // todo auto-generated catch block
        e.printstacktrace();
      }
    }

改為:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
try
   {
     //2003版本的excel,用.xls結(jié)尾
     wookbook = new hssfworkbook(fis);//得到工作簿
      
   }
   catch (exception ex)
   {
     //ex.printstacktrace();
     try
     {
       //這里需要重新獲取流對象,因為前面的異常導(dǎo)致了流的關(guān)閉——加了這一行
        fis = new fileinputstream(filepath);
       //2007版本的excel,用.xlsx結(jié)尾
       
       wookbook = new xssfworkbook(filepath);//得到工作簿
     } catch (ioexception e)
     {
       // todo auto-generated catch block
       e.printstacktrace();
     }
   }

解析:因為前面異常導(dǎo)致了流的關(guān)閉,所以需要重新創(chuàng)建一個流對象。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女视频在线观看黄 | 成人爽a毛片免费啪啪红桃视频 | 欧美韩国一区 | 成人网视频 | 黄污网址 | 全黄裸片武则天艳史 | asiass极品裸体女pics | 国内精品免费一区二区2001 | 国产免费观看一区二区三区 | 涩涩操 | 久久无| 欧美性猛交xxx乱大交3蜜桃 | 一级在线视频 | 一级在线| 国产高清永久免费 | 国产1区2区3区中文字幕 | 91午夜视频| 国产精品区在线12p 午夜视频色 | caoporn国产一区二区 | 精品亚洲免费 | 99视频在线观看视频 | 欧美在线黄色 | 91精品国产91久久久 | 九草网 | 国产精品久久久久久久久久尿 | 精品一区二区三区免费 | 成人福利在线视频 | 最新影院 | chinese乱子伦xxxx国语对白 | 美女羞羞视频网站 | 亚洲网站在线观看 | 国产一区二区不卡视频 | 免费高清一级欧美片在线观看 | 成人免费毛片网站 | 国产精品亚洲综合一区二区三区 | 成人在线观看一区二区 | 久久成人国产精品 | 久久人人做 | 国产毛片aaa一区二区三区视频 | 久久艹综合 | 中文字幕www |