java jxl 读取excel

来源:互联网 发布:矢量软件 编辑:程序博客网 时间:2024/05/18 01:13
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();    }}

原创粉丝点击