zoukankan      html  css  js  c++  java
  • poi 导入Excle

    一,AOP 是什么

    Apache POI 提供java 程序对Microsoft Office格式文档的读写功能操作

    二,所需要的jar包

    三,实现代码

     1, 读取Excle 返回Workbook格式

      //读取excel
        public static Workbook readExcel(String filePath) {
            Workbook wb = null;
            if (filePath == null) {
                return null;
            }
            String extString = filePath.substring(filePath.lastIndexOf("."));
            InputStream is = null;
            try {
                is = new FileInputStream(filePath);    //获取文件流
                if (".xls".equals(extString)) {
                    return wb = new HSSFWorkbook(is);   //将文件留转换成相对的Excle 模板    , 2003版本
                } else if (".xlsx".equals(extString)) {
                    return wb = new XSSFWorkbook(is); //将文件留转换成相对的Excle 模板       2007版本
                } else {
                    return wb = null;
                }
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return wb;
        }

    2, 接下来就是要对获取到的workbook 类型进行再一次数据获取

    Workbook :Exlce 模板

    Sheet :工作簿

    Row : 行数据

    Cell : 列数据

      public static  void  getExcle(String path){
            Workbook wb=null; //获取Excle
            Sheet sheet = null; //获取Excle 中的工作簿
            Row row = null; //获工作簿中行数据
    
            wb = readExcel(path);  //获取Excle
            sheet = wb.getSheetAt(0);//获取第一个工作簿
            int rownum =sheet.getPhysicalNumberOfRows();  //获取最大行数
            for(int i=0 ; i<rownum ;i++){  //循环遍历 数据
                row = sheet.getRow(i); //获取当前行数据
                System.out.println("第一列:"+row.getCell(1)+"---第二列"+row.getCell(2));
            }
        }

    优化代码

     public static Map getIncident(String filePath) {
            Workbook wb = null;
            Sheet sheet = null;
            Row row = null;
            List<Map<String, String>> list = null;
            Map<String, String> map = new LinkedHashMap<String, String>();//存放Excle表格内容
            wb = readExcel(filePath);
            if (wb != null) {
                //用来存放表中数据
                list = new ArrayList<Map<String, String>>();
                //获取第一个sheet
                sheet = wb.getSheetAt(0);
                //获取最大行数
                int rownum = sheet.getPhysicalNumberOfRows();
                //获取第一行
                row = sheet.getRow(0);
                //获取最大列数
                int colnum = row.getPhysicalNumberOfCells();
                for (int i = 0; i < rownum; i++) {
                    row = sheet.getRow(i);
                    if (row != null) {
                        map.put((String) getCellFormatValue(row.getCell(0)), (String) getCellFormatValue(row.getCell(1)));
                    } else {
                        break;
                    }
                }
            }
    
            return map;
        /*//遍历解析出来的list
            for (Map.Entry<String,String> entry : map.entrySet()) {
                System.out.print(entry.getKey()+":"+entry.getValue()+",");
            }*/
        }
  • 相关阅读:
    flutter 中 List 和 Map 的用法
    AndroidStudio中Flutter打包APK
    flutter中的异步机制 Future
    [控件] 画饼状图的控件
    用CAShapeLayer写股市K线图动画效果
    使用Instruments中的CoreAnimation分析动画
    山寨Facebook的Shimmer效果
    maskView遮罩中多张图片的动画
    将CAGradientLayer用作maskView的遮罩图层
    [翻译] JKLLockScreenViewController
  • 原文地址:https://www.cnblogs.com/javaLin/p/10306298.html
Copyright © 2011-2022 走看看