zoukankan      html  css  js  c++  java
  • ApachePOI 入门案例

    1.创建项目apache_poi

    2.添加依赖

        <!-- ApachePOI依赖-->
        <dependencies>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>4.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>4.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>4.0.1</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
        </dependencies>

    3. 编写测试类,实现导出excel、导入excel

    package com.pomelo.poi;
    
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.junit.Test;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    public class ApachePoiDemo {
    
        //往excel写入数据
        @Test
        public void write() throws Exception {
            // 创建工作簿
            Workbook workbook = new HSSFWorkbook();
            // 创建工作表
            Sheet sheet = workbook.createSheet();
            // 创建第一行
            Row row = sheet.createRow(0);
            // 创建第一行的第一列
            Cell cell = row.createCell(0);
            // 往单元格设置内容
            cell.setCellValue("第一行第一列");
    
            // 输出excel
            workbook.write(new FileOutputStream("c:\tmp\poi\demo1.xls"));
            workbook.close();
        }
    
        // 读取excel内容
        @Test
        public void read() throws Exception {
            // 根据excel文件流获取工作簿
            Workbook workbook = new HSSFWorkbook(new FileInputStream("c:\tmp\poi\demo1.xls"));
            // 获取工作表
            Sheet sheet = workbook.getSheetAt(0);
            // 获取第一行
            Row row = sheet.getRow(0);
            // 获取第一行第一列
            Cell cell = row.getCell(0);
            // 获取单元格
            System.out.println("单元格内容:" + cell.getStringCellValue());
            System.out.println("总行数:"+sheet.getPhysicalNumberOfRows());
            System.out.println("总列数:"+row.getPhysicalNumberOfCells());
    
            workbook.close();
        }
    }
  • 相关阅读:
    JavaScript深拷贝和浅拷贝
    JavaScript正则表达式(一)-常用方法
    面向对象之继承-5种JavaScript继承的方法
    java学习笔记之System类
    MySQL的grant,revoke使用
    LoadRunner+Android模所器录制脚本
    Mysql
    mosquitto_pub和mosquitto_sub 命令参数说明
    【转载】原 IntelliJ IDEA (idea)引入eclipse web项目
    eclipse恢复默认布局
  • 原文地址:https://www.cnblogs.com/pomelo-lemon/p/11395416.html
Copyright © 2011-2022 走看看