首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
30 篇文章
1
可视化接口管理平台 YApi,让你轻松搞定 API 的管理问题
2
面试必考:秒杀系统如何设计?
3
Spring Boot 中的线程池,这也太好用了!
4
用了很多年的 CMS 垃圾收集器,终于换成了 G1,真香!!
5
雪花算法到底是啥原理?附 Java 实现!
6
SpringBoot:优雅地处理全局异常
7
七种分布式事务的解决方案,一次讲给你听!
8
RabbitMQ 死信队列是什么鬼?
9
RabbitMQ 中的消息还能过期?
10
来,通过 Excel 来认识神器——POI
11
使用easypoi导出excel
12
图文并茂:AQS 是怎么运行的?
13
使用 Docker 部署 Spring Boot 项目,带劲!!
14
写了个牛逼的日志切面,甩锅更方便了!
15
终于有人把 Spring 循环依赖讲清楚了!
16
免费获取 IntelliJ IDEA 激活码的 6 种方式!
17
Spring Boot 如何快速集成 Redis 哨兵?
18
10w+ Excel 数据导入,怎么优化?
19
10 个牛逼的单行代码编程技巧,你会用吗?
20
Logback 配置文件这么写,TPS 提高 10 倍!
21
18 个示例带你掌握 Java 8 日期时间处理!
22
用户密码到底要怎么加密存储?
23
Spring Cloud 如何动态刷新 Git 仓库配置?
24
为什么微服务一定要有网关?
25
牛逼哄哄的数据库连接池,底层原理是个啥?
26
如何快速安全的插入千万条数据?
27
Redis 是怎么实现 “附近的人” 的?
28
20 亿的 URL 集合,如何快速判断其中一个?
29
不用找了,大厂在用的分库分表方案,都在这了!
30
Java 程序员常犯的 10 个 SQL 错误!

来,通过 Excel 来认识神器——POI

Java技术栈

www.javastack.cn

关注阅读更多优质文章

1、POI是什么

Apache POI - the Java API for Microsoft Documents,顾名思义,Apache的三方包,用来操作微软office文档的,多数时候用来操作excel,所以这里就以excel方面来说明。

需要引入两个包,maven地址如下(version 3.9):

代码语言:javascript
复制
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
</dependency>


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

POI的组件列表中,针对excel的主要是HSSF和XSSF组件,前者针对97-2007的通用版excel,即后缀xls;后者针对2007或更高版的excel,即后缀xlsx。

官方概要如下:

代码语言:javascript
复制
HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. 
XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.

2、POI核心类

面向对象面向对象,既然如此,自然去找找一些能表示excel中内容的类。

2.1 工作簿 Workbook

创建或维护Excel工作簿的所有类的超接口,Workbook,属于org.apache.poi.ss.usermodel包。

其下有两个实现类:

  • HSSFWorkbook : 有读取.xls 格式和写入Microsoft Excel文件的方法。它与微软Office97-2003版本兼容
  • XSSFWorkbook : 有读写Microsoft Excel和OpenOffice的XML文件的格式.xls或.xlsx的方法。它与MS-Office版本2007或更高版本兼容

所以在针对不同版本的excel时,需要对应以上使用不同的Workbook。构造函数中,常用的:

HSSFWorkbook

代码语言:javascript
复制
HSSFWorkbook()
HSSFWorkbook(java.io.InputStream s)

XSSFWorkbook

代码语言:javascript
复制
XSSFWorkbook()
XSSFWorkbook(java.io.File file)
XSSFWorkbook(java.io.InputStream is)

2.2 标签页 Sheet

HSSFSheetXSSFSheet 都是Sheet接口的实现类,Sheet可以使用Workbook的两个方法获得:

代码语言:javascript
复制
workbook.createSheet();
workbook.createSheet(String sheetName);

2.3 行 Row

同理,Row是 HSSFRowXSSFRow 的接口,通过Sheet获取:

代码语言:javascript
复制
sheet.createRow(int rownum);

2.4 单元格 Cell

同理,Cell是 HSSFCellXSSFCell 的接口,通过Row获取:

代码语言:javascript
复制
row.createCell(int column);
row.createCell(int column, int type);

3、创建和读取

其实如果能理解面向对象,就很简单了,另外包括字体,公式,超链接等,都有对应的封装类,此处只提出了核心的几个,需要了解更多的需要自行展开。

例子的话,直接从别人教程里摘出来吧,另,读取的workbook,可以debug瞅瞅内容。关注公从号Java技术栈,回复:工具,可以获取更多工具系列干货。

3.1 创建空白工作簿

代码语言:javascript
复制
import java.io.*;
import org.apache.poi.xssf.usermodel.*;
public class CreateWorkBook 
{
   public static void main(String[] args)throws Exception 
   {
      
      XSSFWorkbook workbook = new XSSFWorkbook(); 
      
      FileOutputStream out = new FileOutputStream(
      new File("createworkbook.xlsx"));
      
      workbook.write(out);
      out.close();
      System.out.println("
      createworkbook.xlsx written successfully");
   }
}

3.2 打开现有的工作簿

代码语言:javascript
复制
import java.io.*;
import org.apache.poi.xssf.usermodel.*;
public class OpenWorkBook
{
   public static void main(String args[])throws Exception
   { 
      File file = new File("openworkbook.xlsx");
      FileInputStream fIP = new FileInputStream(file);
      
      XSSFWorkbook workbook = new XSSFWorkbook(fIP);
      if(file.isFile() && file.exists())
      {
         System.out.println(
         "openworkbook.xlsx file open successfully.");
      }
      else
      {
         System.out.println(
         "Error to open openworkbook.xlsx file.");
      }
   }
}

3.3、任意对象List转至为Excel文档

可用注解定义标签名和列名,写了个方法,可以将某个类的List转换为对应的Excel文档,列名如果在不使用注解的情况下默认为属性名:

推荐阅读:Java 中初始化 List 集合的 6 种方式!

类:

代码语言:javascript
复制
@Excel(name = "学生标签页")
public class Student {

    @Excel(name = "姓名")
    private String name;

    private boolean male;

    @Excel(name = "身高")
    private int height;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isMale() {
        return male;
    }

    public void setMale(boolean male) {
        this.male = male;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

测试方法:

代码语言:javascript
复制
public static void main(String[] args) {
    List<Student> list = new ArrayList<Student>();
    Student student1 = new Student();
    student1.setName("小红");
    student1.setMale(false);
    student1.setHeight(167);

    Student student2 = new Student();
    student2.setName("小明");
    student2.setMale(true);
    student2.setHeight(185);

    list.add(student1);
    list.add(student2);

    File file = new File("C:/Users/Dulk/Desktop/1314.xls");
    createExcel(list, file);
}

输出结果:

注解:

代码语言:javascript
复制
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;


@Retention(RetentionPolicy.RUNTIME)
public @interface Excel {
    
    public String name() default "";
}

方法:

代码语言:javascript
复制
import org.apache.log4j.Logger;
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.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


public class ExcelUtil {
    private static Logger log = Logger.getLogger(ExcelUtil.class);

    
    public static Workbook gainWorkbook(File file) throws ExcelException {
        if (!isExcel(file)) {
            throw new ExcelException("文件不是Excel类型");
        }
        
        if (!file.exists()) {
            try {
                OutputStream os = new FileOutputStream(file);
                Workbook workbook = isOlderEdition(file) ? new HSSFWorkbook() : new XSSFWorkbook();
                workbook.write(os);
                log.debug("文件不存在,新建该Excel文件");
                os.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            InputStream is = new FileInputStream(file);
            return isOlderEdition(file) ? new HSSFWorkbook(is) : new XSSFWorkbook(is);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    
    private static boolean isOlderEdition(File file) {
        return file.getName().matches(".+\\.(?i)xls");
    }

    
    private static boolean isExcel(File file) {
        String fileName = file.getName();
        String regXls = ".+\\.(?i)xls";
        String regXlsx = ".+\\.(?i)xlsx";
        return fileName.matches(regXls) || fileName.matches(regXlsx);
    }

    
    public static <E> Workbook createExcel(List<E> list, File file) {
        String sheetName = "default";
        if (list.size() == 0) {
            return null;
        }

        Workbook workbook = null;
        try {
            Class clazz = list.get(0).getClass();
            Field[] fields = clazz.getDeclaredFields();
            if (clazz.isAnnotationPresent(Excel.class)) {
                Excel excel = (Excel) clazz.getAnnotation(Excel.class);
                sheetName = excel.name();
            }

            workbook = gainWorkbook(file);
            Sheet sheet = workbook.createSheet(sheetName);
            
            Row line = sheet.createRow(0);
            for (int k = 0; k < fields.length; k++) {
                Cell cell = line.createCell(k);
                String columnName = fields[k].getName();
                if (fields[k].isAnnotationPresent(Excel.class)) {
                    Excel excel = fields[k].getAnnotation(Excel.class);
                    columnName = excel.name();
                }
                cell.setCellValue(columnName);
            }
            
            for (int i = 1; i <= list.size(); i++) {
                Row row = sheet.createRow(i);
                for (int j = 1; j <= fields.length; j++) {
                    Cell cell = row.createCell(j - 1);
                    String fieldName = fields[j - 1].getName();
                    String fieldFirstLetterUpper = fieldName.substring(0, 1).toUpperCase();
                    String prefix = "get";
                    if ("boolean".equals(fields[j - 1].getType().getName())) {
                        prefix = "is";
                    }
                    String methodName = prefix + fieldFirstLetterUpper + fieldName.substring(1);
                    Method method = clazz.getMethod(methodName);
                    cell.setCellValue(String.valueOf(method.invoke(list.get(i - 1))));
                }
            }
            log.debug("List读入完毕");
            OutputStream os = new FileOutputStream(file);
            workbook.write(os);
            os.close();

        } catch (ExcelException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return workbook;
    }
}

参考链接:www.yiibai.com/apache_poi/apache_poi_core_classes.html

作者:Dulk 来源:www.cnblogs.com/deng-cc/p/7443192.html

下一篇
举报
领券