Excel--05--读Excel----添加新行数
Posted 高高for 循环
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Excel--05--读Excel----添加新行数相关的知识,希望对你有一定的参考价值。
需求
原Excel-----01.xlsx
模板 temp
具体需求:
1. 读以上Excel ,添加新的行数,并且自定义内容.
2. 没定义的 按模板默认来
代码
依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.0</version>
</dependency>
代码实现:
工厂模式 适配EXCEl 2003 ,2007
Workbook workbook = WorkbookFactory.create(InputStream);
package com.cy.io;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class Excel01 {
public static void parse(String srcPath, String dirPath) throws IOException {
Path srcFile = Paths.get(srcPath);
Path dirFile = Paths.get(dirPath);
Files.deleteIfExists(dirFile);
Files.copy(srcFile, dirFile, StandardCopyOption.COPY_ATTRIBUTES);
FileInputStream InputStream = new FileInputStream(new File(dirPath));
//工厂模式 适配EXCEl 2003 ,2007
Workbook workbook = WorkbookFactory.create(InputStream);
Sheet moveSheet = workbook.getSheet("电影");
Sheet musicSheet = workbook.getSheet("音乐");
Sheet tempSheet = workbook.getSheet("temp");
//获取最后一行
int last1 = moveSheet.getLastRowNum();
Row moveRow = moveSheet.createRow(last1 + 1);
int last2 = musicSheet.getLastRowNum();
Row musicRow = musicSheet.createRow(last2 + 1);
//模板1
Row tempRow0 = tempSheet.getRow(0);
//模板2
Row tempRow1 = tempSheet.getRow(1);
for (int K = 0; K < tempRow0.getPhysicalNumberOfCells(); K++) {
//设置样式,默认对比模板的样式
moveRow.setRowStyle(tempRow0.getRowStyle());
musicRow.setRowStyle(tempRow1.getRowStyle());
Cell tempCell0 = tempRow0.getCell(K);
Cell tempCell1 = tempRow1.getCell(K);
Cell moveCell1 = moveRow.createCell(K);
Cell musicCell1 = musicRow.createCell(K);
CellType cellType = tempCell0.getCellType();
//设置cell样式
moveCell1.setCellStyle(tempCell0.getCellStyle());
musicCell1.setCellStyle(tempCell1.getCellStyle());
switch (cellType) {
case STRING:
switch (K) {
case 0:
//自定义
moveCell1.setCellValue("慧慧");
//使用模板
musicCell1.setCellValue(tempCell1.toString());
break;
case 1:
moveCell1.setCellValue("电视剧");
musicCell1.setCellValue(tempCell1.toString());
break;
case 2:
moveCell1.setCellValue("29");
musicCell1.setCellValue(tempCell1.toString());
break;
case 3:
moveCell1.setCellValue("....");
musicCell1.setCellValue(tempCell1.toString());
break;
default:
//默认使用模板cell填充
moveCell1.setCellValue(tempCell1.toString());
musicCell1.setCellValue(tempCell1.toString());
break;
}
break;
case NUMERIC:
double numericCellValue = tempCell0.getNumericCellValue();
double numericCellValue1 = tempCell1.getNumericCellValue();
moveCell1.setCellValue(numericCellValue);
musicCell1.setCellValue(numericCellValue1);
break;
default:
break;
}
}
FileOutputStream outputStream = new FileOutputStream(dirPath);
workbook.write(outputStream);
workbook.close();
InputStream.close();
outputStream.close();
}
}
测试;
public static void main(String[] args) throws IOException {
String srcPath = "C:\\\\Users\\\\gaogao\\\\Desktop\\\\新建文件夹\\\\01.xlsx";
String dirPath = "C:\\\\Users\\\\gaogao\\\\Desktop\\\\新建文件夹\\\\02.xlsx";
parse(srcPath, dirPath);
}
以上是关于Excel--05--读Excel----添加新行数的主要内容,如果未能解决你的问题,请参考以下文章
通过 VBA excel 程序在 MS Word 标题中的边框线后添加页码和新行