1. 程式人生 > >easypoi 快速開發 匯出 各種姿勢的excel

easypoi 快速開發 匯出 各種姿勢的excel

應用:

基本可以應付所有變態的Excel匯出需求,各種姿勢!!

Maven:

<!-- easyPoi --> <dependency>   <groupId>cn.afterturn</groupId>   <artifactId>easypoi-base</artifactId>   <version>3.0.1</version> </dependency> <dependency>   <groupId>cn.afterturn</groupId>   <artifactId>easypoi-annotation</artifactId>   <version>3.0.1</version> </dependency> <dependency>   <groupId>cn.afterturn</groupId>   <artifactId>easypoi-web</artifactId>   <version>3.0.1</version> </dependency>

模板:

匯出:

// 查詢資料,此處省略 List<EasyPOIModel> list = new ArrayList<EasyPOIModel>(); 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<String,Object> data = new HashMap<String, Object>(); 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/";

/** * 生成excel物件 * @param params 模板匯出引數設定 * @param data 模板匯出資料 * @param templateName 模板名稱 * @return workBook物件 * @throws Exception 異常丟擲 */ public static Workbook getWorkbook(TemplateExportParams params, Map<String, Object> 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; }

/** * 匯出excel物件 * @param response httpResponse物件 * @param workbook workBook物件 * @param fileName 匯出檔名 * @throws Exception 異常丟擲 */ 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(); } }

/** * 獲取模板檔案--獲取到的檔案為臨時檔案,用完後需要手動刪除 * <p>由於springboot打包成jar之後,不能以絕對路徑的形式讀取模板檔案,故此處將模板檔案以臨時檔案的形式寫到磁碟中,用完請手動刪除</p> * @param templatePath 模板路徑 * @return 模板檔案 * @throws Exception 異常丟擲 */ 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; }

/** * InputStream 轉file * @param ins 輸入流 * @param 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(); } } }