参数有表头:linkedHashMap的key要与内容的key一致
表内容:map的key或者bean的属性名要与表头的数据保持一致,不要求顺序。
默认保存到系统的临时文件。
下面是代码
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
/**
* @author niexiang
* @Description
* @create 2017-12-13 16:18
**/
public class PoiUtil {
/**
* Map生成excel表
* @param head (头部的key必须对应map的key)
* @param contents
* @return 返回保存的地址
*/
public static String mapToExcel(LinkedHashMap<String,String> head, List<Map> contents,String sheetName,String fileName) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(sheetName);
//生成内容
for (int i = 0;i< contents.size()+1;i++){//excel的行数
HSSFRow row = sheet.createRow(i);
int j = 0;//列
for (Map.Entry<String,String> property : head.entrySet()) {
HSSFCell cell = row.createCell(j++);
if (i == 0) {//表头信息
cell.setCellValue(property.getValue());
}else{
cell.setCellValue(contents.get(i - 1).get(property.getKey()) == null ? null :contents.get(i - 1).get(property.getKey()).toString());
}
}
}
String folder=System.getProperty("java.io.tmpdir");
String filePath = folder + "/" + fileName;
return writeFile(workbook,filePath);
}
/**
* bean生成excel表
* @param head (头部的key必须对应bean的属性名)
* @param contents
* @return 返回保存的地址
*/
public static <T> String beanToExcel(LinkedHashMap<String,String> head, List<T> contents,String sheetName,String fileName) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(sheetName);
Class clazz = contents.get(0).getClass();
//生成内容
for (int i = 0;i< contents.size()+1;i++){//excel的行数
HSSFRow row = sheet.createRow(i);
int j = 0;//列
for (Map.Entry<String,String> property : head.entrySet()) {
HSSFCell cell = row.createCell(j++);
if (i == 0) {//表头信息
cell.setCellValue(property.getValue());
}else{
Field field = clazz.getDeclaredField(property.getKey());
field.setAccessible(true);
Object result = field.get(contents.get(i - 1));
cell.setCellValue(result == null ? null :result.toString());
}
}
}
String folder=System.getProperty("java.io.tmpdir");
String filePath = folder + fileName;
return writeFile(workbook,filePath);
}
private static String writeFile(HSSFWorkbook workbook, String filePath) {
FileOutputStream stream = null;
BufferedOutputStream outputStream = null;
try {
stream = new FileOutputStream(filePath);
outputStream = new BufferedOutputStream(stream);
workbook.write(outputStream);
return filePath;
} catch (IOException e) {
e.printStackTrace();
}finally {
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (stream != null){
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static void main(String[] args) {
LinkedHashMap<String,String> map = new LinkedHashMap<>();
map.put("name","姓名");
map.put("age","年龄");
map.put("gender","性别");
List<Person> list = new ArrayList<>();
for (int i = 0; i < 10 ; i++) {
list.add(new Person("123",i,"男"));
}
try {
beanToExcel(map,list,"ceshi123","ceshi123.xls");
} catch (Exception e) {
e.printStackTrace();
}
}
}