|
EXCEL导出的工具类
ExcelExportUtil
要用到这个工具类,需要先了解Excel类
Excel类的代码见下:
package com.core.annotation;
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 Excel {
//导入时,对应数据库的字段 主要是用户区分每个字段,不能有annocation重名的
//导出时的列名 导出排序跟定义了annotation的字段的顺序有关
public String exportName();
//导出时在excel中每个列的宽 单位为字符,一个汉字=2个字符
//如 以列名列内容中较合适的长度 例如姓名列6 【姓名一般三个字】 性别列4【男女占1,但是列标题两个汉字】
//限制1-255
public int exportFieldWidth();
//导出时是否进行字段转换 例如 性别用int存储,导出时可能转换为男,女
//若是sign为1,则需要在pojo中加入一个方法 get字段名Convert()
//例如,字段sex ,需要加入 public String getSexConvert() 返回值为string
//若是sign为0,则不必管
public int exportConvertSign();
//导入数据是否需要转化 及 对已有的excel,是否需要将字段转为对应的数据
//若是sign为1,则需要在pojo中加入 void set字段名Convert(String text)
public int importConvertSign();
}
在需要导出的类上使用注解,比如我用的是Tonline类,我在字段cname上加了注解
@Excel(exportConvertSign=0,exportFieldWidth=25,exportName="真实姓名", importConvertSign = 0)
private String cname;
用注解的好处是灵活,侵入性低,可以方便的对需要的字段进行统一处理,不需要导出的字段不加EXCEL注解
测试类:
package com.jeecg.util.excel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jeecg.model.Tonline;
import com.jeecg.pageModel.Online;
import com.jeecg.service.OnlineServiceI;
public class ExcelTest {
public static void main (String args[])
{
String[] paths = { "classpath:spring*.xml" };
ApplicationContext ctx3 = new ClassPathXmlApplicationContext(paths);
OnlineServiceI service = (OnlineServiceI)ctx3.getBean("onlineService");
Online online=new Online();
List<Tonline> name = service.find(online);
File file=new File("D:\\test.xls");
OutputStream stream;
try {
stream = new FileOutputStream(file);
ExcelExportUtil.exportExcel("导出测试", Tonline.class, name, stream);
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
注:因为我用了MYECLIPSE来加载JEECG项目,第一次我跑测试类时,报错
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.jeecg.dao.impl.BaseDaoImpl.setSessionFactory(org.hibernate.SessionFactory); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [D:\jeecg\jeecg\trunk\jeecg-framework\WebRoot\WEB-INF\classes\spring-hibernate.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at com.jeecg.util.excel.ExcelTest.main(ExcelTest.java:23)
关键的一行:nested exception is java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()
出错是因为类加载的顺序不正确所致,理由是系统引入JEECG项目时,有两个JAR包有同包名,同类名的情况存在,解决办法:为了保证测试效果,我先删除了机子上的对JAVAEE.JAR包的引入,再跑测试程序,测试通过
|
|