com.jeesuite.common2.excel.ExcelTemplateWriter Maven / Gradle / Ivy
The newest version!
package com.jeesuite.common2.excel;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jeesuite.common2.excel.helper.ExcelBeanHelper;
public final class ExcelTemplateWriter implements Closeable {
private static final Logger LOG = LoggerFactory.getLogger(ExcelTemplateWriter.class);
private String templatePath;
private OutputStream outputStream;
private final SXSSFWorkbook workbook;
/**
* 构造方法,传入需要操作的excel文件路径
*
* @param outputPath 需要操作的excel文件的路径
* @throws IOException IO流异常
* @throws InvalidFormatException 非法的格式异常
*/
public ExcelTemplateWriter(String templatePath,String outputPath) throws IOException, InvalidFormatException {
this.templatePath = templatePath;
File file = new File(outputPath);
boolean exists = file.exists();
if(!exists)file.createNewFile();
outputStream = new FileOutputStream(file);
this.workbook = createWorkbook();
}
public ExcelTemplateWriter(String templatePath,OutputStream outputStream) throws InvalidFormatException, IOException {
super();
this.templatePath = templatePath;
this.outputStream = outputStream;
this.workbook = createWorkbook();
}
private SXSSFWorkbook createWorkbook() throws IOException, InvalidFormatException {
File templateFile = new File(templatePath);
if(!templateFile.exists())throw new FileNotFoundException("Template["+templatePath + "] not found");
SXSSFWorkbook workbook = new SXSSFWorkbook(new XSSFWorkbook(templateFile), 1000);
return workbook;
}
/**
* 将数据写入excel文件
*
* @param list 数据列表
* @param 泛型
* @return 写入结果
*/
public boolean write(List list, Class clazz) {
List