开发者社区> 问答> 正文

怎么样用Eclipse解析.xls文件

怎么样用Eclipse解析.xls文件

展开
收起
爵霸 2016-06-15 10:06:22 2060 0
1 条回答
写回答
取消 提交回答
  • 这段代码可以把一个Excel读到Map中,你可以试试

     
    /**
         * 读取多张工作表,多张工作表以shell的形式出现
         * @param fileName 文件名,文件不存在会抛出异常
         * @return 返回读取结果
         */
        public java.util.Map<String, List<String>>[] readWorkBook(InputStream in) throws IOException {
             
            // 构建Workbook对象, 只读Workbook对象
            Workbook readwb = null;
            try {
                readwb = Workbook.getWorkbook(in);
            } catch (BiffException e) {
                throw new IOException(e);
            }
            // Sheet的下标是从0开始
            Sheet shells[] = readwb.getSheets();
            Map<String, List<String>> tables[] = new Map[shells.length];
            // 获取第一张Sheet表
            for(int index = 0; index < shells.length; index++){
                tables[index] = new HashMap<String, List<String>>();
                // 获取Sheet表中所包含的总行数
                int rsColumns = shells[index].getColumns();
                // 获取Sheet表中所包含的总列数
                int rsRows = shells[index].getRows();
                for (int i = 0; i < rsColumns; i++) {
                    String head = null;
                    List<String> rowData = new ArrayList<String>();
                    for (int j = 0; j < rsRows; j++) {
                        // 获取指定单元格的对象引用
                        Cell cell = shells[index].getCell(i, j);
                        if(j == 0){
                            head = cell.getContents();
                        } else {
                            rowData.add(cell.getContents());
                        }
                    }
                     
                    tables[index].put(head, rowData);
                }
            }
            try{
                in.close();
            } catch (IOException e) {
                throw e;
            } finally{
                readwb.close();
            }
            return tables;
     
        }
     
    2019-07-17 19:38:44
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像