复制excel表,往excel表中写入数据

时间:2022-08-24 16:26:45

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//复制excel文件

public void copy(File sourceFile, File targetFile) {
if (targetFile.exists()) {
targetFile.delete();
}
int len = (int) sourceFile.length();
byte[] data = new byte[len];
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream(sourceFile);
output = new FileOutputStream(targetFile);
input.read(data);
output.write(data);
} catch (Exception ex) {
log.error("Cannot init student template {}", targetFile, ex);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
}
}
if (output != null) {
try {
output.close();
} catch (IOException e) {
}
}
}
}

//向excel表中写入数据

public void writeExcel(List<Map> templateList, int m, String path) {
OutputStream out = null;
try {
// 获取总列数
int columnNumCount = m;
// 读取Excel文档
File finalXlsxFile = new File(path);
Workbook workBook = getWorkbok(finalXlsxFile);
// sheet 对应一个工作页
Sheet sheet = workBook.getSheetAt(0);
Sheet sheet1 = workBook.getSheetAt(1);
/**
* 删除原有数据,除了属性列
*/
int rowNumber = sheet.getLastRowNum(); // 第一行从0开始算
System.out.println("原始数据总行数,除属性列:" + rowNumber);
for (int i = 1; i <= rowNumber; i++) {
Row row = sheet.getRow(i);
sheet.removeRow(row);
}
/**
* 往Excel中写新数据
*/
for (int j = 0; j < templateList.size(); j++) {
// 创建一行:从第二行开始,跳过属性列
Row row = sheet.createRow(j + 1);
Row row1 = null;
// 得到要插入的每一条记录
Map dataMap = templateList.get(j);
String name = dataMap.get("name").toString();
String subjectName = null;
if(dataMap.containsKey("subjectName")){
subjectName = dataMap.get("subjectName").toString();
row1 = sheet1.createRow(j + 1);
}
for (int k = 0; k <= columnNumCount; k++) {
// 在一行内循环(列)
Cell first = row.createCell(0);
first.setCellValue(name);
if(subjectName != null){
Cell secend = row1.createCell(0);
secend.setCellValue(subjectName);
}
}
}
// 创建文件输出流,准备输出电子表格:这个必须有,否则你在sheet上做的任何操作都不会有效
out = new FileOutputStream(finalXlsxFile.getAbsolutePath());
workBook.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("数据导出成功");
}

/**
* 判断Excel的版本,获取Workbook
*
* @param in
* @param filename
* @return
* @throws IOException
*/
public static Workbook getWorkbok(File file) throws IOException {
Workbook wb = null;
FileInputStream in = new FileInputStream(file);
if (file.getName().endsWith(EXCEL_XLS)) { // Excel 2003
wb = new HSSFWorkbook(in);
} else if (file.getName().endsWith(EXCEL_XLSX)) { // Excel 2007/2010
wb = new XSSFWorkbook(in);
}
return wb;
}