1. 程式人生 > 實用技巧 >java指定順序通過對映匯出excel

java指定順序通過對映匯出excel

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExcelAttribute {
    /** 對應的列名稱 */
    String name() default
""; /** 列序號 */ int sort(); /** 欄位型別對應的格式 */ String format() default ""; }

匯出的類

package com.ihrm.common.poi.utils;

import com.ihrm.domain.poi.ExcelAttribute;
import lombok.Getter;
import lombok.Setter;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.functions.T; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field;
import java.net.URLEncoder; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; @Getter @Setter public class ExcelExportUtil<T> { private int rowIndex; private int styleIndex; private String templatePath; private Class clazz; private Field fields[]; public ExcelExportUtil(Class clazz,int rowIndex,int styleIndex) { this.clazz = clazz; this.rowIndex = rowIndex; this.styleIndex = styleIndex; fields = clazz.getDeclaredFields(); } /** * 基於註解匯出 */ public void export(HttpServletResponse response,InputStream is, List<T> objs,String fileName) throws Exception { XSSFWorkbook workbook = new XSSFWorkbook(is); Sheet sheet = workbook.getSheetAt(0); CellStyle[] styles = getTemplateStyles(sheet.getRow(styleIndex)); AtomicInteger datasAi = new AtomicInteger(rowIndex); for (T t : objs) { Row row = sheet.createRow(datasAi.getAndIncrement()); for(int i=0;i<styles.length;i++) { Cell cell = row.createCell(i); cell.setCellStyle(styles[i]); for (Field field : fields) { if(field.isAnnotationPresent(ExcelAttribute.class)){ field.setAccessible(true); ExcelAttribute ea = field.getAnnotation(ExcelAttribute.class); if(i == ea.sort()) { cell.setCellValue(field.get(t).toString()); } } } } } fileName = URLEncoder.encode(fileName, "UTF-8"); response.setContentType("application/octet-stream"); response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("ISO8859-1"))); response.setHeader("filename", fileName); workbook.write(response.getOutputStream()); } public CellStyle[] getTemplateStyles(Row row) { CellStyle [] styles = new CellStyle[row.getLastCellNum()]; for(int i=0;i<row.getLastCellNum();i++) { styles[i] = row.getCell(i).getCellStyle(); } return styles; } }

來源於傳智播客