Alibaba EasyExcel导出【工具类】

功能简介

支持单页Sheet、多页Sheet、自定义Head表头,以最简入参的方式封装,生成Excel。
简洁易用、依赖少

Api文档


环境

JDK-8
依赖第三方Jar包:com.alibaba.easyexcel,3.0版本及以上

Maven

pom.xml

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>easyexcel</artifactId>
	<version>3.2.0</version>
</dependency>

Class

Excel.java

package com.qoo.util;

import static java.util.Optional.ofNullable;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Description;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.converters.bigdecimal.BigDecimalStringConverter;
import com.alibaba.excel.converters.date.DateStringConverter;
import com.alibaba.excel.converters.localdatetime.LocalDateTimeStringConverter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;

/**
 * Excel
 * @author Haining.Liu
 * @date 2022-09-23 17:15:23
 */
public class Excel {
	/**
	 * {@link java.math.BigDecimal}转换器
	 */
	public static final BigDecimalStringConverter DECIMAL_CONVERTER = new BigDecimalStringConverter();
	/**
	 * {@link java.util.Date}转换器
	 */
	public static final DateStringConverter DATE_CONVERTER = new DateStringConverter();
	/**
	 * {@link java.time.LocalDateTime}转换器
	 */
	public static final LocalDateTimeStringConverter LOCAL_DATE_TIME_CONVERTER = new LocalDateTimeStringConverter();

	/**
	 * 导出 - 单页Sheet
	 * @see {@link #write(HttpServletResponse, List)}
	 */
	public static <T> void output(HttpServletResponse res, List<T> data) throws IOException {
		write(res, data).finish();
	}
	/**
	 * 导出 - 单页Sheet
	 * @see {@link #write(HttpServletResponse, List, String)}
	 */
	public static <T> void output(HttpServletResponse res, List<T> data, String name) throws IOException {
		write(res, data, name).finish();
	}
	/**
	 * 导出 - 单页Sheet
	 * @see {@link #write(OutputStream, List)}
	 */
	public static <T> void output(OutputStream out, List<T> data) {
		write(out, data).finish();
	}
	/**
	 * 导出 - 单页Sheet
	 * @see {@link #write(OutputStream, List, String)}
	 */
	public static <T> void output(OutputStream out, List<T> data, String name) {
		write(out, data, name).finish();
	}
	/**
	 * 导出 - 单页Sheet
	 * @param res {@link HttpServletResponse}
	 * @param data {@link List}<{@link T}>:为空时,无法获取表头;
	 * @return {@link ExcelWriter}
	 * @author Haining.Liu
	 * @throws IOException
	 * @date 2022-09-23 17:15:23
	 */
	public static <T> ExcelWriter write(HttpServletResponse res, List<T> data) throws IOException {
		Collection<List<?>> datas = Collections.singletonList(data);
		return write(res, datas);
	}
	/**
	 * 导出 - 单页Sheet
	 * @param res {@link HttpServletResponse}
	 * @param data {@link List}<{@link T}>
	 * @param name {@link String}:名称;
	 * @return {@link ExcelWriter}
	 * @author Haining.Liu
	 * @throws IOException
	 * @date 2022-09-23 17:15:23
	 */
	public static <T> ExcelWriter write(HttpServletResponse res, List<T> data, String name) throws IOException {
		LinkedHashMap<String, List<?>> m = new LinkedHashMap<>();
		m.put(name, data);
		return write(res, m, name);
	}
	/**
	 * 导出 - 单页Sheet
	 * @param out {@link OutputStream}
	 * @param data {@link List}<{@link T}>:为空时,无法获取表头;
	 * @return {@link ExcelWriter}
	 * @author Haining.Liu
	 * @date 2022-09-23 17:15:23
	 */
	public static <T> ExcelWriter write(OutputStream out, List<T> data) {
		Collection<List<?>> datas = Collections.singletonList(data);
		return write(out, datas);
	}
	/**
	 * 导出 - 单页Sheet
	 * @param out {@link OutputStream}
	 * @param data {@link List}<{@link T}>
	 * @param name {@link String}:名称;
	 * @return {@link ExcelWriter}
	 * @author Haining.Liu
	 * @date 2022-09-23 17:15:23
	 */
	public static <T> ExcelWriter write(OutputStream out, List<T> data, String name) {
		LinkedHashMap<String, List<?>> m = new LinkedHashMap<>();
		m.put(name, data);
		return write(out, m);
	}

	/**
	 * 导出
	 * @see {@link #write(HttpServletResponse, Collection)}
	 */
	public static void output(HttpServletResponse res, Collection<List<?>> datas) throws IOException {
		write(res, datas).finish();
	}
	/**
	 * 导出
	 * @see {@link #write(HttpServletResponse, Collection, String)}
	 */
	public static void output(HttpServletResponse res, Collection<List<?>> datas, String fnm) throws IOException {
		write(res, datas, fnm).finish();
	}
	/**
	 * 导出
	 * @see {@link #write(HttpServletResponse, LinkedHashMap)}
	 */
	public static void output(HttpServletResponse res, LinkedHashMap<String, List<?>> datas) throws IOException {
		write(res, datas).finish();
	}
	/**
	 * 导出
	 * @see {@link #write(HttpServletResponse, LinkedHashMap, String)}
	 */
	public static void output(HttpServletResponse res, LinkedHashMap<String, List<?>> datas, String fnm) throws IOException {
		write(res, datas, fnm).finish();
	}
	/**
	 * 导出
	 * @see {@link #write(OutputStream, LinkedHashMap)}
	 */
	public static void output(OutputStream out, LinkedHashMap<String, List<?>> datas) {
		write(out, datas).finish();
	}
	/**
	 * 生成 Excel 内存文件
	 * @see {@link #write(HttpServletResponse, LinkedHashMap, String)}
	 */
	public static ExcelWriter write(HttpServletResponse res, Collection<List<?>> datas) throws IOException {
		return write(res, sheetData(datas));
	}
	/**
	 * 生成 Excel 内存文件
	 * @param res {@link HttpServletResponse}
	 * @param datas {@link List}<{@link List}<?>>:Sheet数据集。为空时,无法获取表头;
	 * @param fnm {@link String}:文件名;
	 * @return {@link ExcelWriter}
	 * @author Haining.Liu
	 * @date 2023-8-8 18:53:36
	 */
	public static ExcelWriter write(HttpServletResponse res, Collection<List<?>> datas, String fnm) throws IOException {
		return write(res, sheetData(datas), fnm);
	}
	/**
	 * 生成 Excel 内存文件
	 * @see {@link #write(HttpServletResponse, LinkedHashMap, String)}
	 */
	public static ExcelWriter write(HttpServletResponse res, LinkedHashMap<String, List<?>> datas) throws IOException {
		String fnm = null;
		if (datas != null && !datas.isEmpty()) {
			Entry<String, List<?>> nx = datas.entrySet().iterator().next();
			fnm = nx.getKey() + '-' + suffix();
		}
		return write(res, datas, fnm);
	}
	/**
	 * 生成 Excel 内存文件
	 * @param res {@link HttpServletResponse}
	 * @param datas {@link LinkedHashMap}<{@link String}, {@link List}<?>>:k - Sheet名,v - 数据集。为空时,无法获取表头;
	 * @param fnm {@link String}:文件名;
	 * @return {@link ExcelWriter}
	 * @author Haining.Liu
	 * @date 2023-8-8 18:53:36
	 */
	public static ExcelWriter write(HttpServletResponse res, LinkedHashMap<String, List<?>> datas, String fnm) throws IOException {
		return write(resHead(res, fnm).getOutputStream(), datas);
	}
	/**
	 * 生成 Excel 内存文件
	 * @see {@link #write(OutputStream, LinkedHashMap)}
	 */
	public static ExcelWriter write(OutputStream out, Collection<List<?>> datas) {
		return write(out, sheetData(datas));
	}
	/**
	 * 生成 Excel 内存文件
	 * @param out {@link OutputStream}
	 * @param datas {@link LinkedHashMap}<{@link String}, {@link List}<?>>:k - Sheet名,v - 数据集。为空时,无法获取表头;
	 * @return {@link ExcelWriter}
	 * @author Haining.Liu
	 * @date 2023-8-8 18:53:36
	 */
	public static ExcelWriter write(OutputStream out, LinkedHashMap<String, List<?>> datas) {
		ExcelWriter ew = writer(out);
		if (datas == null || datas.isEmpty()) {
			return ew;
		}
		Iterator<Entry<String, List<?>>> it = datas.entrySet().iterator();
		for (int i = 0; it.hasNext(); i++) {
			Entry<String, List<?>> nx = it.next();
			String k = nx.getKey();
			List<?> v = nx.getValue();

			ExcelWriterSheetBuilder wsb = create(ew, v, k, i);
			WriteSheet ws = wsb.build();
			if (ws.getHead() != null) {	// 指定Head时,剔除第一行表头
				v = v.stream().skip(1).collect(Collectors.toList());
			}
			ew.write(v, ws);
		}
		return ew;
	}
	/**
	 * 创建Sheet
	 * @param ew {@link ExcelWriter}
	 * @param c {@link Class}<{@link T}>:Head类;
	 * @param sheetNm {@link String}:名称;
	 * @param sheetNo {@link Integer}:顺序号;
	 * @return {@link ExcelWriterSheetBuilder}
	 * @version V1.0.1
	 * @author Haining.Liu
	 * @date 2023-8-8 14:34:58
	 */
	public static <T> ExcelWriterSheetBuilder create(ExcelWriter ew, Class<T> c, String sheetNm, Integer sheetNo) {
		ExcelWriterSheetBuilder ws = create(ew, sheetNm);
		ws.sheetNo(sheetNo).head(c);
		return ws;
	}
	/**
	 * 创建Sheet
	 * @param ew {@link ExcelWriter}
	 * @param sheetNm {@link String}:名称;
	 * @return {@link ExcelWriterSheetBuilder}
	 * @version V1.0.1
	 * @author Haining.Liu
	 * @date 2023-8-8 14:34:58
	 */
	public static ExcelWriterSheetBuilder create(ExcelWriter ew, String sheetNm) {
		ExcelWriterSheetBuilder ws = new ExcelWriterSheetBuilder(ew);
		return ws.sheetName(sheetNm);
	}
	/**
	 * 创建Sheet
	 * @param ew {@link ExcelWriter}
	 * @param data {@link List}<?>:Head类;
	 * @param sheetNm {@link String}:名称;
	 * @param sheetNo {@link Integer}:顺序号;
	 * @return {@link ExcelWriterSheetBuilder}
	 * @version V1.0.1
	 * @author Haining.Liu
	 * @date 2023-8-8 14:34:58
	 */
	private static ExcelWriterSheetBuilder create(ExcelWriter ew, List<?> data, String sheetNm, Integer sheetNo) {
		Class<?> c = classify(data);
		ExcelWriterSheetBuilder ws = create(ew, sheetNm == null || sheetNm.trim().isEmpty() ? sheetName(c, sheetNo + 1) : sheetNm);	// org.apache.commons.lang3.StringUtils.isBlank(sheetNm)
		if (!List.class.isAssignableFrom(c)) {
			ws.head(c);
		} else {	// 转List<List<String>>
			ofNullable(data)
					.map(v -> v.get(0))
					.map(v -> (List<?>) v)
					.map(v -> v.stream()
							.map(row -> {
								if (row instanceof Collection) {
									return (Collection<?>) row;
								} else if (row instanceof Iterable) {
									Iterable<?> it = (Iterable<?>) row;
									return StreamSupport.stream(it.spliterator(), false).collect(Collectors.toList());
								} else if (row == null) {
									return Collections.emptyList();
								}
								return Collections.singletonList(row);
							}).map(row -> row.stream().map(col -> ofNullable(col).map(Object::toString).orElse("")).collect(Collectors.toList()))
							.collect(Collectors.toList())
					).ifPresent(ws::head);
		}
		return ws.sheetNo(sheetNo);
	}

	/**
	 * Create {@link ExcelWriter}
	 * @param out {@link OutputStream}
	 * @return {@link ExcelWriter}
	 * @author Haining.Liu
	 * @date 2022-09-23 17:15:23
	 */
	private static ExcelWriter writer(OutputStream out) {
		ExcelWriterBuilder ewb = EasyExcel.write(out)
				.excelType(ExcelTypeEnum.XLSX)
				.registerConverter(ToStringConverter.get())
				.registerConverter(LocalDateConverter.get())
				.registerConverter(LocalTimeConverter.get())
				/*
				 * 其它Converter自行添加{@link com.alibaba.excel.converters.*};
				 * 或使用注解单独处理字段类型转换
				 */
				.registerConverter(DECIMAL_CONVERTER)
				.registerConverter(DATE_CONVERTER)
				.registerConverter(LOCAL_DATE_TIME_CONVERTER);
		return ewb.build();
	}
	/**
	 * 响应头
	 * @param res {@link HttpServletResponse}
	 * @param fnm {@link String}
	 * @return {@link HttpServletResponse}
	 * @throws UnsupportedEncodingException
	 * @author Haining.Liu
	 * @date 2022-09-23 17:15:23
	 */
	private static HttpServletResponse resHead(HttpServletResponse res, String fnm) throws UnsupportedEncodingException {
		if (fnm == null || fnm.trim().isEmpty()) {	// org.apache.commons.lang3.StringUtils.isBlank(fnm)
			fnm = suffix();
		}
		String utf8 = StandardCharsets.UTF_8.name();
		String encFnm = URLEncoder.encode(fnm, utf8) + ExcelTypeEnum.XLSX.getValue();
		res.setCharacterEncoding(utf8);
		res.setContentType("application/vnd.ms-excel; charset=" + utf8);
		res.setHeader("Content-Disposition", "attachment; filename=" + encFnm + "; filename*=" + utf8 + "''" + encFnm);
		return res;
	}
	/**
	 * 后缀名
	 * @return {@link String}
	 * @author Haining.Liu
	 * @date 2023-8-8 18:53:36
	 */
	private static String suffix() {
		return LocalDateTime.now().format(DateTimeFormatter.ofPattern(DateUtils.DATE_FORMAT_14));
	}
	/**
	 * 获取每个Sheet页数据
	 * @param datas {@link Collection}<{@link List}<?>>
	 * @return {@link LinkedHashMap}<{@link String}, {@link List}<?>>
	 * @author Haining.Liu
	 * @date 2023-8-8 18:53:36
	 */
	private static LinkedHashMap<String, List<?>> sheetData(Collection<List<?>> datas) {
		AtomicInteger i = new AtomicInteger();
		return ofNullable(datas)
				.map(Collection::stream)
				.map(st -> st.collect(Collectors.toMap(t -> sheetName(classify(t), i.incrementAndGet()), Function.identity(), (k1, k2) -> k2, LinkedHashMap::new)))
				.orElse(null);
	}
	/**
	 * 获取Sheet页名称
	 * @param c {@link Class}<{@link T}>:当前Sheet数据类型;
	 * @param n {@link Number}:当前Sheet序号;
	 * @return {@link String}
	 * @version V1.0.1
	 * @author Haining.Liu
	 * @date 2023-8-7 18:53:36
	 */
	private static <T> String sheetName(Class<T> c, Number n) {
		return ofNullable(desc(c)).orElse("Sheet-" + n.intValue());
	}
	/**
	 * 抽取Sheet类
	 * @param <T>
	 * @param data {@link List}<{@link T>
	 * @return {@link Class}<{@link T}>
	 * @author Haining.Liu
	 * @date 2022-09-23 17:15:23
	 */
	@SuppressWarnings("unchecked")
	private static <T> Class<T> classify(List<T> data) {
		Class<T> c = null;
		if (data != null && !data.isEmpty()) {
			T t = data.get(0);
			c = (Class<T>) t.getClass();
		}
		return c;
	}
	/**
	 * 获取{@link Description}描述
	 * @param c {@link Class}<?>
	 * @return {@link String}
	 * @author Haining.Liu
	 * @date 2022-09-23 17:15:23
	 */
	private static String desc(Class<?> c) {
		if (c == null || c == Object.class) {
			return null;
		}
		Description anno = c.getAnnotation(Description.class);
		if (anno != null) {
			return anno.value();
		}
		return desc(c.getSuperclass());
	}
}

ToStringConverter.java

package com.qoo.util.convert;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * 转{@link String}
 * @author Haining.Liu
 * @date 2021-1-9 18:58:16
 */
public class ToStringConverter implements Converter<Object> {

	@Override
	public Class<Object> supportJavaTypeKey() {
		return Object.class;
	}

	@Override
	public CellDataTypeEnum supportExcelTypeKey() {
		return CellDataTypeEnum.STRING;
	}

	@Override
	public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
		Object sv = null;
		CellDataTypeEnum tp = cellData.getType();
		if (tp == CellDataTypeEnum.STRING
				|| tp == CellDataTypeEnum.DIRECT_STRING
				|| tp == CellDataTypeEnum.RICH_TEXT_STRING) {
			sv = cellData.getStringValue();
		} else if (tp == CellDataTypeEnum.NUMBER) {
			sv = cellData.getNumberValue();
		} else if (tp == CellDataTypeEnum.BOOLEAN) {
			sv = cellData.getBooleanValue();
		} else if (tp == CellDataTypeEnum.DATE) {
			sv = cellData.getDataFormatData() != null ? cellData.getDataFormatData() : cellData.getData();
		} else if (tp == CellDataTypeEnum.EMPTY) {
			sv = null;
		} else {
			sv = cellData.getData();
		}
		return sv;
	}

	@Override
	public WriteCellData<String> convertToExcelData(Object value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
		WriteCellData<String> w = new WriteCellData<String>();
		w.setType(this.supportExcelTypeKey());
		w.setStringValue(value == null ? null : value.toString());
		return w;
	}

	public ToStringConverter() {
		super();
	}
	public static ToStringConverter get() {
		return D.C;
	}
	private static class D {
		private static final ToStringConverter C = new ToStringConverter();
	}
}

LocalDateConverter.java

package com.qoo.util.convert;

import java.time.LocalDate;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * 转{@link LocalDate}
 * @author Haining.Liu
 * @date 2022-09-23 17:15:23
 */
public class LocalDateConverter implements Converter<LocalDate> {

	@Override
	public Class<LocalDate> supportJavaTypeKey() {
		return LocalDate.class;
	}

	@Override
	public CellDataTypeEnum supportExcelTypeKey() {
		return ToStringConverter.get().supportExcelTypeKey();
	}
	@Override
	public LocalDate convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
		String sv = cellData.getStringValue();
		if (sv == null
				|| sv.length() != DateUtils.DEFAULT_DATE_FORMAT.length()
				|| !sv.contains("-")) {
			return null;
		}
		return LocalDate.parse(sv);
	}
	@Override
	public WriteCellData<String> convertToExcelData(LocalDate value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
		return ToStringConverter.get().convertToExcelData(value, contentProperty, globalConfiguration);
	}

	public LocalDateConverter() {
		super();
	}
	public static LocalDateConverter get() {
		return D.C;
	}
	private static class D {
		private static final LocalDateConverter C = new LocalDateConverter();
	}
}

LocalTimeConverter.java

package com.qoo.util.convert;

import java.time.LocalTime;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * 转{@link LocalTime}
 * @author Haining.Liu
 * @date 2022-09-23 17:15:23
 */
public class LocalTimeConverter implements Converter<LocalTime> {

	@Override
	public Class<LocalTime> supportJavaTypeKey() {
		return LocalTime.class;
	}

	@Override
	public CellDataTypeEnum supportExcelTypeKey() {
		return ToStringConverter.get().supportExcelTypeKey();
	}
	@Override
	public LocalTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
		String sv = cellData.getStringValue();
		if (sv == null
				|| sv.length() < "HH:mm".length()
				|| !sv.contains(":")) {
			return null;
		}
		return LocalTime.parse(sv);
	}
	@Override
	public WriteCellData<String> convertToExcelData(LocalTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
		return ToStringConverter.get().convertToExcelData(value, contentProperty, globalConfiguration);
	}

	public LocalTimeConverter() {
		super();
	}
	public static LocalTimeConverter get() {
		return D.C;
	}
	private static class D {
		private static final LocalTimeConverter C = new LocalTimeConverter();
	}
}

测试示例

@PostMapping("/exp")
public void exp(HttpServletResponse res) throws IOException {
	int n = 10;
	List<Ex1> datas1 = new ArrayList<>(n);
	List<Ex2> datas2 = new ArrayList<>(n);
	List<Ex3> datas3 = new ArrayList<>(n);
	for (int i = 0; i < n; i++) {
		Ex1 d1 = new Ex1().setId(i + 1).setName("Ex." + i);
		datas1.add(d1);
		Ex2 d2 = new Ex2().setId(i + 1).setMn(new BigInteger(i + "100000000")).setRemark(new StringBuilder("此列不显示"));
		datas2.add(d2);
		Ex3 d3 = new Ex3().setId(i + 10_0000.2F).setMn(new BigDecimal("1320000000." + i));
		datas3.add(d3);
	}
	List<Object> headData = new ArrayList<>(datas1);
	headData.add(0, Arrays.asList(
			Arrays.asList("信息", "ID"),
			Arrays.asList("信息", "名称"),
			Arrays.asList(null, "日期-时间"),
			Arrays.asList("日期 / 时间", "日期"),
			Arrays.asList("日期 / 时间", "时间"),
			Arrays.asList("日期 / 时间", "当前时间")
		));
	/* 单页Sheet */
	Excel.output(res, headData);	// 自定义表头
	Excel.output(res, datas1);
	Excel.output(res, datas1, "文件名_FileName_SheetName_XLSX");

	/* 多页Sheet */
	// List格式
	Excel.output(res, (Collection<List<?>>) Arrays.asList(headData, datas1, datas2, datas3));
	Excel.output(res, (Collection<List<?>>) Arrays.asList(headData, datas1, datas2, datas3), "文件名_FileName_XLSX");
	// Map格式
	LinkedHashMap<String, List<?>> ds = new LinkedHashMap<>();
	ds.put(null, headData);
	ds.put("", datas1);
	ds.put("Page2", datas2);
	ds.put("页3", datas3);
	Excel.output(res, ds);
	Excel.output(res, ds, "文件名_FileName_XLSX");
}

@Data
@Accessors(chain = true)
@Description("测试导出Excel")
private class Ex1 {
	@com.alibaba.excel.annotation.write.style.ColumnWidth(5)
	@ExcelProperty(value = "ID", order = 10)
	long id;

	@ExcelProperty(value = "名称", order = 20)
	String name;

	@com.alibaba.excel.annotation.write.style.ColumnWidth(15)
	@ExcelProperty(value = "日期", order = 50)
	LocalDate date = LocalDate.of(2021, 9, 9);

	@com.alibaba.excel.annotation.write.style.ColumnWidth(15)
	@ExcelProperty(value = "时间", order = 60)
	LocalTime time = LocalTime.of(13, 22, 01);

	@com.alibaba.excel.annotation.write.style.ColumnWidth(20)
	@ExcelProperty(value = "时", order = 30)
	LocalDateTime dt = LocalDateTime.of(this.date, this.time);

	@com.alibaba.excel.annotation.write.style.ColumnWidth(20)
	@ExcelProperty(value = "Now", order = 100)
	Date now = new Date();
}

@Data
@Accessors(chain = true)
@Description("第二Sheet")
private class Ex2 {
	@ExcelProperty(value = "ID", order = 10)
	long id;

	@ExcelProperty(value = "钱", order = 90, converter = com.alibaba.excel.converters.biginteger.BigIntegerStringConverter.class)
	BigInteger mn = BigInteger.valueOf(13);

	@com.alibaba.excel.annotation.ExcelIgnore
	@ExcelProperty(value = "备注", order = 100, converter = ToStringConverter.class)
	StringBuilder remark;
}

@Data
@Accessors(chain = true)
@Description("第叁")
private class Ex3 {
	@ExcelProperty(value = "ID", order = 10)
	float id;

	@ExcelProperty(value = "Decimal", order = 90)
	BigDecimal mn;
}

使用过程中,如出现bug,或有其它优化建议,欢迎在此文章“评论区”留言讨论,并留下您的邮箱,以便改正后及时通知您。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值