当前位置:网站首页>通过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都是通过追加的方式添加的)
边栏推荐
- Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
- Android interview, Android audio and video development
- HiEngine:可媲美本地的云原生内存数据库引擎
- Applet modification style (placeholder, checkbox style)
- uniapp获取微信头像和昵称
- Reflection and imagination on the notation like tool
- The road of enterprise digital transformation starts from here
- 全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
- How to realize the Online timer and offline timer in the game
- UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
猜你喜欢
The road of enterprise digital transformation starts from here
Password reset of MariaDB root user and ordinary user
JAD的安装、配置及集成IDEA
Tianyi cloud understands enterprise level data security in this way
Go deep into the underlying C source code and explain the core design principles of redis
MMO project learning 1: preheating
C# 语言的高级应用
Blue sky drawing bed Apple quick instructions
The basic grammatical structure of C language
The problem of returning the longtext field in MySQL and its solution
随机推荐
JAD installation, configuration and integration idea
vagrant2.2.6支持virtualbox6.1版本
Password reset of MariaDB root user and ordinary user
After the company went bankrupt, the blackstones came
Tupu software digital twin smart wind power system
Mathematical modeling of oil pipeline layout MATLAB, mathematical model of oil pipeline layout
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
JS solution force deduction daily question (12) - 556 Next larger element III (2022-7-3)
#夏日挑战赛# HarmonyOS - 实现消息通知功能
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
在线协作产品哪家强?微软 Loop 、Notion、FlowUs
Hiengine: comparable to the local cloud native memory database engine
[AI framework basic technology] automatic derivation mechanism (autograd)
测试的核心价值到底是什么?
2022 the most complete Tencent background automation testing and continuous deployment practice in the whole network [10000 words]
微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知
MMO project learning 1: preheating
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
Cf:b. almost Terry matrix [symmetry + finding rules + structure + I am structural garbage]