import java.util.Date; public class Student { private int id; private String name; private int age; private Date birth; public Student() { } public Student(int id, String name, int age, Date birth) { this.id = id; this.name = name; this.age = age; this.birth = birth; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } }
import java.io.File; 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; public class ExportToExcel { /** * @功能:手工构建一个简单格式的Excel */ private static List<Student> getStudent() throws Exception { List<Student> list = new ArrayList<Student>(); 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")); Student user4 = new Student(4, "王6", 23, df.parse("1987-11-12")); list.add(user1); list.add(user2); list.add(user3); list.add(user4); return list; } public static void writeDataToExcel(String[] heads, String filePath,String sheetName) throws Exception{ // 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet(sheetName); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row = sheet.createRow(0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 HSSFCell cell = null; int line=0; for(String head:heads){ cell = row.createCell(line++); cell.setCellValue(head); cell.setCellStyle(style); } // 第五步,写入实体数据 实际应用中这些数据从数据库得到, List<Student> list = ExportToExcel.getStudent(); int count = 0; for (Student student : list) { row = sheet.createRow(++count); // 第四步,创建单元格,并设置值 row.createCell(0).setCellValue((double) student.getId()); row.createCell(1).setCellValue(student.getName()); row.createCell(2).setCellValue((double) student.getAge()); cell = row.createCell(3); cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(student .getBirth())); } // 第六步,将文件存到指定位置 try { File file = new File(filePath); if(file.exists()){ file.delete(); } FileOutputStream fout = new FileOutputStream(file); wb.write(fout); fout.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { String[] heads = new String[]{"学号","姓名","年龄","出生日期"}; writeDataToExcel(heads, "E:/students.xls","学生表一"); } }
相关推荐
可以运行的POI导出Excel文件实例,里面有两种方法,一个是Servlet,一个是main
使用apach poi 导出Excel,可直接在项目中使用使用apach poi 导出Excel,可直接在项目中使用使用apach poi 导出Excel,可直接在项目中使用
java使用POI导出 Excel+图片工具类 ,里面含有poi jar包,只调用接口即可直接保存Excel。使用的时候需先把数据封装,具体包装需根据实际导出数据进行处理。文件demo中只提供包装格式。
Poi导出Excel工具类 支持大数据量 多sheet页方式 已经封装好,直接拿来调用里面的方法传入参数就行 很简单
一个POI导出Excel万级数据分页实现 解决内存溢出问题 完整的 project demo 有数据库dmp文件
POI导出Excel工具类,自动设置标题 列名 文件名,可插入图片,合并单元格
Java Poi 导出excel(支持各种设置字体、颜色、垂直居中)
poi导出excel需要的jar
poi导出excel文档
利用poi导出excel2007的时候出现内存溢出,当数据量过大,这种情况就更不容易控制了,这个文档就是解决这类问题,我就是用的这个,数据二十万都没溢出过,希望对大家有帮助。
该实例集成maven开发环境,里面讲解了POI导入和导出Excel表格的基本用法
poi导出excel demo,poi导出excel demo,poi导出excel demo,poi导出excel demo,poi导出excel demo,
JAVA POI导出EXCEL代码
Java实现POI导出Excel 博文参考:http://blog.csdn.net/itmyhome1990/article/details/49818045
poi作为导出excel常用的工具,方便快捷。对于excel指定下拉列表的列,如何生成呢?本文提供如何生成下拉列表的excel列
POI导出EXCEL经典实现 (Java反射机制更加灵活的应用)
poi 导出EXCEL 例子 poi 导出EXCEL 例子poi 导出EXCEL 例子poi 导出EXCEL 例子
struts2中利用poi导出excel的简单例子
利用poi导出Excel
里面是完整的java程序,导入可执行,根据数据生成excel文件,http://localhost:8080/poi/export