使用POI和Java反射机制导出数据到excel中
发布时间:2020-05-25 15:26:22 所属栏目:Java 来源:互联网
导读:使用POI和Java反射机制导出数据到excel中
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10.1</version>
</dependency>
第二步:定义一个用于保存数据的实体类 package com.xjj.poi;
import java.util.Date;
public class Student {
private long id;
private String name;
private int age;
private boolean sex;
private Date birthday;
public Student() {
}
public Student(long id,String name,int age,boolean sex,Date birthday) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.birthday = birthday;
}
//sex转换为中文
public String getSexName(){
return (sex==true) ? "男" : "女";
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean getSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
第三步:利用Java反射机制把数据填入excel workbook中并测试输出到一个文件。具体步骤请看代码注释。测试例子中,可以自由的决定需要读取实体类Student中的哪些属性到excel中。 package com.xjj.util;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.xjj.poi.Student;
public class ExcelUtil {
/**
* 根据输入的数据生成一个XSSFWorkbook
* @param title:sheet名称
* @param propertyHeaderMap:<property,header>(<T中的property名称、有getter就行,对应显示在Excel sheet中的列标题>)
* 用LinkedHashMap保证读取的顺序和put的顺序一样
* @param dataSet:实体类集合
* @return:XSSFWorkbook
*/
public static <T> XSSFWorkbook generateXlsxWorkbook(String title,LinkedHashMap<String,String> propertyHeaderMap,Collection<T> dataSet) {
// 声明一个工作薄
XSSFWorkbook workbook = new XSSFWorkbook();
// 生成一个表格
XSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((int) 15);
XSSFCellStyle headerStyle = getHeaderStyle(workbook);
XSSFCellStyle contentStyle = getContentStyle(workbook);
// 生成表格标题行
XSSFRow row = sheet.createRow(0);
int i = 0;
for(String key : propertyHeaderMap.keySet()){
XSSFCell cell = row.createCell(i);
cell.setCellStyle(headerStyle);
XSSFRichTextString text = new XSSFRichTextString(propertyHeaderMap.get(key));
cell.setCellValue(text);
i++;
}
//循环dataSet,每一条对应一行
int index = 0;
for(T data : dataSet){
index ++;
row = sheet.createRow(index);
int j = 0;
for(String property : propertyHeaderMap.keySet()){
XSSFCell cell = row.createCell(j);
cell.setCellStyle(contentStyle);
//拼装getter方法名
String getMethodName = "get" + property.substring(0,1).toUpperCase() + property.substring(1);
try {
//利用反射机制获取dataSet中的属性值,填进cell中
Class<? extends Object> tCls = data.getClass();
Method getMethod = tCls.getMethod(getMethodName,new Class[] {});
Object value = getMethod.invoke(data,new Object[] {}); //调用getter从data中获取数据
// 判断值的类型后进行类型转换
String textValue = null;
if (value instanceof Date) {
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
textValue = sdf.format(date);
} else {
// 其它数据类型都当作字符串简单处理
textValue = value.toString();
}
/*if(textValue != null){
Pattern p = Pattern.compile("^//d+(//.//d+)?$");
Matcher matcher = p.matcher(textValue);
if (matcher.matches()) {
// 是数字当作double处理
cell.setCellValue(Double.parseDouble(textValue));
} else {
XSSFRichTextString richString = new XSSFRichTextString(textValue);
cell.setCellValue(richString);
}
}*/
XSSFRichTextString richString = new XSSFRichTextString(textValue);
cell.setCellValue(richString);
} catch (Exception e) {
e.printStackTrace();
}
j++;
}
}
return workbook;
}
/**
* 生成一个标题style
* @return style
*/
public static XSSFCellStyle getHeaderStyle(Workbook workbook){
return getHeaderStyle(workbook,Color.BLUE,IndexedColors.WHITE.getIndex());
}
/**
* 生成一个指定颜色的标题style
* @param workbook
* @param foregroundColor
* @param fontColor
* @return
*/
public static XSSFCellStyle getHeaderStyle(Workbook workbook,Color foregroundColor,short fontColor){
// 生成一个样式(用于标题)
XSSFCellStyle style = (XSSFCellStyle) workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(new XSSFColor(foregroundColor));
style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
style.setBorderRight(XSSFCellStyle.BORDER_THIN);
style.setBorderTop(XSSFCellStyle.BORDER_THIN);
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
// 生成一个字体
XSSFFont font = (XSSFFont) workbook.createFont();
font.setColor(fontColor);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
return style;
}
/**
* 生成一个用于内容的style
* @param workbook
* @return
*/
public static XSSFCellStyle getContentStyle(Workbook workbook){
// 生成并设置另一个样式(用于内容)
XSSFCellStyle style = (XSSFCellStyle) workbook.createCellStyle();
//style.setFillForegroundColor(new XSSFColor(Color.YELLOW));
//style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
style.setBorderRight(XSSFCellStyle.BORDER_THIN);
style.setBorderTop(XSSFCellStyle.BORDER_THIN);
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
// 生成另一个字体
XSSFFont font = (XSSFFont) workbook.createFont();
font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style.setFont(font);
return style;
}
//测试:
public static void main(String[] args) {
List<Student> dataSet = new ArrayList<Student>();
dataSet.add(new Student(10000001,"张三",20,true,new Date()));
dataSet.add(new Student(20000002,"李丽",24,false,new Date()));
dataSet.add(new Student(30000003,"王五",22,new Date()));
LinkedHashMap<String,String> propertyHeaderMap = new LinkedHashMap<>();
//propertyHeaderMap.put("id","唯一标识"); //注释掉,不导出id
propertyHeaderMap.put("name","姓名");
propertyHeaderMap.put("age","年龄");
propertyHeaderMap.put("sexName","性别"); //直接获取Student中的sexName,而不是sex
propertyHeaderMap.put("birthday","生日");
try {
XSSFWorkbook ex = ExcelUtil.generateXlsxWorkbook("测试tab",propertyHeaderMap,dataSet);
OutputStream out = new FileOutputStream("F://student3.xlsx");
ex.write(out);
System.out.println("导出成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试结果(性别已经自动转换为中文): 以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- java – 在clojure中,是(=’a’a)指的是’同一个原子’?
- SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)
- java – log4j:与Tomcat 6的错误
- java-为什么出队对我的代码无法正常工作?
- Java 9中如何对IntegerCache进行修改详解
- java – 配置Spring Security以使用customPasswordAuthenti
- android apk 一键生成混淆文件python脚本分享
- java对properties文件进行解析
- java – Netty增加ChannelBuffer大小
- 如何通过拖动鼠标禁用缩放而不通过jfreechart中的mousewhee
