写入 Excel 文件

// 写入 Excel 文件
// ============================================================================== //
Workbook wb = new HSSFWorkbook(); // or Workbook wb = new XSSFWorkbook(); // Create a sheet.
Sheet sheet = wb.createSheet("The Name of the Sheet"); // Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(0); // Create a cell and put a value in it.
Cell cell = row.createCell(0);
cell.setCellValue("Let me have a try!"); // Or do it on one line.
row.createCell(1).setCellValue("Let me have a try!"); /*
备注:
try (...) {
...
}
上述的表达能够在所有语句执行完之后自动释放括号中的资源。
*/
try (OutputStream fileOut = new FileOutputStream("workbook.xls")) { // or workbook.xlsx
wb.write(fileOut);
} catch (FileNotFoundException e) {
System.out.println("The path is not correct!!!");
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
}

读取 Excel 文件

// 读取 Excel 文件到 Workbook 对象中
// ============================================================================== // /*
使用 File 对象比使用 InputStream 对象所需的内存资源更少,因为使用 InputStream 对象
需要将整个文件缓存到内存中。
*/ // Use a file
try {
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls")); // or MyExcel.xlsx
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} // Use an InputStream, needs more memory
try (InputStream in = new FileInputStream("MyExcel.xls")) { // or MyExcel.xlsx
Workbook wb = WorkbookFactory.create(in);
} catch (FileNotFoundException e) {
System.out.println("The file does not exist or the path is not correct!!!");
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}

遍历 Excel 文件

// 遍历 Excel
// ============================================================================== //
try {
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls")); // or MyExcel.xlsx for (Sheet sheet : wb) {
for (Row row : sheet) {
for (Cell cell : row) {
// pass
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}

需要的 maven 依赖

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>

完整代码

package ai.labrador.poi.excel;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*; import java.io.*; /**
* 学习使用 HSSF 或者 XSSF 读写 Excel 文件
* Reference:
* [Busy Developers' Guide to HSSF and XSSF Features](http://poi.apache.org/spreadsheet/quick-guide.html#NewWorkbook)
*
* @author Genpeng Xu
* @date 2018/06/05
*/
public class LearnHSSFAndXSSF {
public static void main(String[] args) { // 写入 Excel 文件
// ============================================================================== //
Workbook wb = new HSSFWorkbook(); // or Workbook wb = new XSSFWorkbook(); // Create a sheet.
Sheet sheet = wb.createSheet("The Name of the Sheet"); // Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(0); // Create a cell and put a value in it.
Cell cell = row.createCell(0);
cell.setCellValue("Let me have a try!"); // Or do it on one line.
row.createCell(1).setCellValue("Let me have a try!"); /*
备注:
try (...) {
...
}
上述的表达能够在所有语句执行完之后自动释放括号中的资源。
*/
try (OutputStream fileOut = new FileOutputStream("workbook.xls")) { // or workbook.xlsx
wb.write(fileOut);
} catch (FileNotFoundException e) {
System.out.println("The path is not correct!!!");
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
} // 读取 Excel 文件到 Workbook 对象中
// ============================================================================== // /*
使用 File 对象比使用 InputStream 对象所需的内存资源更少,因为使用 InputStream 对象
需要将整个文件缓存到内存中。
*/ // Use a file
try {
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls")); // or MyExcel.xlsx
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} // Use an InputStream, needs more memory
try (InputStream in = new FileInputStream("MyExcel.xls")) { // or MyExcel.xlsx
Workbook wb = WorkbookFactory.create(in);
} catch (FileNotFoundException e) {
System.out.println("The file does not exist or the path is not correct!!!");
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} // 遍历 Excel
// ============================================================================== //
try {
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls")); // or MyExcel.xlsx for (Sheet sheet : wb) {
for (Row row : sheet) {
for (Cell cell : row) {
// pass
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
}
}

Apache POI 读写 Excel 文件的更多相关文章

  1. 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据

    1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...

  2. 使用poi读写excel文件

    使用poi库测试了一下读取excel文件,效果不错,跟大家分享一下. 第一列是数值型,第二列是字符型,代码如下: package poi; import java.io.FileInputStream ...

  3. 使用apache POI解析Excel文件

    1. Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. 2. POI结构 ...

  4. Apache POI读写Excel

    Apache POI是Apache软件基金会的开放源码函式库,POIAPI给Java程序对Microsoft Office格式档案读和写的功能. 官方文档 [https://poi.apache.or ...

  5. 如何用Apache POI操作Excel文件-----如何对一个单元格加注解?

    有的时候,我们需要通过操作Apache POI,在生成Cell数据的同时,能对其生成的Cell,加上注解(comments),类似于下面的. 那么对于这种情况,我们的代码应该如何写呢? 借花献佛,我就 ...

  6. 如何用Apache POI操作Excel文件-----如何在已有的Excel文件中插入一行新的数据?

    在POI的第一节入门中,我们提供了两个简单的例子,一个是如何用Apache POI新建一个工作薄,另外一个例子是,如果用Apache POI新建一个工作表.那么在这个章节里面,我将会给大家演示一下,如 ...

  7. 如何用Apache POI操作Excel文件-----如何用Apache POI 画一个离散图

    有的时候,我们需要Excel中的数据,通过一个图画,可视化的表现出来. 那么这个时候,应该如何做呢?现在就借花献佛,以Apache POI自己提供的一个例子为例,给大家演示一下POI的API 如何画图 ...

  8. (6) 如何用Apache POI操作Excel文件-----POI-3.10的一个和注解(comment)相关的另外一个bug

    如果POI-3.10往一个工作表(sheet)里面插入数据的话,需要注意了,其有一个不太被容易发现的bug. 被插入的工作表(sheet)里面的单元格没有包含任何的注解(comment)的时候,插入一 ...

  9. java使用Apache POI操作excel文件

    官方介绍 HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is ...

随机推荐

  1. 50个必备的实用jQuery代码段(转)

    1. 如何创建嵌套的过滤器: //允许你减少集合中的匹配元素的过滤器, //只剩下那些与给定的选择器匹配的部分.在这种情况下, //查询删除了任何没(:not)有(:has) //包含class为“s ...

  2. Android开源库集锦(转)

    一.兼容类库 ActionBarSherlock : Action Bar是Android 3.0后才开始支持的,ActionBarSherlock是让Action Bar功能支持2.X后的所有平台, ...

  3. find_circ 识别circRNA 的原理

    find_circ 通过识别junction reads 来预测circRNA 和参考基因组比对完之后,首先剔除和基因组完全比对的reads,保留没比对上的reads, 这部分reads 直接比是比对 ...

  4. 小知识(class文件查看jdk版本,beyond,could not find setter)

    最近几天工作当中遇到了一些问题,所以记录下来. 1.如何查看class文件的sdk版本 2.beyond compare比对文件 3.Could not find setter for native_ ...

  5. 【Java面试题】35 List, Set, Map是否继承自Collection接口?

    Collection是最基本的集合接口,声明了适用于JAVA集合(只包括Set和List)的通用方法. Set 和List 都继承了Conllection:Set具有与Collection完全一样的接 ...

  6. 无法在Word中打开MathType怎么办

    MathType是一种数学公式编辑器,通常我们都是与Office文档配合使用,但是如果大家在Word中使用MathType编辑公式时,遇到MathType无法打开的情况,我们应该怎么办?下面我们就针对 ...

  7. 最值得一看的几条简单的谷歌 Google 搜索技巧,瞬间提升你的网络搜索能力

    可能你和我一样,几乎每天都必须与搜索引擎打交道,不过很多时候,你辛辛苦苦搜了半天也没找到合适的资料,然而“高手们”上来一眨眼功夫就能命中目标了.这并不是别人运气好,而是搜索引擎其实是有很多技巧可以帮助 ...

  8. php 在windows下配置虚拟目录的方法

    1.先找到apache的配置文件 httpd.conf 找如如下代码: # Virtual hosts#Include conf/extra/httpd-vhosts.conf 把# Include ...

  9. 静默安装oracle 11g,环境预检查时报错,SEVERE: [FATAL] PRVF-0002 : 无法检索本地节点名

    环境描述: 操作系统:Redhat 6.6_x64 oracle:11.2.0.4 x64 问题描述: 今天在安装oracle 11g的数据库,在进行预安装环境检查的时候,报下面的错误: [oracl ...

  10. Java精选笔记_Tomcat开发Web站点

    Tomcat开发Web站点 Web开发的相关知识 B/S架构和C/S架构 C/S架构是Client/Server的简写,也就是客户机/服务器端的交互.常见应用 : QQ. 迅雷. 360. 旺旺等 B ...