java excel生成_java导出excel工具类

第一步:创建需要导出的excel实体类

/**

* 导出学生基本信息的实体类

* Created by staunch on 2016/11/22.

*/

@Data

public class ExcelBaseInfo {

@ExcelAnno(head = "姓名")

private String name;

@ExcelAnno(head = "学号")

private String studentId;

@ExcelAnno(head = "学院")

private String CollegeName;

@ExcelAnno(head = "专业")

private String majorName;

@ExcelAnno(head = "年级")

private String gradeName;

@ExcelAnno(head = "宿舍")

private String dorm;

@ExcelAnno(head = "性别")

private String sex;

@ExcelAnno(head = "民族")

private String nation;

@ExcelAnno(head = "出生日期")

private Date birthday;

}

说明:@ExcelAnno这个为自定义注解,如果有字段在excel中不需要导出,则不加自定义注解即可。

第二步:自定义注解如下:

/**

* Created by staunch on 2016/11/25.

*/

@Documented

@Target({ElementType.METHOD, ElementType.FIELD,ElementType.PARAMETER,ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

public @interface ExcelAnno {

String head() default "";

}

第三步:工具类源码

/**

* Created by staunch on 2016/11/22.

*/

@Log4j

public class ExportExcelUtil {

/**

* 这是一个通用的方法,利用了JAVA的反射机制,可以将放置在JAVA集合中并且符号一定条件的数据以EXCEL

* 的形式输出到指定IO设备上

* @param title 表格标题名

* @param dataset 需要显示的数据集合,集合中一定要放置符合javabean风格的类的对象。

* @param out 与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中

* @param pattern 如果有时间数据,设定输出格式。默认为"yyy-MM-dd"

*/

@SuppressWarnings("deprecation")

public void exportExcel(String title, List dataset, OutputStream out,

String pattern) throws Exception {

// 声明一个工作薄

HSSFWorkbook workbook = new HSSFWorkbook();

// 生成一个表格

HSSFSheet sheet = workbook.createSheet(title);

// 设置表格默认列宽度为20个字节

sheet.setDefaultColumnWidth((short) 20);

// 生成一个样式

HSSFCellStyle style = workbook.createCellStyle();

// 设置这些样式

style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);

style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

style.setBorderBottom(HSSFCellStyle.BORDER_THIN);

style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

style.setBorderRight(HSSFCellStyle.BORDER_THIN);

style.setBorderTop(HSSFCellStyle.BORDER_THIN);

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

// 生成一个字体

HSSFFont font = workbook.createFont();

font.setColor(HSSFColor.VIOLET.index);

font.setFontHeightInPoints((short) 12);

font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

// 把字体应用到当前的样式

style.setFont(font);

// 生成并设置另一个样式

HSSFCellStyle style2 = workbook.createCellStyle();

style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);

style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);

style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);

style2.setBorderRight(HSSFCellStyle.BORDER_THIN);

style2.setBorderTop(HSSFCellStyle.BORDER_THIN);

style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);

style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

// 生成另一个字体

HSSFFont font2 = workbook.createFont();

font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

// 把字体应用到当前的样式

style2.setFont(font2);

if (dataset == null || dataset.size() == 0) return;

T tempT = dataset.get(0);

Field[] heads = tempT.getClass().getDeclaredFields();

List headList = new ArrayList<>();

//获取字段注解的表头

for(int i=0;i

//过滤掉没加注解的字段

if (heads[i].getAnnotations().length == 0) continue;

ExcelAnno exAnno =(ExcelAnno)heads[i].getAnnotations()[0];

String head = exAnno.head();

headList.add(head);

}

// 产生表格标题行

HSSFRow row = sheet.createRow(0);

for (int i=0;i

HSSFCell cell = row.createCell(i);

cell.setCellStyle(style);

HSSFRichTextString text = new HSSFRichTextString(headList.get(i));

cell.setCellValue(text);

}

// 遍历集合数据,产生数据行

Iterator it = dataset.iterator();

int index = 0;

while (it.hasNext()) {

index++;

row = sheet.createRow(index);

T t = (T) it.next();

Field[] fields = t.getClass().getDeclaredFields();

List fieldsList = new ArrayList<>();

for (Field field : fields) {

if(field.getAnnotations().length != 0)

fieldsList.add(field);

}

for (Field field:fieldsList) {

HSSFCell cell = row.createCell(fieldsList.indexOf(field));

cell.setCellStyle(style2);

String fieldName = field.getName();

String getMethodName = "get"

+ fieldName.substring(0, 1).toUpperCase()

+ fieldName.substring(1);

Class tCls = t.getClass();

Method getMethod = tCls.getMethod(getMethodName,

new Class[]

{});

Object value = getMethod.invoke(t, new Object[]

{});

// 判断值的类型后进行强制类型转换

String textValue = null;

if (value == null) {

cell.setCellValue("");

}

if (value instanceof Integer) {

int intValue = (Integer) value;

cell.setCellValue(intValue);

} else if (value instanceof Float) {

float fValue = (Float) value;

cell.setCellValue(fValue);

} else if (value instanceof Double) {

double dValue = (Double) value;

cell.setCellValue(dValue);

} else if (value instanceof Long) {

long longValue = (Long) value;

cell.setCellValue(longValue);

} else if (value instanceof Date) {

Date date = (Date) value;

SimpleDateFormat sdf = new SimpleDateFormat(pattern);

textValue = sdf.format(date);

cell.setCellValue(textValue);

} else {

// 其它数据类型都当作字符串简单处理

textValue = value==null? "":value.toString();

cell.setCellValue(textValue);

}

}

}

workbook.write(out);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值