java基于反射机制动态导出excel

       项目中经常需要导出对象列表。每次都觉得每次手工写太麻烦了,今天基于java反射机制和泛型完成了对象的excel导出。可灵活配置表头。当然功能非常简陋还,会接着完善。

       包含四个文件:

       PrintConstant.java   常量信息

       ExcelSheet.java       excel实体类

       ExcelColumn.java   excel中的列

       JXLExcelView.java   生成excel的主要逻辑


      PrintConstant.java

package com.anansi.gamemis.print;

import java.util.ArrayList;

import com.anansi.gs.city.model.RewardCode;

public class PrintConstant {

	public enum ExcelEntity{
		
		@SuppressWarnings("unchecked")
		REWARD_CODE(1, "验证码.xls", "导出验", new ExcelSheet[]{
					new ExcelSheet(RewardCode.class, "验证码", "验证码", 1, null, new ExcelColumn[]{
						new ExcelColumn("period", "期数", 30, COLUMN_ALIGN.CENTER),
						new ExcelColumn("code", "验证码", 40, COLUMN_ALIGN.CENTER),
						new ExcelColumn("createAt", "创建时间", 30, COLUMN_ALIGN.CENTER),
						new ExcelColumn("status", "状态", 30, COLUMN_ALIGN.CENTER),
						new ExcelColumn("consumeAt", "消费时间", 30, COLUMN_ALIGN.CENTER),
						new ExcelColumn("playerId", "消费人", 30, COLUMN_ALIGN.CENTER),
					})
		});
		
		private final int id;
		private final String excelName;
		private final String note;
		private final ExcelSheet[] sheets;
		
		private ExcelEntity(int id, String excelName, String note, ExcelSheet[] sheets) {
			this.id = id;
			this.excelName = excelName;
			this.note = note;
			this.sheets = sheets;
		}
		
		public int getId() {
			return id;
		}

		public String getExcelName() {
			return excelName;
		}
		public String getNote() {
			return note;
		}
		public ExcelSheet[] getSheets() {
			return sheets;
		}
		
		public static ExcelEntity getById(int id){
			ExcelEntity entity = null;
			for (ExcelEntity element : ExcelEntity.values()) {
				if(element.getId() == id){
					entity = element;
					break;
				}
			}
			return entity;
		}
		
	}
	
	public enum COLUMN_ALIGN{
		LEFT, 
		CENTER,
		RIGHT
	}
}

ExcelSheet.java

package com.anansi.gamemis.print;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.record.formula.functions.T;

public class ExcelSheet<T> {

	/*
	 * sheet中要打印的实体的clazz类型
	 */
	private Class<T> clazz;
	
	/*
	 * sheet的主题
	 */
	private String subject;
	
	/*
	 * sheet的名称
	 */
	private String sheetName;
	
	/*
	 * 该sheet在excel中所处的页签
	 */
	private int sheetIndex;
	
	/*
	 * 所有的列
	 */
	private ExcelColumn[] columns;
	
	/*
	 * 数据
	 */
	private ArrayList<T> data;
	
	 ExcelSheet(Class<T> clazz, String subject, String sheetName, int sheetIndex, ArrayList<T> data,  ExcelColumn[] columns) {
		super();
		this.clazz = clazz;
		this.subject = subject;
		this.sheetName = sheetName;
		this.sheetIndex = sheetIndex;
		this.columns = columns;
		this.data = data;
	}

	public Class<T> getClazz() {
		return clazz;
	}

	public void setClazz(Class<T> clazz) {
		this.clazz = clazz;
	}

	public String getSheetName() {
		return sheetName;
	}

	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}

	public int getSheetIndex() {
		return sheetIndex;
	}

	public void setSheetIndex(int sheetIndex) {
		this.sheetIndex = sheetIndex;
	}

	public ExcelColumn[] getColumns() {
		return columns;
	}

	public void setColumns(ExcelColumn[] columns) {
		this.columns = columns;
	}

	public ArrayList<T> getData() {
		return data;
	}

	public void setData(ArrayList<T> data) {
		this.data = data;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}
	
}

 ExcelColumn.java 

package com.anansi.gamemis.print;

import com.anansi.gamemis.print.PrintConstant.COLUMN_ALIGN;

public class ExcelColumn {

	/*
	 * 实体属性域名称
	 */
	private String classFieldName;
	/*
	 * 这一列的title
	 */
	private String title;
	/*
	 * 宽度
	 */
	private int width;
	/*
	 * 对其方式
	 */
	private COLUMN_ALIGN align;
	
	ExcelColumn(String classFieldName, String title, int width, COLUMN_ALIGN align) {
		super();
		this.classFieldName = classFieldName;
		this.title = title;
		this.width = width;
		this.align = align;
	}
	
	public String getClassFieldName() {
		return classFieldName;
	}
	public void setClassFieldName(String classFieldName) {
		this.classFieldName = classFieldName;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public COLUMN_ALIGN getAlign() {
		return align;
	}
	public void setAlign(COLUMN_ALIGN align) {
		this.align = align;
	}
	
	
}

JXLExcelView.java

package com.anansi.gamemis.model;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Alignment;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.springframework.web.servlet.view.document.AbstractJExcelView;

import com.anansi.gamemis.exception.EnumNotFoundException;
import com.anansi.gamemis.exception.MisException;
import com.anansi.gamemis.print.ExcelSheet;
import com.anansi.gamemis.print.PrintConstant.ExcelEntity;

public class NewJXLExcelView extends AbstractJExcelView {

	private WritableCellFormat wcfFC;

	@SuppressWarnings("unchecked")
	@Override
	public void buildExcelDocument(Map<String, Object> map, WritableWorkbook work, HttpServletRequest req,
			HttpServletResponse response) {

		OutputStream os = null;
		WritableSheet ws = null;
		try {
			ExcelEntity entity = (ExcelEntity) map.get("excelEntity");
			if (entity == null) {
				throw new MisException("the excelEntity is null");
			}

			int startRow = 0;
			String excelName = entity.getExcelName();

			if (os == null) {
				// 设置response方式,使执行此controller时候自动出现下载页面,而非直接使用excel打开
				response.setContentType("APPLICATION/OCTET-STREAM");
				response.setHeader("Content-Disposition",
						"attachment; filename=" + URLEncoder.encode(excelName, "UTF-8"));
				os = response.getOutputStream();

				// 全局设置
				WorkbookSettings setting = new WorkbookSettings();
				java.util.Locale locale = new java.util.Locale("zh", "CN");
				setting.setLocale(locale);
				setting.setEncoding("ISO-8859-1");
				// 创建工作薄
				work = Workbook.createWorkbook(os); // 建立excel文件
			}

			for (ExcelSheet sheet : entity.getSheets()) {
				if (sheet != null) {
					String sheetName = sheet.getSheetName();
					ws = work.createSheet(sheetName, sheet.getSheetIndex()); // sheet名称

					// 添加标题
					addColumNameToWsheet(ws, startRow, sheet);
					startRow = fullTplWsheet(sheet, ws, startRow);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			// 写入文件
			try {
				work.write();
				work.close();
				os.flush();
				os.close();
			} catch (WriteException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

	}

	public <T> int fullTplWsheet(ExcelSheet sheet, WritableSheet wsheet, int startRow) throws RowsExceededException,
			WriteException, EnumNotFoundException, SecurityException, IllegalArgumentException, InstantiationException,
			IllegalAccessException, NoSuchMethodException, InvocationTargetException {
		int row = startRow;
		int index = 0;
		ArrayList<T> list = sheet.getData();
		wsheet.mergeCells(0, row, 5, 0); // 为title,合并单元格
		for (; row < startRow + list.size(); row++, index++) {
			T object = list.get(index);
			int column = 0;
			wsheet.addCell(new jxl.write.Label(column, (row + 2), String.valueOf(index + 1), wcfFC));
			fillContent(object, sheet, wsheet, column, row);
		}

		return row + 10;
	}

	/**
	 * 添加标题样式
	 * 
	 * @param wsheet
	 * @param startRow
	 *            开始写标题所处的行数
	 * @throws RowsExceededException
	 * @throws WriteException
	 */
	private void addColumNameToWsheet(jxl.write.WritableSheet wsheet, int startRow, ExcelSheet sheet)
			throws RowsExceededException, WriteException {

		// head样式
		WritableCellFormat format = getFormat(getFont());
		format.setBackground(jxl.format.Colour.ORANGE);

		// 内容样式
		WritableFont contentFont = getContentFont();
		wcfFC = getFormat(contentFont);

		// title样式
		WritableCellFormat titleFormat = getTitleFormat(getTitleFont());

		int colSize = sheet.getColumns().length;

		// title
		wsheet.addCell(new jxl.write.Label(0, startRow, sheet.getSubject(), titleFormat));

		String colName = null;
		int witdh = 0;

		// 加入编号列
		fullSubject(wsheet, 0, "编号", 20, startRow, format);

		for (int i = 1; i <= colSize; i++) {
			colName = sheet.getColumns()[i - 1].getTitle();
			witdh = sheet.getColumns()[i - 1].getWidth();
			fullSubject(wsheet, i, colName, witdh, startRow, format);
		}

	}

	/**
	 * 填充标题栏
	 */
	private void fullSubject(WritableSheet wsheet, int index, String colName, int witdh, int startRow,
			WritableCellFormat format) throws RowsExceededException, WriteException {
		if (null == colName || "".equals(colName))
			colName = "";
		Label wlabel1 = new Label(index, startRow + 1, colName, format);
		wsheet.addCell(wlabel1);
		// 默认设置列宽
		witdh = witdh == 0 ? 20 : witdh;
		wsheet.setColumnView(index, witdh);
	}

	// 设置title格式
	private WritableCellFormat getTitleFormat(WritableFont wfont) {

		WritableCellFormat wcfTitle = new WritableCellFormat(wfont);
		try {
			wcfTitle.setBackground(jxl.format.Colour.BLACK); // 设置单元格的背景颜色
			wcfTitle.setAlignment(jxl.format.Alignment.LEFT); // 设置对齐方式
		} catch (WriteException e) {
			e.printStackTrace();
		}
		return wcfTitle;
	}

	// 设置格式
	private WritableCellFormat getFormat(WritableFont wfont) {

		WritableCellFormat wcfFC = new WritableCellFormat(wfont);
		try {
			wcfFC.setWrap(true);
			wcfFC.setAlignment(Alignment.CENTRE);
			wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);
		} catch (WriteException e) {
			e.printStackTrace();
		}
		return wcfFC;
	}

	// 设置head字体
	private WritableFont getTitleFont() {
		return new WritableFont(WritableFont.ARIAL, 20, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
				jxl.format.Colour.BLACK);
	}

	// 设置head字体
	private WritableFont getFont() {
		return new WritableFont(WritableFont.ARIAL, WritableFont.DEFAULT_POINT_SIZE, WritableFont.BOLD);
	}

	// 设置content字体
	private WritableFont getContentFont() {
		return new WritableFont(WritableFont.ARIAL, WritableFont.DEFAULT_POINT_SIZE);
	}

	public <T> void fillContent(T instance, ExcelSheet sheet, WritableSheet wsheet, int column, int row)
			throws RowsExceededException, WriteException, InstantiationException, IllegalAccessException,
			SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {

		// Object invokeTest = type.newInstance();
		for (int k = 0; k < sheet.getColumns().length; k++) { // 用前台传递的每一个dataIndex字段去和类的属性比较
			String data = sheet.getColumns()[k].getClassFieldName(); // 得到每一个字段值,如orgId,orgName……

			// 第一个参数为getXXX datasubString(0,1)
			// 截取第一个字母,如orgId截取o再otUpperCase变成大写
			Method method = sheet.getClazz().getMethod(
					"get" + data.substring(0, 1).toUpperCase() + data.substring(1, data.length()), new Class[] {});
			Object result = method.invoke(instance, new Object[] {});

			wsheet.addCell(new jxl.write.Label(++column, (row + 2), result + "", wcfFC));
		}

	}

}

代码有点乱,很高兴你能看到这里。哈哈

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java注解是一种元数据,它可以为类、方法、字段等元素添加额外的信息。在Java中,可以使用自定义注解和反射来实现导入导出Excel文档。 首先,定义一个自定义注解,用于标记需要导出的实体类的字段: ```java @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface ExcelField { /** * 列名 */ public String name(); /** * 顺序 */ public int order(); } ``` 然后,在实体类的字段上添加该注解: ```java public class User { @ExcelField(name = "姓名", order = 1) private String name; @ExcelField(name = "年龄", order = 2) private int age; // 省略其他字段和方法 } ``` 接着,定义一个工具类,用于读取和写入Excel文档: ```java public class ExcelUtil { /** * 从Excel中读取数据 */ public static <T> List<T> readFromExcel(InputStream is, Class<T> clazz) { List<T> list = new ArrayList<>(); try { Workbook workbook = WorkbookFactory.create(is); Sheet sheet = workbook.getSheetAt(0); Map<Integer, String> headers = getHeaders(sheet.getRow(0)); for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); T obj = clazz.newInstance(); for (int j = 0; j < row.getLastCellNum(); j++) { Cell cell = row.getCell(j); String value = getValue(cell); String fieldName = headers.get(j); Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); setValue(obj, field, value); } list.add(obj); } } catch (Exception e) { e.printStackTrace(); } return list; } /** * 写入数据到Excel中 */ public static <T> void writeToExcel(List<T> list, OutputStream os) { try { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet(); Row header = sheet.createRow(0); Map<String, Integer> fields = getFields(list.get(0).getClass()); List<String> fieldNames = new ArrayList<>(fields.keySet()); Collections.sort(fieldNames); for (int i = 0; i < fieldNames.size(); i++) { String fieldName = fieldNames.get(i); Cell cell = header.createCell(i); cell.setCellValue(fields.get(fieldName)); } for (int i = 0; i < list.size(); i++) { Row row = sheet.createRow(i + 1); T obj = list.get(i); for (int j = 0; j < fieldNames.size(); j++) { String fieldName = fieldNames.get(j); Field field = obj.getClass().getDeclaredField(fieldName); field.setAccessible(true); Object value = field.get(obj); Cell cell = row.createCell(j); cell.setCellValue(value.toString()); } } workbook.write(os); } catch (Exception e) { e.printStackTrace(); } } /** * 获取Excel中的列名 */ private static Map<Integer, String> getHeaders(Row row) { Map<Integer, String> headers = new HashMap<>(); for (int i = 0; i < row.getLastCellNum(); i++) { Cell cell = row.getCell(i); String value = getValue(cell); headers.put(i, value); } return headers; } /** * 获取实体类中的字段名和顺序 */ private static <T> Map<String, Integer> getFields(Class<T> clazz) { Map<String, Integer> fields = new HashMap<>(); Field[] declaredFields = clazz.getDeclaredFields(); for (Field field : declaredFields) { if (field.isAnnotationPresent(ExcelField.class)) { ExcelField excelField = field.getAnnotation(ExcelField.class); fields.put(field.getName(), excelField.order()); } } return fields; } /** * 设置实体类中的字段值 */ private static <T> void setValue(T obj, Field field, String value) throws Exception { String typeName = field.getType().getName(); if ("int".equals(typeName)) { field.set(obj, Integer.parseInt(value)); } else if ("java.lang.String".equals(typeName)) { field.set(obj, value); } // 省略其他类型的判断 } /** * 获取单元格中的值 */ private static String getValue(Cell cell) { String value = ""; if (cell != null) { switch (cell.getCellType()) { case STRING: value = cell.getStringCellValue(); break; case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); value = new SimpleDateFormat("yyyy-MM-dd").format(date); } else { value = String.valueOf(cell.getNumericCellValue()); } break; case BOOLEAN: value = String.valueOf(cell.getBooleanCellValue()); break; case FORMULA: value = String.valueOf(cell.getCellFormula()); break; default: value = ""; } } return value; } } ``` 最后,可以使用该工具类来读取和写入Excel文档: ```java public class Main { public static void main(String[] args) { // 从Excel中读取数据 try (InputStream is = new FileInputStream("users.xlsx")) { List<User> list = ExcelUtil.readFromExcel(is, User.class); for (User user : list) { System.out.println(user.getName() + ", " + user.getAge()); } } catch (Exception e) { e.printStackTrace(); } // 写入数据到Excel中 List<User> list = new ArrayList<>(); list.add(new User("张三", 20)); list.add(new User("李四", 30)); list.add(new User("王五", 40)); try (OutputStream os = new FileOutputStream("users.xlsx")) { ExcelUtil.writeToExcel(list, os); } catch (Exception e) { e.printStackTrace(); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值