写入 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. QT 交叉编译工具选择

    使用QT交叉编译,生成的都是x86的可执行文件.Zoro告诉我交叉工具配置错了. 参考链接: http://www.cnblogs.com/zengjfgit/p/4744507.html linux ...

  2. 在linux上开发210的hdmi-servers输出

    这段时间一直在研究hdmi-servers,因为友善对这个在是闭源的,所以由于兴趣的关系和工作的关系,决定自己写一个hdmi-servers. 在hdmi中,最关键的是弄清楚了Hdmi显示数据的怎么来 ...

  3. Java处理图片时编译不通过

    Java中处理图片时,MyEclipse需要导入以下包: import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.j ...

  4. JVM基础知识与配置

    1 怎样设置JVM内存设置 本文向大家简介一下进行JVM内存设置几种方法.安装Java开发软件时.默认安装包括两个目录,一个JDK(Java开发工具箱).一个JRE(Java执行环境,内含JVM),当 ...

  5. 转载:Create a Flash Login System Using PHP and MySQL

    本文共两部分: 1. http://dev.tutsplus.com/tutorials/create-a-flash-login-system-using-php-and-mysql-part-1- ...

  6. glsl 多重纹理

    #include"glsl.h" void SHADER::drawBox() { glBegin(GL_QUADS); // Front Face glNormal3f( 0.0 ...

  7. poj 3740 Easy Finding(Dancing Links)

    Easy Finding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15668   Accepted: 4163 Des ...

  8. 解决导入protobuf源代码Unity报错的问题

    将源代码导入Assets目录后, unity引擎会出现以下报错: 解决办法: 在 unity项目Assets目录中创建smcs.rsp文件,内容为-unsafe,其作用为可编译不安全代码.     然 ...

  9. linux convert命令安装及使用

    linux下ImageMagick安装和使用 检查系统有无安装ImageMagick shell> rpm -qa | grep ImageMagick 没有就开始安装ImageMagick s ...

  10. oracle扩展dblink数。

    [标记]在进行数据迁移时:出现 Compilation errors for PROCEDURE ZDGAME.GFF_FETCH_MZR_LOG Error: ORA-04052: error oc ...