您的位置:首页 > 编程语言 > Java开发

java jxl 读取excel

2017-09-26 09:50 309 查看
private void parseData(byte[] data) {
InputStream is = new ByteArrayInputStream(data);
Workbook book = null;
try {
book = Workbook.getWorkbook(is);
Sheet sheet = book.getSheet(0);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < sheet.getRows(); i++) {
Cell[] cells = sheet.getRow(i);
if (i == 0) {
String[] cellArr = new String[cells.length];
for (int j = 0; j < cells.length; j++) {
if (cells[j].getType() != CellType.EMPTY) {
cellArr[j] = cells[j].getContents().replaceAll("\\n", "");
}
}
this.columns = cellArr;
} else {
HashMap<String, String> map = new HashMap<String, String>();
for(int j = 0; j < columns.length; j++) {
String str = "";
if (!StringUtil.isNullOrEmpty(columns[j])) {
if (cells[j].getType() == CellType.DATE) {
DateCell datecell = (DateCell)cells[j];
Date date = datecell.getDate();
str = DateUtil.getDate(date);
} else {
str = cells[j].getContents();
}
map.put(sheet.getRow(0)[j].getContents(), str);
}
}
list.add(map);
}
}
this.rows = list;
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
book.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java jxl 读取excel