写入 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. linux -- ubuntu 14.10开机出现错误“Error found when loading /root/.profile”解决

    修改完root权限自动登录后,发现开机出现以下提示: Error found when loading /root/.profile stdin:is not a tty ………… 解决方法:在终端中 ...

  2. CentOS基础命令大全

    1.关机 (系统的关机.重启以及登出 ) 的命令 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours: ...

  3. java与c#的语法对比

    1,命名空间与包 C#为了把实现相似功能的类组织在一起,引入了命名空间的概念(namespace) Java中与此对应的东西叫做包(package) 2,类的访问控制方面的不同 C#只有两种:publ ...

  4. PHP利用memcache缓存技术提高响应速度

    PHP下memcache模块是一个高效的守护进程,提供用于内存缓存的过程式程序和面向对象的方便的接口,特别是对于设计动态web程序时减少对数据库的访问.memcache也提供用于通信对话(sessio ...

  5. 根据IP定位城市

    根据IP定位城市:http://www.sucaihuo.com/js/35.html 示例:http://www.sucaihuo.com/jquery/0/35/demo/

  6. shiro 解决 跨域(仅端口不同) 登陆 问题

    1. 登陆成功设置cookie (服务端 通过 json返回 token) //设置cookie document.cookie = "JSESSIONID="+data.data ...

  7. 数字转人民币大写(SQL SERVER)

    --数字转人民币大写NumToRMB ---新建方法create  FUNCTION dbo.NumToRMB (@num numeric(14,5))   RETURNS varchar(100) ...

  8. Extjs不错的博客

    http://www.cnblogs.com/fangsui/category/372751.html http://www.cnblogs.com/WangJinYang/tag/EXT.NET/ ...

  9. php执行外部命令

    php执行外部命令的不少,例如:exec.shell_exec.system.popen等.我特意的讲一下popen,它跟其他函数不太一样,其他函数执行一个命令后,会等待其返回后,再向下执行,而pop ...

  10. PyQt4日历部件QXalendarWidget

    QCalendarWidget类提供了以月为单位地日历部件.该部件允许用户以一种简单而直接的方式选择日期. #!/usr/bin/python # -*- coding: utf-8 -*- impo ...