当前位置:网站首页>通过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都是通过追加的方式添加的)
边栏推荐
- Blue sky drawing bed Apple quick instructions
- PHP uses ueditor to upload pictures and add watermarks
- 618“低调”谢幕,百秋尚美如何携手品牌跨越“不确定时代”?
- Mathematical modeling of oil pipeline layout MATLAB, mathematical model of oil pipeline layout
- Hiengine: comparable to the local cloud native memory database engine
- Go语言 | 01 WSL+VSCode环境搭建避坑指南
- Taishan Office Technology Lecture: from the layout height of the line, talk about the height overflow and height shrinkage of the drawing height (launched in the whole network)
- Technology sharing | common interface protocol analysis
- Debezium系列之:修改源码支持drop foreign key if exists fk
- Android面试,android音视频开发
猜你喜欢
Benefits of automated testing
Decision tree and random forest
块编辑器如何选择?印象笔记 Verse、Notion、FlowUs
How to choose the notion productivity tools? Comparison and evaluation of notion, flowus and WOLAI
司空见惯 - 英雄扫雷鼠
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
The basic grammatical structure of C language
2022 the most complete Tencent background automation testing and continuous deployment practice in the whole network [10000 words]
Django使用mysqlclient服务连接并写入数据库的操作过程
[AI framework basic technology] automatic derivation mechanism (autograd)
随机推荐
uniapp获取微信头像和昵称
5 years of experience, 27 days of Android programmer interview, 2022 programmer advanced classic
JAD的安装、配置及集成IDEA
测试外包公司怎么样?
The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
5年经验Android程序员面试27天,2022程序员进阶宝典
强化学习-学习笔记4 | Actor-Critic
vagrant2.2.6支持virtualbox6.1版本
Is it safe for Guohai Securities to open an account online?
Debezium系列之:修改源码支持unix_timestamp() as DEFAULT value
You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
司空见惯 - 英雄扫雷鼠
Go语言学习教程(十五)
Oracle故障处理:Ora-10873:file * needs to be either taken out of backup or media recovered
中国银河证券开户安全吗 证券开户
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Go语言 | 01 WSL+VSCode环境搭建避坑指南
Mariadb root用户及普通用户的密码 重置
MMO project learning 1: preheating