java 分析excel模板_java如何读取Excel简单模板
場景:對于經(jīng)常需要導(dǎo)入excel模板或數(shù)據(jù)來解析后加以應(yīng)用的,使用頻率非常之高,做了一個比較穩(wěn)定的版本,體現(xiàn)在這些地方
工具:org.apache.poi
使用前必須了解這些:
1、要解析,那肯定先判斷是不是excel
2、xls后綴的excel,是03版及以前的用hssfworkbook類
xlsx后綴的excel,是07版及以后的用xssfworkbook解析
3、getworkbook這個方法是我自己亂造各種excel數(shù)據(jù)不斷測試搜索修正得出的結(jié)果,其他的像簡單的判斷后綴xls還是xlsx來決定用hssh還是xssf是不保險的,比如你可能沒遇過org.apache.poi.openxml4j.exceptions.invalidformatexception這樣的異常,當(dāng)然這個異常仍然是因為excel類型導(dǎo)致獲取workbook時出錯,然而我查到的結(jié)果是,excel最底層是xml實現(xiàn)的,類型問題出在這兒,看異常的描述也可以稍微看出來openxml4j.exceptions
4 、可能出現(xiàn)空行,空的單元格,或者單元格值為空的情況,這些情況,在我的readexcel()方法里都考慮到了,為什么我不用迭代器,或者加強的for each循環(huán)?就是因為這些坑爹的空單元格或者空行啊,迭代器內(nèi)部在取cell單元格對象時跳過這些空的對象,who knows why?我也不知道,反正我測試過,跳過去了,本來5個單元格,一個空的,結(jié)果就只得到4個數(shù)據(jù),即使用cell.isempty()和cell!=null來判斷,也沒卵用,因為遍歷的時候直接跳過去了,都沒有判斷的機會
5、取單元格數(shù)據(jù),這個就比較簡單了,判斷單元格類型,根據(jù)類型做相應(yīng)的處理取出來,但是我覺得我這個getcellvalue()的方法應(yīng)該有漏洞,先這么用著
下面上代碼,簡單描述下關(guān)鍵部位
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("導(dǎo)入的文件不存在:" + 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 {
//直接判斷后綴來返回相應(yīng)的workbook對象多數(shù)情況沒問題,但是這個更保險,第3條已經(jīng)說明
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文件內(nèi)容
*/
public map> readexcel(string filepath) throws filenotfoundexception, fileformatexception {
// 檢查和獲取workbook對象
this.prereadcheck(filepath);
workbook wb = null;
map> map = new hashmap>();
try {
wb = this.getworkbook(filepath);
// 默認(rèn)只讀取第一個sheet
sheet sheet = wb.getsheetat(0);
int rowcount = sheet.getlastrownum();//邏輯行,包括空行
int cellcount = sheet.getrow(0).getlastcellnum();//第一行(將來作為字段的行)有多少個單元格
for (int i=0;i
list list = new arraylist();
row row = sheet.getrow(i);
if(null!=row){
for (int j=0;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
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;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持萬仟網(wǎng)。
希望與廣大網(wǎng)友互動??
點此進行留言吧!
總結(jié)
以上是生活随笔為你收集整理的java 分析excel模板_java如何读取Excel简单模板的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab连续卷积动画实现(gui编程
- 下一篇: 怎么判断一个字符串的最长回文子串是否在头