zoukankan      html  css  js  c++  java
  • java操作excel-----poi

    一、所需依赖包

      1、使用maven会自动导入相关依赖,所以只需要导入2007版的的包,其他包自动导入,包括2003所需jar包。

        

            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.17</version>
            </dependency>

      2、如果需要直接导包,下面这些包全部下载下来,手动导入。

            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.17</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.17</version>
            </dependency>
            <dependency>
                <groupId>org.apache.xmlbeans</groupId>
                <artifactId>xmlbeans</artifactId>
                <version>3.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>3.17</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-collections4</artifactId>
                <version>4.3</version>
            </dependency>        

        

    二、excel读取

      1、数据模板

        

      2、演示代码

        

        
        @SuppressWarnings("resource")
        public static void main(String[] args) {
            //excel所在地址
            String path = "E:\demo\excel\one.xlsx";
            
            File file = new File(path);
            InputStream in = null;
            
            try {
                in = new FileInputStream(file);
                //文件后缀名
                String type = path.substring(path.indexOf(".")+1);
                Workbook wb;
                
                //根据后缀名选择不同版本的实现2003/2007版,分别对应xls/xlsx
                if("xlsx".equals(type) || "XlSX".equals(type)) {
                    wb = new XSSFWorkbook(in);
                }else if("xls".equals(type) || "XLS".equals(type)) {
                    wb = new HSSFWorkbook(in);
                }else {
                    System.out.println("文件后缀格式有误");
                    return;
                }
                //获取sheet
                Sheet sheet = wb.getSheetAt(0);
                //总行数
                int rsRows = sheet.getPhysicalNumberOfRows();
                //总列数
                int rsColumn = sheet.getRow(0).getPhysicalNumberOfCells();
                
                //存放excel的二维数组
                String[][] data = new String[rsRows][rsColumn];
    //            Row row ;
                Cell cell;
                for(int i =0;i<rsRows;i++) {
                    for(int j =0;j<rsColumn;j++) {
                        cell = sheet.getRow(i).getCell(j);
                        //把非string类型的单元格设置成string
                        cell.setCellType(CellType.STRING);
                        data[i][j] = cell.getStringCellValue();
                    }
                }
                
                //打印结果
                for(int i=0;i<data.length;i++) {
                    for(int j=0;j<data[i].length;j++) {
                        System.out.print(data[i][j]+"	");
                    }
                    System.out.println();
                }
                
            } catch (FileNotFoundException e) {
                System.out.println("读取文件失败");
                e.printStackTrace();
            } catch (IOException e) {
                System.out.println("创建workbook失败");
                e.printStackTrace();
            }finally {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
            
            
        
        }

    结果: 

    id    name    address    
    1    吴    上海    
    2    li    地球    
    3    ki    火星
    就算这个世道烂成一堆粪坑,那也不是你吃屎的理由
  • 相关阅读:
    MongoDB学习笔记(五) MongoDB文件存取操作(转)
    MongoDB学习笔记(四) 用MongoDB的文档结构描述数据关系(转)
    Log4net配置相关
    UML 依赖 关联 聚合 组合
    亲属称谓
    Unity预定义程序集及自定义包编译顺序
    For Windows Phone8 phones make sure that the Windows Phone IP Over USB Transport(IpOverUsbSvc) service is running
    提升Entityframework效率的几种方法
    将RDLC报表工具栏中的英文改为中文
    C#函数式程序设计初探——基础理论篇
  • 原文地址:https://www.cnblogs.com/whalesea/p/10973505.html
Copyright © 2011-2022 走看看