当前位置:网站首页>poi追加写EXCEL文件
poi追加写EXCEL文件
2022-07-06 08:31:00 【renkai721】
需求场景描述:
我们有时候需要把用户上传的一个完整的EXCEL拆分为多个不同的EXCEL,然后发给不同部门的人处理完之后,再把这些EXCEL合并到一个EXCEL文件中。
下面的这个文章地址是EXCEL2003的处理,如果是2007版本以上的需要自己手动修改HSSF为XSSF就可以了。
下面是poi处理的代码片段
1、pom文件中增加poi依赖
<!-- poi start -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
2、追加写EXCEL的核心代码。
public void appendExcel(String saveOrderLocalPath, int beginRow, List<SubmitYhdDecomposeExcel> list) throws IOException {
FileOutputStream out = null;
log.info("saveOrderLocalPath={},beginRow={}",saveOrderLocalPath,beginRow);
FileInputStream fileInputStream = new FileInputStream(saveOrderLocalPath);
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFCellStyle cellStyle = workbook.createCellStyle();
// -----------------追加数据-------------------
if(beginRow > 1){
// 从哪一行开始写,注意这里传递过来的beginRow是EXCEL真实的第几行,行数
// 代码中需要判断大于1行的时候需要下标减1,程序写入是从0开始的。
// 由于有表头,所以beginRow=1的时候这里不减1
beginRow -= 1;
}
for (int i = 0; i < list.size(); i++) {
SubmitYhdDecomposeExcel rowObj = list.get(i);
XSSFRow startRow = sheet.createRow(i + beginRow);
for (int col = 0; col < 18; col++) {
XSSFCell cell = startRow.createCell(col);
switch (col) {
case 0:
cell.setCellValue(rowObj.getCus_no());
break;
case 1:
cell.setCellValue(rowObj.getType());
break;
case 2:
cell.setCellValue(rowObj.getNum());
break;
case 3:
cell.setCellValue(rowObj.getPrice_input());
break;
case 4:
cell.setCellValue(rowObj.getNormal_type());
break;
case 5:
cell.setCellValue(rowObj.getBak());
break;
case 6:
cell.setCellValue(rowObj.getUnfit());
break;
case 7:
cell.setCellValue(rowObj.getPack());
break;
case 8:
cell.setCellValue(rowObj.getOne_price());
break;
case 9:
cell.setCellValue(rowObj.getLow_price());
break;
case 10:
cell.setCellValue(rowObj.getDate());
break;
case 11:
cell.setCellValue(rowObj.getCheap_type());
break;
case 12:
cell.setCellValue(rowObj.getCheap_bak());
break;
case 13:
cell.setCellValue(rowObj.getCheap_unfit());
break;
case 14:
cell.setCellValue(rowObj.getCheap_pack());
break;
case 15:
cell.setCellValue(rowObj.getCheap_one_price());
break;
case 16:
cell.setCellValue(rowObj.getCheap_low_price());
break;
case 17:
cell.setCellValue(rowObj.getCheap_date());
break;
default:
break;
}
cell.setCellStyle(cellStyle);
}
}
// 输出Excel文件
out = new FileOutputStream(saveOrderLocalPath);
workbook.write(out);
out.flush();
out.close();
fileInputStream.close();
log.info("追加写入EXCEL完成。");
}
边栏推荐
- MySQL learning record 10getting started with JDBC
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform
- logback1.3. X configuration details and Practice
- 【MySQL】鎖
- 优秀的软件测试人员,都具备这些能力
- 个人电脑好用必备软件(使用过)
- 2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
- LDAP应用篇(4)Jenkins接入
- Hungry for 4 years + Ali for 2 years: some conclusions and Thoughts on the road of research and development
猜你喜欢
Configuring OSPF load sharing for Huawei devices
What is CSRF (Cross Site Request Forgery)?
2022 Inner Mongolia latest construction tower crane (construction special operation) simulation examination question bank and answers
C language double pointer -- classic question type
ROS编译 调用第三方动态库(xxx.so)
2. File operation - write
Leetcode question brushing (5.28) hash table
Wincc7.5 download and installation tutorial (win10 system)
C language custom type: struct
Synchronized solves problems caused by sharing
随机推荐
Colorlog结合logging打印有颜色的日志
[2022 广东省赛M] 拉格朗日插值 (多元函数极值 分治NTT)
LDAP應用篇(4)Jenkins接入
[MySQL] log
JS native implementation shuttle box
synchronized 解决共享带来的问题
leetcode刷题 (5.31) 字符串
2022.02.13 - NC003. Design LRU cache structure
2. File operation - write
根据csv文件某一列字符串中某个数字排序
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
Precise query of tree tree
Roguelike游戏成破解重灾区,如何破局?
MySQL learning record 10getting started with JDBC
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Yyds dry goods inventory three JS source code interpretation eventdispatcher
Wincc7.5 download and installation tutorial (win10 system)
堆排序详解
Tidb backup and recovery introduction
tree树的精准查询