poi版本为3.14
公共的导入方法:
第一种实现:
/**
* 公共的到处excel方法
* @param list 数据集合
* @param map
*/
public static void excelOutput(List list,LinkedHashMap<String, String> map){
try {
HSSFWorkbook workbook = new HSSFWorkbook();//创建Excel工作簿对象
HSSFSheet sheet = workbook.createSheet();//在工作簿中创建工作表对象
workbook.setSheetName(0, "测试");//设置工作表的名称
HSSFRow row = null;//行
HSSFCell cell = null;//列
//创建表头,使用迭代器进行迭代
int _x=0;
row = sheet.createRow(0);//创建第1行
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext();) {
Map.Entry propertyEntry = (Map.Entry) propertyIterator.next();//把迭代的键值临时保存在Map.Entry中。
String headValue = propertyEntry.getValue() != null ? (String) propertyEntry.getValue() : "" + "\"";
cell=row.createCell(_x);//新增一列
cell.setCellType(1);//设置单元格的数据格式为字符串
cell.setCellValue(headValue); //向单元格中写入数据
_x++;
}
// 写入文件内容
int _i=1;//写入文件的行的内容的开始
for(Iterator it = list.iterator();it.hasNext();){
row = sheet.createRow(_i);//创建第二行到最后一行
Object rowObje = (Object) it.next();//返回迭代的下一个元素。
Class<?> clz = rowObje.getClass();//list集合的所有对象
int _x1=0;//写入文件内容的列的开始
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext();) {
Map.Entry propertyEntry = (Map.Entry) propertyIterator.next();
Field field = clz.getDeclaredField(propertyEntry.getKey().toString());//通过反射,通过键获取类的所有的名称和数据类型
String fieldType = field.getGenericType().toString();//通过反射获取键的数据类型
String methodName = "get";
if (("boolean").equals(fieldType)) {//布尔类型的是特殊的
methodName = "is";
}
//通过反射,得到对象的get方法和数据格式
Method m = (Method) rowObje.getClass().getMethod(methodName + getMethodName(propertyEntry.getKey().toString()));
Object value = m.invoke(rowObje);//反射调用对象里面的方法
cell = row.createCell(_x1);//新增1列
cell.setCellType(1);//给单元格设置数据格式,为字符串
cell.setCellValue(value.toString());
_x1++;
}
_i++;
}
String filePath = "D:";//文件保存路径
String fileName = "测试文件.xlsx";//文件名称
File xlsFile = new File(filePath,fileName);
FileOutputStream fos = new FileOutputStream(xlsFile);//创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
workbook.write(fos);//将文档对象写入文件输出流
fos.close();//关闭流
System.out.println("导出完成");
} catch (Exception e) {
System.out.println("导出失败");
}
}
//把字符串的首字母大写
private static String getMethodName(String fieldName){
String methodName = fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
return methodName;
}
测试数据:
public static void main(String[] args) {
List<UserBo> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
UserBo bo = new UserBo();
bo.setId(i);
bo.setName("张三"+i);
bo.setPassword("密码"+i);
bo.setPhone("电话"+i);
bo.setDate(new Timestamp(i*1000000000));
bo.setBo(true);
bo.setDou(15.23+i);
list.add(bo);
}
LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("id", "id");
map.put("name", "名字");
map.put("password", "密码");
map.put("phone", "手机号码");
map.put("date", "时间");
map.put("bo", "布尔");
map.put("dou", "小数");
excelOutput(list,map);
}
需要用到的类:POI导出、Object类、Class类、反射、关于List和LinkedHashMap的迭代、Iterator迭代类
反射的类是:Field类、Method类
实现的思路:
1、传入list集合和LinkedHashMap集合,必须传入LinkedHashMap,不能用map,因为通过迭代,重LinkedHashMap取出的值是有序的
2、通过迭代LinkedHashMap,可以获取导出的excel表格的表头
3、通过反射获取list集合里面的对象的所有属性和数据类型
4、通过反射得到get方法
5、通过反射调用get方法
6、把通过调用get方法得到的值,赋给单元格。
第二种实现(不需要用到Iterator迭代类,其他没有变);
/**
* 公共的到处excel方法
* @param list 数据集合
* @param map
*/
public static void excelOutput(List list,LinkedHashMap<String, String> map){
try {
HSSFWorkbook workbook = new HSSFWorkbook();//创建Excel工作簿对象
HSSFSheet sheet = workbook.createSheet();//在工作簿中创建工作表对象
workbook.setSheetName(0, "测试");//设置工作表的名称
HSSFRow row = null;//行
HSSFCell cell = null;//列
//创建表头
int _x=0;
row = sheet.createRow(0);//创建第1行
for (String key : map.keySet()) {
String value = map.get(key);
String headValue = value;
cell=row.createCell(_x);//新增一列
cell.setCellType(1);//设置单元格的数据格式为字符串
cell.setCellValue(headValue); //向单元格中写入数据
_x++;
}
// 写入文件内容
for(int i=0;i<list.size();i++){
row = sheet.createRow(i+1);//创建第二行到最后一行
Object rowObje = list.get(i);//返回迭代的下一个元素。
Class<?> clz = rowObje.getClass();//list集合的所有对象
int _x1=0;//写入文件内容的列的开始
for (String key : map.keySet()) {
Field field = clz.getDeclaredField(key);//通过反射,通过键获取类的所有的名称和数据类型
String fieldType = field.getGenericType().toString();//通过反射获取键的数据类型
String methodName = "get";
if (("boolean").equals(fieldType)) {//布尔类型的是特殊的
methodName = "is";
}
//通过反射,得到对象的get方法和数据格式
Method m = (Method) rowObje.getClass().getMethod(methodName + key.substring(0,1).toUpperCase() + key.substring(1));
Object value = m.invoke(rowObje);//反射调用对象里面的方法
cell = row.createCell(_x1);//新增1列
cell.setCellType(1);//给单元格设置数据格式,为字符串
cell.setCellValue(value.toString());
_x1++;
}
}
String filePath = "D:";//文件保存路径
String fileName = "测试文件.xlsx";//文件名称
File xlsFile = new File(filePath,fileName);
FileOutputStream fos = new FileOutputStream(xlsFile);//创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
workbook.write(fos);//将文档对象写入文件输出流
fos.close();//关闭流
System.out.println("导出完成");
} catch (Exception e) {
System.out.println("导出失败");
}
}
测试数据和第一个方法是一样的。
如果有什么缺陷,请大家指出下。