上次不好意思。发这个贴没写完。被弄为隐藏帖了。
使用注解导出Excel主要使用了POI包和反射原理,使得导出段可以灵活配置,并且不需要额外的配置文件
1:注解代码:
2:在需要导出的DTO或实体中对需要导出的字段配置导出注解
示例:
3:导出类,利用反射原理:
4: 搞定:导出时执行
得到Users 的list
ExportExcelUtil.exportExcel(usersList, "人员信息表", response);
这个是个比较老的版本,现在已经把注解和ExportExcelUtil升级了,不过还没有高强度测试。。
欢迎大家提出意见
使用注解导出Excel主要使用了POI包和反射原理,使得导出段可以灵活配置,并且不需要额外的配置文件
1:注解代码:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author 1097877378
*
*/
@Target(value = ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
public @interface ExportColumn {
/**
* 列名
*/
public String[] name() default {};//为什么用数组,大家想一下
/**
* 格式化字符串
*/
public String format() default "";
/**
* 导出次序
*/
public int order() default 0;
}
2:在需要导出的DTO或实体中对需要导出的字段配置导出注解
示例:
import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import =ExportColumn;
public class Users {
private Long id;
private String name;
private String password;
private Date birthday;
public Long getId() {
return id;
}
@ExportColumn(name={"姓名"})
public String getName() {
return name;
}
@ExportColumn(name={"密码"})
public String getPassword() {
return password;
}
@ExportColumn(format="%1$tY-%1$tm-%1$td", name={"生日"})
public Date getBirthday() {
return birthday;
}
...省略get,set
3:导出类,利用反射原理:
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import ExportColumn;
public class ExportExcelUtil {
/**
* 导出Excel文件到输出流
* @param response
* @throws IOException
*/
public static <T> void exportExcel(List<T> data, String fileName, HttpServletResponse response) throws Exception {
response.reset();
//fileName = URLEncoder.encode(fileName, "UTF-8");//only IE
fileName = new String(fileName.getBytes("GBK"), "ISO-8859-1");//IE and FireFox
response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
HSSFWorkbook workbook = createWorkbook(data);
OutputStream os = response.getOutputStream();// 取得输出流
workbook.write(os);
os.flush();
os.close();
}
private static <T> HSSFWorkbook createWorkbook(List<T> data) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet1"); {
createHeader(sheet, data.get(0));
createBody(sheet, data);
}
return workbook;
}
private static <T> void createHeader(Sheet sheet, T dataRow) {
Row header = sheet.createRow(0);
Method[] methods = dataRow.getClass().getMethods();
Arrays.sort(methods, new Comparator<Method>() {
public int compare(Method o1, Method o2) {
return o1.getName().compareTo(o2.getName());
}});
int cellindex = 0;
for(Method method : methods) {//取得DTO的导出列
if (method.isAnnotationPresent(ExportColumn.class)) {
ExportColumn export = method.getAnnotation(ExportColumn.class);
String[] titles = export.name();
Cell headerCell = header.createCell(cellindex);
headerCell.setCellValue(titles[0]);
cellindex ++;
}
}
}
private static <T> void createBody(Sheet sheet, List<T> data) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
if (!data.isEmpty()) {
int rowindex = 1;
for (T datarow : data) {
Row sheetrow = sheet.createRow(rowindex);
Method[] methods = datarow.getClass().getMethods();
Arrays.sort(methods, new Comparator<Method>() {
public int compare(Method o1, Method o2) {
return o1.getName().compareTo(o2.getName());
}});
int cellindex = 0;
for(Method method : methods) {//取得DTO的导出列
if (method.isAnnotationPresent(ExportColumn.class)) {
Object cellvalue = (Object)method.invoke(datarow);
Cell sheetcell = sheetrow.createCell(cellindex);
if (null != cellvalue) {
ExportColumn export = method.getAnnotation(ExportColumn.class);
String cellstr = cellvalue.toString();
if (!export.format().equals("")){
cellstr = String.format(export.format(), cellvalue);//按注解格式化
}
sheetcell.setCellValue(cellstr);
}
cellindex ++;
}
}
rowindex ++;
}
}
}
}
4: 搞定:导出时执行
得到Users 的list
ExportExcelUtil.exportExcel(usersList, "人员信息表", response);
这个是个比较老的版本,现在已经把注解和ExportExcelUtil升级了,不过还没有高强度测试。。
欢迎大家提出意见
6319

被折叠的 条评论
为什么被折叠?



