我们在工作或者做项目的时候,有时候经常会遇到Excel的读写处理,如导出excel或者导入Excel到数据库中……;因此就会我们学一些简单的技术框架来操作Excel,而当下操作Excel比较流行的就是Apache POI和阿里巴巴的easyExcel;
下面我将向大家介绍使用POI操作Excel的基本读写,具体如下:

1.什么是Apache POI

Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。POI为“Poor Obfuscation Implementation”的首字母缩写,意为“简洁版的模糊实现”。

android ptp 挂载之后_apache

2.应用场景

1.将用户信息导出为excel表格;
2.将Excel表中的信息 录入到网站数据库;

3.IDEA环境的搭建

打开IDEA,创建一个普通的Maven项目,自定义项目名称,位置以及工作坐标!

android ptp 挂载之后_java_02

4.导入依赖

在pom.xml中添加依赖,包括poi,日期格式化工具,junit单元测试,具体如下。

<dependencies>
<!--        xls(03)-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
<!--         xlsx(07)-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>
<!--        日期格式化工具-->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.1</version>
        </dependency>
<!--        junit测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

5.Excel对象介绍

由于Java语言万物皆对象,所以我们也需要把Excel中的对象抽取出来方便操作使用,Excel对象都分别是工作簿,工作表,行,列,具体如下图所示。

android ptp 挂载之后_java_03

6.测试代码

通过下面的代码将数据写到对应的Excel表中,并将Excel表生成到当前目录下。写的主要过程就是,首先获取工作簿,然后通过工作簿创建一个工作表,然后在表中创建行,最后通过列确定单元格,最后在单元格里写入值。

package com.xing;

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.joda.time.DateTime;
import org.junit.Test;

import java.io.FileOutputStream;

public class ExcelWriteTest {
    String path="D:\\professional-course\\EasyExcel\\poi";//这里需要设置成自己的路径
    @Test
    public void testWrite03() throws Exception {
        //创建一个工作簿
        Workbook workbook=new HSSFWorkbook();//操作excel03版本的工作簿
        //创建一个工作表
        Sheet sheet=workbook.createSheet();
        //创建一行
        Row row=sheet.createRow(0);
        //创建一个单元格
        Cell cell11 = row.createCell(0);
        //通过行和列就确定了一个单元格
        cell11.setCellValue("POI的学习之旅");

        Cell cell12=row.createCell(1);
        cell12.setCellValue("Java学习");

        //第二行
        Row row2=sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("当前时间:");
        Cell cell22=row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd:HH:mm:ss");
        cell22.setCellValue(time);

        //生成一张表(IO流)
        FileOutputStream fileOutputStream = new FileOutputStream(path + "03.xls");
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();
        System.out.println("03.xls生成完毕");
    }
}

7.结果展示

android ptp 挂载之后_android ptp 挂载之后_04