写一个java 读取excel模板并且复制其中一行到新加行的示例
时间: 2023-04-02 10:01:15 浏览: 48
您好,我是C知道,我可以回答您的问题。以下是一个Java读取Excel模板并复制其中一行到新行的示例代码:
```
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelTemplateCopy {
public static void main(String[] args) throws IOException {
// 读取Excel模板
FileInputStream inputStream = new FileInputStream(new File("template.xlsx"));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt();
// 复制第2行到第3行
Row sourceRow = sheet.getRow(1);
Row newRow = sheet.createRow(2);
for (int i = ; i < sourceRow.getLastCellNum(); i++) {
Cell oldCell = sourceRow.getCell(i);
Cell newCell = newRow.createCell(i);
newCell.setCellValue(oldCell.getStringCellValue());
}
// 写入新Excel文件
FileOutputStream outputStream = new FileOutputStream(new File("output.xlsx"));
workbook.write(outputStream);
workbook.close();
}
}
```
希望这个示例能够帮助您解决问题。