使用apache POI把list集合里面的实体写入Excel(java)
一、导入maven依赖包
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency>
二、案例代码
public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {/*** 创建一个Excel文件*/HSSFWorkbook workbook = new HSSFWorkbook();/*** 创建一个sheet*/HSSFSheet sheet = workbook.createSheet("员工表一");/*** 在sheet表中添加0行*/HSSFRow row = sheet.createRow(0);/*** 创建单元格,设置表头*/HSSFCell cell = row.createCell(0);cell.setCellValue("员工工号");cell = row.createCell(1);cell.setCellValue("省份证号");cell = row.createCell(2);cell.setCellValue("姓名");cell = row.createCell(3);cell.setCellValue("性别");cell = row.createCell(4);cell.setCellValue("出生日期");cell = row.createCell(5);cell.setCellValue("联系方式");cell = row.createCell(6);cell.setCellValue("家庭地址");cell = row.createCell(7);cell.setCellValue("担任职务");cell = row.createCell(8);cell.setCellValue("角色名称");cell = row.createCell(9);cell.setCellValue("所属科室");/*** 把list集合里面的数据写入工作表*/List<Doctor> list = doctorService.findAll();SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");for (int i = 0; i < list.size(); i++) {/*** 创建行*/HSSFRow row1 = sheet.createRow(i + 1);Doctor doctor = list.get(i);/*** 为单元格写入值,从0开始*/row1.createCell(0).setCellValue(doctor.getDocid());row1.createCell(1).setCellValue(doctor.getIdcard());row1.createCell(2).setCellValue(doctor.getDocname());row1.createCell(3).setCellValue(doctor.getDocsex());row1.createCell(4).setCellValue(simpleDateFormat.format(doctor.getDocbirthday()));row1.createCell(5).setCellValue(doctor.getDocphone());row1.createCell(6).setCellValue(doctor.getDocaddress());row1.createCell(7).setCellValue(doctor.getDuty());row1.createCell(8).setCellValue(doctor.getRolename());row1.createCell(9).setCellValue(doctor.getDptname());}File file = new File("E://hospital//doctor.xls");if (file.exists()) {file.delete();}/*** 将文件保存到指定的位置*/try {file.createNewFile();workbook.write(file);System.out.println("导出到成功");System.out.println("默认位置:E://hospital//doctor.xls");workbook.close();} catch (IOException e) {e.printStackTrace();}request.setAttribute("msg","导出成功--已保存到:E://hospital//doctor.xls");request.getRequestDispatcher("/success.jsp").forward(request, response);}
三、运行截图
使用apache POI把list集合里面的实体写入Excel(java)相关推荐
- 文档处理工具库——Apache POI的使用
Apache POI工具包可用来出来microsoft office办公套件中的Word文档,excel文档,ppt文档以及visio文档等,直接使用maven导入jar包即可使用. 目录 1.处理e ...
- java poi设置单元格格式为数值,Apache POI 如何读取Excel中数值类型单元格所规定的保留小数位?...
问题描述 java apache poi 包内是否有方法可以获取Excel数值类型单元格中保留的位数? 问题出现的环境背景及自己尝试过哪些方法 有这么一个场景: 需求:从Excel文件中读取数据,原样 ...
- java excel 操作 poi_Java使用apache poi进行excel相关操作
一.基本介绍 1.1.Apache POI介绍 Apache POI是一个可以进行微软的文档进行开源库,可以操作的文档类型包括word.ppt.excel.visio.outlook.... 本文主要 ...
- apache poi 修改docx表格_JAVA poi对word.docx文件的修改
之前老用poi处理excel,近期看了一下处理word,同时也学习了一下,根据网上的资料写了一个读取word.docx 文档并在word中加入自定义的文本以及图片. 其中poi中有2中word的处理一 ...
- GrapeCity Documents for Excel 与 Apache POI 功能对比
GrapeCity Documents for Excel 与 Apache POI 功能对比 GrapeCity Documents for Excel 是什么? GrapeCity Documen ...
- java word转html 报错org/apache/poi/xwpf/usermodel/IRunBody
poi 3.14下报错: org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerExcepti ...
- apache poi教程_Apache POI教程
apache poi教程 Welcome to Apache POI Tutorial. Sometimes we need to read data from Microsoft Excel Fil ...
- 从Excel导出宕机到初学Apache POI
学习来由 因为在定位一个公司的OOM的时候,花了2天时间,定位问题定位出了方向,知道是导出Excel的时候对象占用太大导致的OOM,但是后来计算了一下数据完全没有达到OOM的情况.症结点就是结论是没错 ...
- Apache POI和JXL(Java Excel)的基本使用——操作excel
一.简介 开发中经常会涉及到excel的处理,如导出Excel,导入Excel到数据库中,操作Excel目前有两个框架,一个是apache 的poi, 另一个是 Java Excel Apache P ...
最新文章
- 快讯 | 老黄啊,特斯拉背着你找AMD了,咱可不能给他降价
- 读微型计算机原理与接口技术 段的理解
- 比特币现金扬声器系列II将领先的比特币一起带来
- 学会分析网站空间日志
- GdiPlus[21]: IGPPathGradientBrush 之 InterpolationColors
- linux单cpu进程能并行吗,linux,cpu_如何知道运行多少个进程最有效率?,linux,cpu,并行,parallel-execution - phpStudy...
- 反思代码能力提升方法:重构 多写 知识面
- linux系统外接硬盘_linux通过fstab自动挂载外接磁盘
- Android 使用MediaPlayer播放assets或者raw目录的音频文件
- leetcode 911. Online Election | 911. 在线选举(加强堆 + 二分查找)
- 计算机网络技术之网络系统设计与组建工程
- linux下kafka安装与配置
- golang中的异常如何捕获?
- 【转】解决Navicat 报错:1130-host ... is not allowed to connect to this MySql server,MySQL不允许从远程访问的方法 .
- 联想微型计算机如果进入BIos,联想笔记本各机型如何进入bios
- 敬伟PS教程:基础篇A
- Error: A <Route> is only ever to be used as the child of <Routes> element, never rendereddirectly
- 如何解决光纤熔接过程中遇到这六大问题?
- android设置头像同时设置模糊背景
- 装的系统没有截图和计算机工具栏,不想安装专用的截图工具?这里有几个Windows(snipping tool)截图小技巧_都叫兽软件...