EasyPOI用Map导出数据的详细解决方案

EasyPOI用Map导出数据的详细解决方案

写本次的博客主要是因为公司做的一个大数据平台,因为查询的数据来源于HBase,返回的结果是一个List>,按照我们的习惯,如果使用实体类加上@Excel会自动帮我们完成字段的映射,非常容易,具体操作可以查看我的上一篇博客,如果使用Map的话需要用到ExcelExportUtil中的exportExcel(ExportParams entity, List entityList,Collection dataSet)的方法,如下所示:

 /**
     * 根据Map创建对应的Excel
     * @param entity
     *            表格标题属性
     * @param entityList
     *            Map对象列表
     * @param dataSet
     *            Excel对象数据List
     */
    public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList,
                                       Collection<?> dataSet) {
        Workbook workbook = getWorkbook(entity.getType(),dataSet.size());;
        new ExcelExportService().createSheetForMap(workbook, entity, entityList, dataSet);
        return workbook;
    }

我们怎么构造映射关系呢?
其实,EasyPOI已经为我们提供了一个实体类ExcelExportEntity,当我们导出的时候,我们需要自己创建ExcelExportEntity实例,并且给其参数赋值

话不多说,直接上代码:

1.加入EasyPOI的支持:

  
        <dependency>
            <groupId>cn.afterturngroupId>
            <artifactId>easypoi-baseartifactId>
            <version>3.2.0version>
        dependency>
        <dependency>
            <groupId>cn.afterturngroupId>
            <artifactId>easypoi-webartifactId>
            <version>3.2.0version>
        dependency>
        <dependency>
            <groupId>cn.afterturngroupId>
            <artifactId>easypoi-annotationartifactId>
            <version>3.2.0version>
        dependency>

2.创建实体类:

说好是以Map导出,为什么要创建实体类,因为本人较懒,一会直接将实体类转Map,说白了,实体类其实就是属性名和属性值的Map

import java.io.Serializable;
import java.util.Date;

public class Employee implements Serializable
{

    private Long id;

    private String username;

    private String email;

    private Integer age = 18;

    private Date bornDate = new Date();

    private Boolean sex = true;

    private Department department;

    private String headImage;
}

4.Map、String等工具类

4.1 MapUtil工具类

package com.easypoi;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import com.easypoi.StringUtility;

public  class MapUtil
{
	//获取参数中的整型数值
	public static int GetMapValue(Map<String, Object> Para, String ParaName, int DefaultValue)
	{
		//判断参数是否合法
		if (null == Para)
		{
			return DefaultValue;
		}

		//没有改字段则返回默认值
		if (false == Para.containsKey(ParaName))
		{
			return DefaultValue;
		}

		//将改字段转换为整数
		try
		{
			return Integer.parseInt(Para.get(ParaName).toString());
		}
		catch (Exception Ex)
		{
			return DefaultValue;
		}
	}

	public static long GetMapValueLong(Map<String, Object> Para, String ParaName, long DefaultValue)
	{
		//判断参数是否合法
		if (null == Para)
		{
			return DefaultValue;
		}

		//没有改字段则返回默认值
		if (false == Para.containsKey(ParaName))
		{
			return DefaultValue;
		}

		//将改字段转换为整数
		try
		{
			return Long.parseLong(Para.get(ParaName).toString());
		}
		catch (Exception Ex)
		{
			return DefaultValue;
		}
	}

	//获取参数中的整型数值
	public static String GetMapValue(Map<String, Object> Para, String ParaName, String DefaultValue)
	{
		//判断参数是否合法
		if (null == Para)
		{
			return DefaultValue;
		}

		//没有改字段则返回默认值
		if (false == Para.containsKey(ParaName))
		{
			return DefaultValue;
		}

		//将改字段转换为整数
		try
		{
			Object Value = Para.get(ParaName);
			if (null == Value)
			{
				return DefaultValue;
			}

			return Value.toString();
		}
		catch (Exception Ex)
		{
			return DefaultValue;
		}
	}

	public static Object Map2Object(Map<String, Object> MapData, String ObjectClassName)
	{
		//判断参数是否合法
		if ((null == MapData) || (null == ObjectClassName))
		{
			return null;
		}

		//加载目标类
		Class ClassItem = null;
		try
		{
			ClassLoader Loader = MapUtil.class.getClassLoader();
			ClassItem = Loader.loadClass(ObjectClassName);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		if (null == ClassItem)
		{
			return null;
		}

		//如果目标类是Map则直接返回
		if (ClassItem.isAssignableFrom(Map.class))
		{
			return MapData;
		}

		//构造该类的实例
		Object Instance = null;
		try
		{
			Instance = ClassItem.newInstance();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		if (null == Instance)
		{
			return null;
		}

		//对各个字段进行赋值
		for (Map.Entry<String, Object> Item : MapData.entrySet())
		{
			String Key = Item.getKey();
			if (true == StringUtility.IsNullOrEmpty(Key))
			{
				continue;
			}

			Object Value = Item.getValue();
			if (null == Value)
			{
				continue;
			}

			//先通过字段给对象赋值
			Field FieldItem = null;
			try
			{
				FieldItem = ClassItem.getField(Key);
				if (false == Modifier.isPublic(FieldItem.getModifiers()))
				{
					FieldItem = null;
				}
			}
			catch (Exception Ex)
			{

			}
			if (null != FieldItem)
			{
				try
				{
					FieldItem.set(Instance, Value);
					continue;
				}
				catch (Exception Ex)
				{
					Ex.printStackTrace();
				}
			}

			//通过set方法进行赋值
			Method MethodItem = null;
			try
			{
				MethodItem = ClassItem.getMethod("set" + Key, Value.getClass());
				if (false == Modifier.isPublic(MethodItem.getModifiers()))
				{
					continue;
				}

				MethodItem.invoke(Instance, Value);
				continue;
			}
			catch (Exception Ex)
			{
				Ex.printStackTrace();
			}
		}

		return Instance;
	}

	public static Map<String, Object> Object2Map(Object Obj)
	{
		//判断参数是否合法
		if (null == Obj)
		{
			return null;
		}

		//构造map
		Map<String, Object> ReturnMap = new HashMap<String, Object>();
		String FieldName = null;
		Object FieldValue = null;;

		//反射对象
		Class ClassItem = Obj.getClass();

		//如果目标类和Map相同则不处理
		if (Obj instanceof Map)
		{
			return (Map<String, Object>)Obj;
		}

		//获得所有的字段public类型的
		Field[] FieldList = ClassItem.getFields();
		for (Field FieldItem : FieldList)
		{
			//不是public成员则不处理
			if (false == Modifier.isPublic(FieldItem.getModifiers()))
			{
				continue;
			}

			//获得该字段的值
			try
			{
				FieldName = FieldItem.getName();
				FieldValue = FieldItem.get(Obj);
				ReturnMap.put(FieldName, FieldValue);
			}
			catch (Exception Ex)
			{

			}
		}

		//获得该类的get方法
		for (Method MethodItem : ClassItem.getMethods())
		{
			//必须为public
			if (false == Modifier.isPublic(MethodItem.getModifiers()))
			{
				continue;
			}

			//FieldName
			FieldName = MethodItem.getName();

			//必须为get开头的参数
			if (false == FieldName.startsWith("get"))
			{
				continue;
			}

			//获得真正的字段的名称
			FieldName = FieldName.substring(3);
			if (null == FieldName)
			{
				continue;
			}
			if (0 >= FieldName.length())
			{
				continue;
			}

			//参数必须为无参数
			Type[] InputParameterTypeList = MethodItem.getGenericParameterTypes();
			if (null != InputParameterTypeList)
			{
				if (1 <= InputParameterTypeList.length)
				{
					continue;
				}
			}

			//调用该方法
			try
			{
				FieldValue = MethodItem.invoke(Obj);
				ReturnMap.put(FieldName, FieldValue);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}

		return ReturnMap;
	}


	public static int Map2ConfigObject(Map<String, Object> InputMapData, Object ConfigObject, Map<String, Object> ConfigObjectExtParameter)
	{
		int Result = 0;

		//判断参数是否合法
		if ((null == InputMapData) || (null == ConfigObject))
		{
			return Result;
		}

		//构造新的Map
		Map<String, Object> MapData = new HashMap<String, Object>();
		MapData.putAll(InputMapData);

		//加载目标类
		Class ClassItem = ConfigObject.getClass();
		if (null == ClassItem)
		{
			return Result;
		}

		//如果目标类是Map则直接返回
		if (ClassItem.isAssignableFrom(Map.class))
		{
			return Result;
		}

		//构造该类的实例
		Object Instance = ConfigObject;

		//保存已经删除的字段
		Set<String> AssignedFields = new TreeSet<String>();

		//对各个字段进行赋值
		for (Map.Entry<String, Object> Item : MapData.entrySet())
		{
			String Key = Item.getKey();
			if (true == StringUtility.IsNullOrEmpty(Key))
			{
				continue;
			}

			Object Value = Item.getValue();
			if (null == Value)
			{
				continue;
			}

			//先通过字段给对象赋值
			Field FieldItem = null;
			try
			{
				FieldItem = ClassItem.getField(Key);
				if (false == Modifier.isPublic(FieldItem.getModifiers()))
				{
					FieldItem = null;
				}
			}
			catch (Exception Ex)
			{

			}
			if (null != FieldItem)
			{
				try
				{
					FieldItem.set(Instance, Value);
					AssignedFields.add(Key);
					continue;
				}
				catch (Exception Ex)
				{
					Ex.printStackTrace();
				}
			}

			//通过set方法进行赋值
			Method MethodItem = null;
			try
			{
				MethodItem = ClassItem.getMethod("set" + Key.replaceFirst(Key.substring(0,1), Key.substring(0,1).toUpperCase()), Value.getClass());
				if (false == Modifier.isPublic(MethodItem.getModifiers()))
				{
					continue;
				}

				MethodItem.invoke(Instance, Value);
				AssignedFields.add(Key);
				continue;
			}
			catch (Exception Ex)
			{
			}
		}

		//删除MapData中已经赋值的字段
		for (String Field : AssignedFields)
		{
			MapData.remove(Field);
		}

		//给Parameter赋值
		if (null != ConfigObjectExtParameter)
		{
			ConfigObjectExtParameter.putAll(MapData);
		}

		return Result;
	}

}

4.2 StringUtility

package com.easypoi;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtility
{
	public static boolean IsCellPhone(String Value)
	{
		if (true == StringUtility.IsNullOrEmpty(Value))
		{
			return false;
		}
		
		return Value.matches("^1\\d{10}$");
	}
	
	public boolean EqualsOneOf(String Value, String...ItemList)
	{
		//判断参数是否合法
		if ((null == Value) || (null == ItemList))
		{
			return false;
		}
		
		//判断字符串是否和某一个匹配
		for (String Item : ItemList)
		{
			if (true == StringUtility.IsSame(Value, Item))
			{
				return true;
			}
		}
		
		return false;
	}
	
	public static String FillValue(String Src, int ExpectMinLength, String Value)
	{
		//如果字符串为空则返回默认值
		if (true == StringUtility.IsNullOrEmpty(Src))
		{
			return Value;
		}
		
		//长度不够则返回默认值
		if (Src.length() < ExpectMinLength)
		{
			return Value;
		}
		
		return Src;
	}

	public static int CopyStream(InputStream Input, OutputStream Output)
	{
		//判断参数是否合法
		if ((null == Input) || (null == Output))
		{
			return 1;
		}
		
	  	//构建临时buffer,默认200K,因为图片的大小一般在200K左右
    	int BufferSize = 200 * 1024;
    	byte[] Buffer = new byte[BufferSize];
    	
    	try
    	{
	    	//循环读出内容
	    	for (; ;)
	    	{
	    		//读取一片
	    		int ReadSize = Input.read(Buffer);
	    		if (0 >= ReadSize)
	    		{
	    			break;
	    		}
	    		
	    		//写入到出入流中
	    		Output.write(Buffer, 0, ReadSize);
	    	}
    	}
    	catch (Exception Ex)
    	{
    		Ex.printStackTrace();
    		return 3;
    	}

    	return 0;
	}
		
    public static String BinaryFile2Base64(InputStream FileStream)  
    {  
        ByteArrayOutputStream ArrayStream = new ByteArrayOutputStream();
        
        //拷贝到缓存中
        if (0 != CopyStream(FileStream, ArrayStream))
        {
        	return null;
        }

        //对字节数组Base64编码  
        Encoder Encoder = Base64.getEncoder();
        return new String(Encoder.encode(ArrayStream.toByteArray()));  
    } 
    
    public static String BinaryFile2Base64(byte[] Bytes)
    {
    	if (null == Bytes || 0 >= Bytes.length)
    	{
    		return null;
    	}
    	 //对字节数组Base64编码  
        Encoder Encoder = Base64.getEncoder();
        return new String(Encoder.encode(Bytes));  
    }
	
    public static InputStream Base642BinaryFile(String Base64Content)
    {
    	if (null == Base64Content)
    	{
    		return null;
    	}
    	
    	//对输入内容进行解码
    	byte[] Content = Base64.getDecoder().decode(Base64Content);
    	
		return new ByteArrayInputStream(Content);
    }
    
    public static String GetExtensionFileName(String FileName)
    {
    	if (true == StringUtility.IsNullOrEmpty(FileName))
    	{
    		return "";
    	}
    	
    	int Index = FileName.lastIndexOf('.');
    	if (0 >= Index)
    	{
    		return "";
    	}
    	
    	return FileName.substring(Index + 1); 
    }
    
	public static String GetFileName(String FileName)
	{
		//判断参数是否合法
		if (null == FileName)
		{
			return null;
		}
		
		//获得短文件名
		int Index = FileName.lastIndexOf('/');
		String Name = (0 > Index) ? FileName : FileName.substring(Index + 1);
		
		return Name;
	}
    
	public static String GetFileNameWithoutExtension(String FileName)
	{
		//判断参数是否合法
		if (null == FileName)
		{
			return null;
		}
		
		//获得短文件名
		int Index = FileName.lastIndexOf('/');
		String Name = (0 > Index) ? FileName : FileName.substring(Index + 1);
		
		//获得文件名
		Index = Name.lastIndexOf('.');
		Name = (0 > Index) ? Name : Name.substring(0, Index);

		return Name;
	}
		
	public static boolean IsMatchReg(String Reg, String Value)
	{
		//判断参数是否合法
		if ((true == StringUtility.IsNullOrEmpty(Reg))
			|| true == StringUtility.IsNullOrEmpty(Value))
		{
			return false;
		}
		
		//使用正则表达式进行匹配
		Pattern RegExpression = Pattern.compile(Reg);
		Matcher Match = RegExpression.matcher(Value);
		
		//判断匹配结果
		return Match.matches();
	}
	
	public static boolean IsValidCitizenNO(String CitizenNO)
	{
		return IsMatchReg("^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$", CitizenNO);
	}
	
	public static boolean IsValidBankNO(String BankNO)
	{
		return IsMatchReg("^[0-9]{16,19}$", BankNO);
	}	
	
	public static int IsValid(String Value, int MinLength, int MaxLength, int ErrorCode)
	{
		String LocalValue = Value;
		
		//长度为0判断
		if (true == StringUtility.IsNullOrEmpty(Value))
		{
			LocalValue = "";
		}
		
		//判断长度是否合法
		int Length = LocalValue.length();
		if ((MinLength <= Length) && (Length <= MaxLength))
		{
			return 0;
		}
		
		return ErrorCode;
	}
	
	public static boolean IsNullOrEmpty(String Str)
	{
		if (null == Str)
		{
			return true;
		}
		
		if (0 == Str.length())
		{
			return true;
		}
		
		return false;
	}
	
	public static boolean IsNullOrEmptyEx(Object Value)
	{
		if (null == Value)
		{
			return true;
		}
		
		if (0 == Value.toString().length())
		{
			return true;
		}
		
		return false;
	}	
	
	public static boolean IsSame(String Str1, String Str2)
	{
		if (null != Str1 && null != Str2)
		{
			if (0 == Str1.compareToIgnoreCase(Str2))
			{
				return true;
			}
		}
		
		return false;
	}
	
	public static int CheckString(String Str, int MinLength, int MaxLength)
	{
		//判断是否为空
		if (true == IsNullOrEmpty(Str))
		{
			return 0;
		}
		
		//得到长度
		int Length = Str.length();
		if ((Length < MinLength) || (Length > MaxLength))
		{
			return 0;
		}
		
		return 0;
		
	}
	
	public static String BytesToHexString(byte[] Src, int Offset, int Length)
	{
		if (null == Src)
		{
			return null;
		}
		String To = new String();
		Length += Offset;
		for (int i = Offset; i < Src.length && i < Length; i++)
		{
			String HexStr = Integer.toHexString((Src[i] & 0x0FF)).toUpperCase();
			To += (2 > HexStr.length()) ? '0' + HexStr + ' ' : HexStr + ' ';
		}
		To = To.trim();
		return To;		
	}
	
	public static String BytesToHexString(byte[] Src)
	{
		return BytesToHexString(Src, 0, Src.length);
	}
	
	public static byte ToByte(char C)
	{
		byte B = (byte) "0123456789ABCDEF".indexOf(C);
		if (0 > B)
		{
			B = (byte) "0123456789abcdef".indexOf(C);
		}
		return B;
	}
	
	public static byte[] HexStringToByte(String Src)
	{
		if (null == Src)
		{
			return null;
		}
		Src = Src.replace(" ", "").toUpperCase();
		int len = (Src.length() / 2);
		byte[] To = new byte[len];
		char[] achar = Src.toCharArray();
		for (int i = 0; i < len; i++)
		{
			int pos = i * 2;
			To[i] = (byte) (ToByte(achar[pos]) << 4 | ToByte(achar[pos + 1]));
		}
		return To;
	}
	
	public static byte[] StringToBCD(String Src)
	{
		int len = Src.length();
		int mod = len % 2;
		
		if (mod != 0)
		{
			Src = "0" + Src;
			len = Src.length();
		}
		
		byte abt[] = new byte[len];
		if (len >= 2)
		{
			len = len / 2;
		}
		
		byte bbt[] = new byte[len];
		abt = Src.getBytes();
		int j, k;
		
		for (int p = 0; p < Src.length()/2; p++)
		{
			if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9'))
			{
				j = abt[2 * p] - '0';
			}
			else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z'))
			{
				j = abt[2 * p] - 'a' + 0x0a;
			}
			else
			{
				j = abt[2 * p] - 'A' + 0x0a;
			}
			
			if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9'))
			{
				k = abt[2 * p + 1] - '0';
			}
			else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z'))
			{
				k = abt[2 * p + 1] - 'a' + 0x0a;
			}
			else
			{
				k = abt[2 * p + 1] - 'A' + 0x0a;
			}
			
			int a = (j << 4) + k;
			byte b = (byte) a;
			bbt[p] = b;
		}
		return bbt;
	}
	
	public static String BCDToString(byte[] bytes)
	{
		// 若输入非 BCD 码, 则返回 null
		StringBuffer temp = new StringBuffer(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++)
		{
			int High = (byte) ((bytes[i] & 0xf0) >>> 4);
			int Low = (byte) (bytes[i] & 0x0f);
			if (9 < High || 9 < Low)
			{
				return null;
			}
			temp.append(High);
			temp.append(Low);
		}
		return temp.toString();
	}


	//判断浮点数(double和float)
	public static boolean IsDouble(String str)
	{
		if (true == IsNullOrEmpty(str))
		{
			return false;
		}
		Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
		return pattern.matcher(str).matches();
	}


}

5.测试

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.vo.BaseEntityTypeConstants;
import com.easypoi.Employee;
import com.easypoi.MapUtil;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yfq
 * @create 2020-07-06 14:10
 */
public class Test3
{
    public static void main(String[] args) throws Exception
    {
        // Map作为每一行的数据容器,List作为行的容器
        List<Map<String, Object>> rowDataList = new ArrayList<>();

        // 每个ExcelExportEntity存放Map行数据的key
        List<ExcelExportEntity> keyList = new ArrayList<>();

        Employee employee = new Employee();
        employee.setUsername("张三");
        employee.setSex(false);
        employee.setBornDate(new Date());
        employee.setHeadImage("G:\\1.jpg");

        Map<String, Object> map = MapUtil.Object2Map(employee);
        rowDataList.add(map);

        Employee employee2 = new Employee();
        employee2.setUsername("王五");
        employee2.setSex(false);
        employee2.setBornDate(new Date());
        employee2.setHeadImage("G:\\2.jpg");
        Map<String, Object> map2 = MapUtil.Object2Map(employee2);
        rowDataList.add(map2);

        System.out.println(rowDataList);

        ExcelExportEntity username = new ExcelExportEntity("姓名", "Username");
        keyList.add(username);

        ExcelExportEntity sex = new ExcelExportEntity("性别", "Sex");
        sex.setReplace(new String[]{"男_true","女_false"});
        keyList.add(sex);

        ExcelExportEntity bornDate = new ExcelExportEntity("出生年月", "BornDate");
        //设置导出日期格式
        bornDate.setFormat("yyyy-MM-dd");
        keyList.add(bornDate);


        ExcelExportEntity headImage = new ExcelExportEntity("图像", "HeadImage");
        //此处是设置图片
        headImage.setExportImageType(1);  //设置导出图片类型是文件
        headImage.setType(BaseEntityTypeConstants.IMAGE_TYPE);  //设置类型是图片
        keyList.add(headImage);


        // 生成workbook 并导出
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("花名册","测试"), keyList, rowDataList);

        FileOutputStream fos = new FileOutputStream("G:\\abc.xls");
        workbook.write(fos);
        fos.close();
    }
}

导出结果:
EasyPOI用Map导出数据的详细解决方案_第1张图片

总结一下:

  1. 参数一:ExportParams ,导出参数设置,比如title是导出来大标题,sheetName是单元格名称:

ExportParams 类

public class ExportParams extends ExcelBaseParams {

    /**
     * 表格名称
     */
    private String title;

    /**
     * 表格名称
     */
    private short titleHeight = 10;

    /**
     * 第二行名称
     */
    private String secondTitle;

    /**
     * 表格名称
     */
    private short secondTitleHeight = 8;
    /**
     * sheetName
     */
    private String sheetName;
    /**
     * 过滤的属性
     */
    private String[] exclusions;
    /**
     * 是否添加需要需要
     */
    private boolean addIndex;
    /**
     * 是否添加需要需要
     */
    private String indexName = "序号";
    /**
     * 冰冻列
     */
    private int freezeCol;
    /**
     * 表头颜色
     */
    private short color = HSSFColor.WHITE.index;
    /**
     * 属性说明行的颜色 例如:HSSFColor.SKY_BLUE.index 默认
     */
    private short headerColor = HSSFColor.SKY_BLUE.index;
    /**
     * Excel 导出版本
     */
    private ExcelType type = ExcelType.HSSF;
    /**
     * Excel 导出style
     */
    private Class<?> style = ExcelExportStylerDefaultImpl.class;

    /**
     * 表头高度
     */
    private double headerHeight = 9D;
    /**
     * 是否创建表头
     */
    private boolean isCreateHeadRows = true;
    /**
     * 是否动态获取数据
     */
    private boolean isDynamicData = false;
    /**
     * 是否追加图形
     */
    private boolean isAppendGraph = true;
    /**
     * 单sheet最大值
     * 03版本默认6W行,07默认100W
     */
    private int maxNum = 0;

    /**
     * 导出时在excel中每个列的高度 单位为字符,一个汉字=2个字符
     * 全局设置,优先使用
     */
    public short height = 0;

    public ExportParams() {

    }

    public ExportParams(String title, String sheetName) {
        this.title = title;
        this.sheetName = sheetName;
    }

    public ExportParams(String title, String sheetName, ExcelType type) {
        this.title = title;
        this.sheetName = sheetName;
        this.type = type;
    }

    public ExportParams(String title, String secondTitle, String sheetName) {
        this.title = title;
        this.secondTitle = secondTitle;
        this.sheetName = sheetName;
    }
}
  1. 参数二:
    2.1 ExcelExportEntity ,个人理解是映射关系,对cell类型做映射,
    我们主要关注该类的几个字段:
字段 类型 说明 默认值
key Object 对应数据Map中的Key,用来建立映射关系的 null
width int 宽高就不详细说了,默认是10 10
height int 宽高就不详细说了,默认是10 10
exportImageType int 图片的类型,1是文件,2是数据库,这个非常重要,如果导出是图片,即把地址要显示成图片,一定要设置其参数1是文件,常用是1,2是数据库,该地址来自于数据库中某个表的字段 0
orderNum int 排序顺序 ,可以对字段排序,比如你想把用户编码展示在第一列等 0

2.2 ExcelExportEntity 是ExcelBaseEntity的子类,所以我们还要把ExcelBaseEntity中的字段进行设置

字段 字段类型 说明 默认值
name String 导出标题名,这个必须设置, null
type int 字段类型,默认是BaseEntityTypeConstants.STRING_TYPE,即1,如果导出图片则将type只设置成3 1
format String 导出日期格式,比如设置导出日期格式为"yyyy-MM-dd" null
replace String[] 值得替换 导出是{a_id,b_id} 导入反过来,所以只用写一个 null

ExcelExportEntity 类

package cn.afterturn.easypoi.excel.entity.params;

import java.util.List;

/**
 * excel 导出工具类,对cell类型做映射
 *
 * @author JueYue
 * @version 1.0 2013年8月24日
 */
public class ExcelExportEntity extends ExcelBaseEntity implements Comparable<ExcelExportEntity> {

    /**
     * 如果是MAP导出,这个是map的key
     */
    private Object                  key;

    private double                  width           = 10;

    private double                  height          = 10;

    /**
     * 图片的类型,1是文件,2是数据库
     */
    private int                     exportImageType = 0;

    /**
     * 排序顺序
     */
    private int                     orderNum        = 0;

    /**
     * 是否支持换行
     */
    private boolean                 isWrap;

    /**
     * 是否需要合并
     */
    private boolean                 needMerge;
    /**
     * 单元格纵向合并
     */
    private boolean                 mergeVertical;
    /**
     * 合并依赖`
     */
    private int[]                   mergeRely;
    /**
     * 后缀
     */
    private String                  suffix;
    /**
     * 统计
     */
    private boolean                 isStatistics;

    private String                   numFormat;
    /**
     *  是否隐藏列
     */
    private boolean                  isColumnHidden;
    /**
     * 枚举导出属性字段
     */
    private String                    enumExportField;

    public boolean isColumnHidden() {
        return isColumnHidden;
    }

    public void setColumnHidden(boolean columnHidden) {
        isColumnHidden = columnHidden;
    }

    private List<ExcelExportEntity> list;

    public ExcelExportEntity() {

    }

    public ExcelExportEntity(String name) {
        super.name = name;
    }

    public ExcelExportEntity(String name, Object key) {
        super.name = name;
        this.key = key;
    }

    public ExcelExportEntity(String name, Object key, int width) 					    {
        super.name = name;
        this.width = width;
        this.key = key;
    }

 

    @Override
    public int compareTo(ExcelExportEntity prev) {
        return this.getOrderNum() - prev.getOrderNum();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        ExcelExportEntity other = (ExcelExportEntity) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }
}

ExcelBaseEntity

package cn.afterturn.easypoi.excel.entity.params;

import java.lang.reflect.Method;
import java.util.List;

import cn.afterturn.easypoi.excel.entity.vo.BaseEntityTypeConstants;

/**
 * Excel 导入导出基础对象类
 * @author JueYue
 *  2014年6月20日 下午2:26:09
 */
public class ExcelBaseEntity {
    /**
     * 对应name
     */
    protected String     name;
    /**
     * 对应groupName
     */
    protected String     groupName;
    /**
     * 对应type
     */
    private int          type = BaseEntityTypeConstants.STRING_TYPE;
    /**
     * 数据库格式
     */
    private String       databaseFormat;
    /**
     * 导出日期格式
     */
    private String       format;
    /**
     * 导出日期格式
     */
    private String[]     replace;
    /**
     * 字典名称
     */
    private String       dict;
    /**
     * set/get方法
     */
    private Method       method;
    /**
     * 这个是不是超链接,如果是需要实现接口返回对象
     */
    private boolean     hyperlink;
    /**
     * 固定的列
     */
    private Integer      fixedIndex;

    private List<Method> methods;
}

EasyPOI 关于图片的导出还是非常牛皮的,不用自己实现

你可能感兴趣的:(Java)