poi、Excel工具包,支持map转excel,bean转excel
来源:互联网 发布:网络备案申请表 编辑:程序博客网 时间:2024/06/05 02:16
参数有表头: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(); } }}
阅读全文
0 0
- poi、Excel工具包,支持map转excel,bean转excel
- POI Excel整理(转)
- java EXCEL文件读写工具包POI
- java操作Excel -- poi支持
- POI读写Excel文件[转]
- POI操作EXCEL技巧(转)
- POI操作Excel(转)
- poi实现Excel转html
- 读取 Excel(POI) 转 HTML
- POI 把 excel 转 html
- Excel POI导出excel
- Poi excel
- poi excel
- Poi Excel
- poi excel
- POI EXCEL
- POI (excel)
- POI(excel)
- lc#3求字符串中不包含重复字符的最大长度子串
- vue动态路由配置
- Java上传图片至七牛云
- 虚拟机下Centos7如何设置静态IP地址
- (docker)入门实践、环境部署、基本操作指令、镜像库、数据卷
- poi、Excel工具包,支持map转excel,bean转excel
- extundelete恢复Ubuntu系统下git clean -fd(rm)误删除的文件
- Vim小技巧
- CentOS7中systemctl的使用
- Python
- HTML5全局属性汇总
- 如何快速高效读懂1本书
- java的动态代理
- 7-22 龟兔赛跑