天天看点

easypoi导出excel 效率_easypoi 快速开发 导出 各种姿势的excel

应用:

基本可以应付所有变态的Excel导出需求,各种姿势!!

Maven:

cn.afterturn

easypoi-base

3.0.1

cn.afterturn

easypoi-annotation

3.0.1

cn.afterturn

easypoi-web

3.0.1

模板:

easypoi导出excel 效率_easypoi 快速开发 导出 各种姿势的excel
easypoi导出excel 效率_easypoi 快速开发 导出 各种姿势的excel

导出:

// 查询数据,此处省略

List list = new ArrayList();

int count1 = 0 ;

EasyPOIModel easyPOIModel11 = new EasyPOIModel(count1++,"信科","张三","男",20) ;

EasyPOIModel easyPOIModel12 = new EasyPOIModel(count1++,"信科","李四","男",17) ;

list.add(easyPOIModel11) ;

list.add(easyPOIModel12) ;

// 获取导出excel指定模版

TemplateExportParams params = new TemplateExportParams();

// 标题开始行

params.setHeadingStartRow(0);

// 标题行数

params.setHeadingRows(2);

// 设置sheetName,若不设置该参数,则使用得原本得sheet名称

params.setSheetName("班级信息");

params.setHeadingRows(2);

params.setHeadingStartRow(2);

params.setTempParams("t");

Map data = new HashMap();

data.put("list", list);

book = ExcelUtil.getWorkbook(params, data, "1easypoiExample.xls");

//下载

ExcelUtil.export(response, workbook, "easypoi-excel.xls");

ExcelUtil:

import org.apache.poi.ss.usermodel.Workbook;

import org.springframework.core.io.Resource;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import org.springframework.core.io.support.ResourcePatternResolver;

import cn.afterturn.easypoi.excel.ExcelExportUtil;

import cn.afterturn.easypoi.excel.entity.TemplateExportParams;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletResponse;

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Map;

public class ExcelUtil {

private static final String TEMPLATE_PATH = "template/";

public static Workbook getWorkbook(TemplateExportParams params, Map data, String templateName) throws Exception {

String templatePath = TEMPLATE_PATH + templateName;

File file = getTemplateFile(templatePath);

params.setTemplateUrl(file.getAbsolutePath());

Workbook book = ExcelExportUtil.exportExcel(params, data);

if(file.exists()) {

file.delete();

}

return book;

}

public static void export(HttpServletResponse response, Workbook workbook, String fileName) throws Exception {

response.reset();

response.setContentType("application/x-msdownload");

fileName = fileName + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

response.setHeader("Content-disposition","attachment; filename="+new String(fileName.getBytes("gb2312"),"ISO-8859-1")+".xls");

ServletOutputStream outStream=null;

try{

outStream = response.getOutputStream();

workbook.write(outStream);

}finally{

workbook.close();

outStream.close();

}

}

public static File getTemplateFile(String templatePath) throws Exception {

File file = File.createTempFile("temp", null);

ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

Resource[] resources = resolver.getResources(templatePath);

if(resources.length == 1) {

InputStream inputStream = resources[0].getInputStream();

inputStreamToFile(inputStream, file);

}else {

System.out.println("请检查模板文件是否存在");

}

return file;

}

public static void inputStreamToFile(InputStream ins,File file) {

try {

OutputStream os = new FileOutputStream(file);

int bytesRead = 0;

byte[] buffer = new byte[8192];

while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {

os.write(buffer, 0, bytesRead);

}

os.close();

ins.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}