当前位置:网站首页>通过POI追加数据到excel中小案例
通过POI追加数据到excel中小案例
2022-07-05 19:12:00 【lang20150928】
当前poi的版本为
<poi.veriosn>4.1.2</poi.veriosn>
相关依赖
<!-- apache POI for xls -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.veriosn}</version>
</dependency>
<!-- apache POI for xlsx -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.veriosn}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.veriosn}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${poi.veriosn}</version>
</dependency>
案例代码
public static void main(String[] args) throws IOException {
String fileName = "D:\\data\\excel\\workbook.xls";
File file = new File(fileName);
if (file.exists()) {
FileUtils.forceDelete(file);
}
// 首先创建一个空的excel
try (Workbook wb = WorkbookFactory.create(false)) {
Sheet wbSheet = wb.createSheet();
Row row = wbSheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("First Row");
try (FileOutputStream fileOut = new FileOutputStream(fileName)) {
wb.write(fileOut);
}
}
for (int i = 0; i < 10; i++) {
// 根据指定的流创建excel
try (Workbook wb = WorkbookFactory.create(new FileInputStream(fileName))) {
Sheet wbSheet = wb.getSheetAt(0);
Row row = wbSheet.createRow(i + 1);
Cell cell = row.createCell(i);
cell.setCellValue("Hello World!" + i);
try (FileOutputStream fileOut = new FileOutputStream(fileName)) {
wb.write(fileOut);
}
}
}
}
结果如下所示(后面的Hello World都是通过追加的方式添加的)
边栏推荐
- Hiengine: comparable to the local cloud native memory database engine
- Applet modification style (placeholder, checkbox style)
- The basic grammatical structure of C language
- UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
- Mariadb root用户及普通用户的密码 重置
- Go语言 | 03 数组、指针、切片用法
- IFD-x 微型红外成像仪(模块)关于温度测量和成像精度的关系
- 开源 SPL 消灭数以万计的数据库中间表
- cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
- Is the performance evaluation of suppliers in the fastener industry cumbersome? Choose the right tool to easily counter attack!
猜你喜欢
Common interview questions in Android, 2022 golden nine silver ten Android factory interview questions hit
5年经验Android程序员面试27天,2022程序员进阶宝典
Go语言 | 03 数组、指针、切片用法
软件测试工程师是做什么的?待遇前景怎么样?
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
如何实现游戏中的在线计时器和离线计时器
JMeter 常用的几种断言方法,你会了吗?
出海十年:新旧接力,黑马崛起
Fuzor 2020软件安装包下载及安装教程
面试官:Redis中集合数据类型的内部实现方式是什么?
随机推荐
Common interview questions in Android, 2022 golden nine silver ten Android factory interview questions hit
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Word finds red text word finds color font word finds highlighted formatted text
R语言使用lubridate包处理日期和时间数据实战
出海十年:新旧接力,黑马崛起
cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion
在线协作产品哪家强?微软 Loop 、Notion、FlowUs
vagrant2.2.6支持virtualbox6.1版本
Tupu software digital twin smart wind power system
HAC集群修改管理员用户密码
潘多拉 IOT 开发板学习(HAL 库)—— 实验8 定时器中断实验(学习笔记)
Blue sky drawing bed Apple quick instructions
Debezium系列之:IDEA集成词法和语法分析ANTLR,查看debezium支持的ddl、dml等语句
不愧是大佬,字节大牛耗时八个月又一力作
ELK分布式日志分析系统部署(华为云)
软件测试是干什么的?学习有啥要求?
太牛了,看这篇足矣了
国内低代码开发平台靠谱的都有哪些?
How MySQL queries and modifies JSON data