摘要:第一步创建需要导出的实体类导出学生基本信息的实体类姓名学号学院专业年级宿舍性别民族出生日期说明这个为自定义注解,如果有字段在中不需要导出,则不加自定义注解即可。
第一步:创建需要导出的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 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);
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/66279.html
摘要:一行代码完成对象和之间的转换。说明属性列名称四版本更新日志版本,新特性导出支持对象装换为,并且支持字节数组等多种导出方式导入支持转换为对象,并且支持文件路径等多种导入方式版本,新特性字段支持类型。 《Java对象和Excel转换工具XXL-EXCEL》 showImg(https://segmentfault.com/img/remote/1460000012470335);showI...
Octopus 如何导入excel 如何导出excel Octopus Octopus 是一个简单的java excel导入导出工具. 如何导入excel 下面是一个excel文件中sheet的数据,有四个学生信息. studentId name sex inTime score 20134123 John M 2013-9-1 89 20124524 Joyce F 2012...
摘要:消费之后,多线程处理文件导出,生成文件后上传到等文件服务器。前端直接查询并且展现对应的任务执行列表,去等文件服务器下载文件即可。这客户体验不友好,而且网络传输,系统占用多种问题。拓展阅读导出最佳实践框架 产品需求 产品经理需要导出一个页面的所有的信息到 EXCEL 文件。 需求分析 对于 excel 导出,是一个很常见的需求。 最常见的解决方案就是使用 poi 直接同步导出一个 exc...
阅读 3339·2023-04-26 03:06
阅读 3755·2021-11-22 09:34
阅读 1215·2021-10-08 10:05
阅读 3192·2021-09-22 15:53
阅读 3632·2021-09-14 18:05
阅读 1569·2021-08-05 09:56
阅读 2076·2019-08-30 15:56
阅读 2187·2019-08-29 11:02