1.首先下载jar,我用的3.8版本,网上可以找到;
2.举例要导出的是某一个实体类的所有字段,如Student.java:
import java.util.Date; 2. 3.public class Student 4.{ 5. private int id; 6. private String name; 7. private int age; 8. private Date birth; 9. 10. public Student() 11. { 12. } 13. 14. public Student(int id, String name, int age, Date birth) 15. { 16. this.id = id; 17. this.name = name; 18. this.age = age; 19. this.birth = birth; 20. } 21. 22. public int getId() 23. { 24. return id; 25. } 26. 27. public void setId(int id) 28. { 29. this.id = id; 30. } 31. 32. public String getName() 33. { 34. return name; 35. } 36. 37. public void setName(String name) 38. { 39. this.name = name; 40. } 41. 42. public int getAge() 43. { 44. return age; 45. } 46. 47. public void setAge(int age) 48. { 49. this.age = age; 50. } 51. 52. public Date getBirth() 53. { 54. return birth; 55. } 56. 57. public void setBirth(Date birth) 58. { 59. this.birth = birth; 60. } 61. 62.}
3.下面开始正式导出:
import java.io.FileOutputStream; 2.import java.text.SimpleDateFormat; 3.import java.util.ArrayList; 4.import java.util.List; 5. 6.import org.apache.poi.hssf.usermodel.HSSFCell; 7.import org.apache.poi.hssf.usermodel.HSSFCellStyle; 8.import org.apache.poi.hssf.usermodel.HSSFRow; 9.import org.apache.poi.hssf.usermodel.HSSFSheet; 10.import org.apache.poi.hssf.usermodel.HSSFWorkbook; 11. 12.public class CreateSimpleExcelToDisk 13.{ 14. /** 15. * @功能:手工构建一个简单格式的Excel 16. */ 17. private static List<Student> getStudent() throws Exception 18. { 19. List list = new ArrayList(); 20. SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd"); 21. 22. Student user1 = new Student(1, "张三", 16, df.parse("1997-03-12")); 23. Student user2 = new Student(2, "李四", 17, df.parse("1996-08-12")); 24. Student user3 = new Student(3, "王五", 26, df.parse("1985-11-12")); 25. list.add(user1); 26. list.add(user2); 27. list.add(user3); 28. 29. return list; 30. } 31. 32. public static void main(String[] args) throws Exception 33. { 34. // 第一步,创建一个webbook,对应一个Excel文件 35. HSSFWorkbook wb = new HSSFWorkbook(); 36. // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet 37. HSSFSheet sheet = wb.createSheet("学生表一"); 38. // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short 39. HSSFRow row = sheet.createRow((int) 0); 40. // 第四步,创建单元格,并设置值表头 设置表头居中 41. HSSFCellStyle style = wb.createCellStyle(); 42. style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 43. 44. HSSFCell cell = row.createCell(0); 45. cell.setCellValue("学号"); 46. cell.setCellStyle(style); 47. cell = row.createCell(1); 48. cell.setCellValue("姓名"); 49. cell.setCellStyle(style); 50. cell = row.createCell(2); 51. cell.setCellValue("年龄"); 52. cell.setCellStyle(style); 53. cell = row.createCell(3); 54. cell.setCellValue("生日"); 55. cell.setCellStyle(style); 56. 57. // 第五步,写入实体数据 实际应用中这些数据从数据库得到, 58. List list = CreateSimpleExcelToDisk.getStudent(); 59. 60. for (int i = 0; i < list.size(); i++) 61. { 62. row = sheet.createRow((int) i + 1); 63. Student stu = (Student) list.get(i); 64. // 第四步,创建单元格,并设置值 65. row.createCell(0).setCellValue((double) stu.getId()); 66. row.createCell(1).setCellValue(stu.getName()); 67. row.createCell(2).setCellValue((double) stu.getAge()); 68. cell = row.createCell(3); 69. cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu 70. .getBirth())); 71. } 72. // 第六步,将文件存到指定位置 73. try 74. { 75. FileOutputStream fout = new FileOutputStream("E:/students.xls"); //这里是指定文件导出路径及英文文件名,
/*如果需要弹出下载提示框和中文文件名,设置编码格式以防止乱码 */
// response.setContentType("application/vnd.ms-excel;charset=gb2312");
// String filename="学生表.xls";
// response.setHeader("Content-disposition", "attachment;filename="+new String(filename.getBytes("gb2312"),"iso8859-1"));
// OutputStream fout = response.getOutputStream(); 76. wb.write(fout); 77. fout.close(); 78. } 79. catch (Exception e) 80. { 81. e.printStackTrace(); 82. } 83. } 84.}