Java POI 导出EXCEL经典实现 Java导出Excel
2015-10-19 13:28
585 查看
在web开发中,有一个经典的功能,就是数据的导入导出。特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作。而数据导出的格式一般是EXCEL或者PDF,我这里就用两篇文章分别给大家介绍下。(注意,我们这里说的数据导出可不是数据库中的数据导出!么误会啦^_^)
呵呵,首先我们来导出EXCEL格式的文件吧。现在主流的操作Excel文件的开源工具有很多,用得比较多的就是Apache的POI及JExcelAPI。这里我们用Apache POI!我们先去Apache的大本营下载POI的jar包:http://poi.apache.org/ ,我这里使用的是3.0.2版本。
将3个jar包导入到classpath下,什么?忘了怎么导包?不会吧!好,我们来写一个导出Excel的实用类(所谓实用,是指基本不用怎么修改就可以在实际项目中直接使用的!)。我一直强调做类也好,做方法也好,一定要通用性和灵活性强。下面这个类就算基本贯彻了我的这种思想。那么,熟悉许老师风格的人应该知道,这时候该要甩出一长串代码了。没错,大伙请看:
package com.text; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Color; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; public class CreateSimpleExcelToDisk{ /** * @功能:手工构建一个简单格式的Excel */ private static List<Student> getStudent() throws Exception { List list = new ArrayList(); SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd"); Student user1 = new Student(1, "张三", 16, df.parse("1997-03-12")); Student user2 = new Student(2, "李四", 17, df.parse("1996-08-12")); Student user3 = new Student(3, "王五", 26, df.parse("1985-11-12")); list.add(user1); list.add(user2); list.add(user3); return list; } public static void main(String[] args) throws Exception { // 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet("学生表一"); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row = sheet.createRow((int) 0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 row.setRowStyle(style); style.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex()); style.setFillPattern(CellStyle.THIN_BACKWARD_DIAG); HSSFCell cell = row.createCell(0); cell.setAsActiveCell(); cell.setCellValue("学号"); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue("姓名"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("年龄"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("生日"); cell.setCellStyle(style); // 第五步,写入实体数据 实际应用中这些数据从数据库得到, List list = CreateSimpleExcelToDisk.getStudent(); for (int i = 0; i < list.size(); i++) { row = sheet.createRow((int) i + 1); Student stu = (Student) list.get(i); // 第四步,创建单元格,并设置值 HSSFCell cell0 = row.createCell(0); HSSFCell cell1 = row.createCell(1); HSSFCell cell2 = row.createCell(2); HSSFCell cell3 = row.createCell(3); cell0.setCellStyle(style); cell1.setCellStyle(style); cell2.setCellStyle(style); cell3.setCellStyle(style); cell0.setCellValue((double) stu.getId()); cell1.setCellValue(stu.getName()); cell2.setCellValue((double) stu.getAge()); cell3 = row.createCell(3); cell3.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu.getBirth())); } // 第六步,将文件存到指定位置 try { FileOutputStream fout = new FileOutputStream("E:/students.xls"); wb.write(fout); fout.close(); } catch (Exception e) { e.printStackTrace(); } } }
相关文章推荐
- JAVA RMI远程方法调用简单实例
- Eclipse中调试Maven+Jetty
- Eclipse的Spring插件
- Eclipse导入项目中文乱码
- spring jar包直接下载
- Eclipse 的快捷键以及文档注释、多行注释的快捷键
- Mybatis系列之实战篇(上)
- java--基于UDP的多线程通信
- java中间==、equals和hashCode差额
- Spring的事务到底该给Dao配置还是给Service配置?
- JAVA调用易信接口向指定好友推送消息(二)POST测试
- JavaSE知识点个人总结
- ubuntu 14.04 安装java8
- 权限设计,以及使用优秀的权限框架shiro整合SpringMVC
- JAVA调用易信接口向指定好友推送消息(一)背景需求
- <学习笔记>JAVA 的 keytool
- StrutsPrepareAndExecuteFilter的作用
- 从头认识java-1.6 多态
- 从头认识java-1.6 多态
- Java基础--异常