国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - java如何讀取Excel簡單模板

java如何讀取Excel簡單模板

2021-06-06 14:11Bour Java教程

這篇文章主要為大家詳細介紹了java如何讀取Excel簡單模板,具有一定的參考價值,感興趣的小伙伴們可以參考一下

場景:對于經常需要導入excel模板或數(shù)據(jù)來解析后加以應用的,使用頻率非常之高,做了一個比較穩(wěn)定的版本,體現(xiàn)在這些地方

工具:org.apache.poi

使用前必須了解這些:

1、要解析,那肯定先判斷是不是excel

2、xls后綴的excel,是03版及以前的用hssfworkbook類
      xlsx后綴的excel,是07版及以后的用xssfworkbook解析

3、getworkbook這個方法是我自己亂造各種excel數(shù)據(jù)不斷測試搜索修正得出的結果,其他的像簡單的判斷后綴xls還是xlsx來決定用hssh還是xssf是不保險的,比如你可能沒遇過org.apache.poi.openxml4j.exceptions.invalidformatexception這樣的異常,當然這個異常仍然是因為excel類型導致獲取workbook時出錯,然而我查到的結果是,excel最底層是xml實現(xiàn)的,類型問題出在這兒,看異常的描述也可以稍微看出來openxml4j.exceptions

4 、可能出現(xiàn)空行,空的單元格,或者單元格值為空的情況,這些情況,在我的readexcel()方法里都考慮到了,為什么我不用迭代器,或者加強的for each循環(huán)?就是因為這些坑爹的空單元格或者空行啊,迭代器內部在取cell單元格對象時跳過這些空的對象,who knows why?我也不知道,反正我測試過,跳過去了,本來5個單元格,一個空的,結果就只得到4個數(shù)據(jù),即使用cell.isempty()和cell!=null來判斷,也沒卵用,因為遍歷的時候直接跳過去了,都沒有判斷的機會

5、取單元格數(shù)據(jù),這個就比較簡單了,判斷單元格類型,根據(jù)類型做相應的處理取出來,但是我覺得我這個getcellvalue()的方法應該有漏洞,先這么用著

下面上代碼,簡單描述下關鍵部位

 

?
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
138
139
140
141
142
143
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;
import java.io.inputstream;
import java.io.pushbackinputstream;
import java.util.arraylist;
import java.util.hashmap;
import java.util.map;
import java.util.list;
import org.apache.poi.poixmldocument;
import org.apache.poi.openxml4j.exceptions.invalidformatexception;
import org.apache.poi.openxml4j.opc.opcpackage;
import org.apache.poi.poifs.filesystem.poifsfilesystem;
import org.apache.poi.ss.usermodel.cell;
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;
import org.apache.poi.hssf.usermodel.hssfcell;
import org.apache.poi.hssf.usermodel.hssfworkbook;
import org.apache.xmlbeans.impl.piccolo.io.fileformatexception;
/**
 *yanbiao 2016.10.25
 */
public class excelutil {
 
  private static final string extension_xls = "xls";
  private static final string extension_xlsx = "xlsx";
 
/**
* 文件檢查
*/
private void prereadcheck(string filepath) throws filenotfoundexception, fileformatexception {
 
file file = new file(filepath);
if (!file.exists()) {
throw new filenotfoundexception("導入的文件不存在:" + filepath);
}
if (!(filepath.endswith(extension_xls) || filepath.endswith(extension_xlsx))) {
throw new fileformatexception("傳入的文件不是excel");
}
}
 /**
   * 取得workbook對象
   * xls:hssfworkbook,03版
   * xlsx:xssfworkbook,07版
  */
 private workbook getworkbook(string filepath) throws ioexception, invalidformatexception {
    //直接判斷后綴來返回相應的workbook對象多數(shù)情況沒問題,但是這個更保險,第3條已經說明 
    workbook wb = null;
    inputstream is = new fileinputstream(filepath);
    if (!is.marksupported()) {
      is = new pushbackinputstream(is, 8);
    }
    if (poifsfilesystem.haspoifsheader(is)) {
       return new hssfworkbook(is);
    }
    if (poixmldocument.hasooxmlheader(is)) {
      return new xssfworkbook(opcpackage.open(is));
    }
    throw new illegalargumentexception("您的excel版本目前不支持poi解析");
  }
 
  /**
   * 讀取excel文件內容
   */
  public map<integer, list<string>> readexcel(string filepath) throws filenotfoundexception, fileformatexception {
    // 檢查和獲取workbook對象
    this.prereadcheck(filepath);
    workbook wb = null;
    map<integer,list<string>> map = new hashmap<integer, list<string>>();
    try {
      wb = this.getworkbook(filepath);
      // 默認只讀取第一個sheet
      sheet sheet = wb.getsheetat(0);
      int rowcount = sheet.getlastrownum();//邏輯行,包括空行
      int cellcount = sheet.getrow(0).getlastcellnum();//第一行(將來作為字段的行)有多少個單元格
      for (int i=0;i<rowcount;i++) {          //這里用最原始的for循環(huán)來保證每行都會被讀取
         list<string> list = new arraylist<string>();
         row row = sheet.getrow(i);
         if(null!=row){
            for (int j=0;j<cellcount;j++) {
             list.add(getcellvalue(row.getcell(j)));  //這里也是用for循環(huán),用cell c:row這樣的遍歷,空單元格就被拋棄了 
            }
            system.out.println("第"+(row.getrownum()+1)+"行數(shù)據(jù):"+list.tostring());
            map.put(row.getrownum(), list);
          }else{
            for (int j=0;j<cellcount;j++) {
              list.add("無數(shù)據(jù)");  
            }
            system.out.println("第"+(i+1)+"行數(shù)據(jù):"+list.tostring());
            map.put(i, list);
        }    
      }   
    } catch (exception e) {
        system.out.println("讀取excel異常:"+e.getmessage());
        e.printstacktrace();
      } finally {
         if (wb != null) {
           try {
               wb.close();
            } catch (ioexception e) {
               e.printstacktrace();
             }
         }
       }
    return map;  
  }
  /**
   * 取單元格的值
   */
  private string getcellvalue(cell c) {
    if (c == null) {
      return "無數(shù)據(jù)";
    }
    string value = "";
    switch (c.getcelltype()){
    case hssfcell.cell_type_numeric://數(shù)字
       value = c.getnumericcellvalue()+"";
    break;
    case hssfcell.cell_type_string://字符串
      value = c.getstringcellvalue();
    break;
    case hssfcell.cell_type_boolean://boolean
      value = c.getbooleancellvalue()+"";
    break;
    case hssfcell.cell_type_formula://公式
      value = c.getcellformula()+"";
    break;
    case hssfcell.cell_type_blank://空值
      value= "無數(shù)據(jù)";
     break;
    case hssfcell.cell_type_error:
      value = "非法字符";
     break;
    default:
      value= "未知類型";
     break;   
    }
    return value;
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/yb38156/p/9821804.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 免费国产一区二区 | 国产一级久久久久 | 久久久久久久久久久久国产 | 黄在线免费观看 | 亚洲精品一区二区三区在线观看 | 国产一区视频在线 | 亚洲成人一区二区 | 欧美日韩精品一区二区三区四区 | 亚洲精品久久久久久久久久久 | 欧美成年人网站 | 国产精品美女久久久久久久久久久 | 国产日韩一区 | 蜜臀精品 | 美日韩免费视频 | 国产资源在线播放 | 中国a一片一级一片 | 黄色大片网站 | 亚洲精品久久久久久一区二区 | 国产精品视频导航 | 9l蝌蚪porny中文自拍 | 国产精品一区三区 | 亚洲一区免费 | 久久九九| 久久精品无码一区二区三区 | 在线免费成人 | 狠狠干网站 | 午夜寂寞少妇aaa片毛片 | 一级黄色片网站 | 国产成人精品一区二区 | 中文字幕 亚洲一区 | 九九在线视频 | 午夜社区| 中文字幕在线免费 | 久久精品日产第一区二区三区 | 亚洲一区二区三区四区五区中文 | 精品国偷自产国产一区 | 高清国产午夜精品久久久久久 | 午夜视频 | 欧美国产精品一区二区三区 | 97久久精品人人做人人爽50路 | 黄色大片网站 |