excel数据导出
- 格式:docx
- 大小:282.29 KB
- 文档页数:15
1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能。 .NET的开发人员则可以利用NPOI (POI for .NET) 来存取 POI 的功能。
2.POI结构 HSSF - 提供读写Microsoft Excel XLS格式档案的功能。 XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。 HWPF - 提供读写Microsoft Word DOC格式档案的功能。 HSLF - 提供读写Microsoft PowerPoint格式档案的功能。 HDGF - 提供读Microsoft Visio格式档案的功能。 HPBF - 提供读Microsoft Publisher格式档案的功能。 HSMF - 提供读Microsoft Outlook格式档案的功能。
参考实例 在web开发中,有一个经典的功能,就是数据的导入导出。特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作。而数据导出的格式一般是EXCEL或者PDF
首先我们来导出EXCEL格式的文件吧。现在主流的操作Excel文件的开源工具有很多,用得比较多的就是Apache的POI及JExcelAPI。这里我们用Apache POI!我们先去Apache的大本营下载POI的jar包:http://poi.apache.org/ 或者从我附件下载
Java代码 1. package org.leno.export.util; 2. 3. import java.util.Date; 4. 5. public class Student { 6. private long id; 7. private String name; 8. private int age; 9. private boolean sex; 10.private Date birthday; 11. 12.public Student() { 13.super(); 14.// TODO Auto-generated constructor stub 15.} 16. 17.public Student(long id, String name, int age, boolean sex, Date birthday) { 18.super(); 19.this.id = id; 20.this.name = name; 21.this.age = age; 22.this.sex = sex; 23.this.birthday = birthday; 24.} 25. 26.public long getId() { 27.return id; 28.} 29. 30.public void setId(long id) { 31.this.id = id; 32.} 33. 34.public String getName() { 35.return name; 36.} 37. 38.public void setName(String name) { 39.this.name = name; 40.} 41. 42.public int getAge() { 43.return age; 44.} 45. 46.public void setAge(int age) { 47.this.age = age; 48.} 49. 50.public boolean getSex() { 51.return sex; 52.} 53. 54.public void setSex(boolean sex) { 55.this.sex = sex; 56.} 57. 58.public Date getBirthday() { 59.return birthday; 60.} 61. 62.public void setBirthday(Date birthday) { 63.this.birthday = birthday; 64.} 65. 66.}
Java代码 1. package org.leno.export.util; 2. 3. public class Book { 4. private int bookId; 5. private String name; 6. private String author; 7. private float price; 8. private String isbn; 9. private String pubName; 10.private byte[] preface; 11. 12.public Book() { 13.super(); 14.} 15. 16.public Book(int bookId, String name, String author, float price, 17.String isbn, String pubName, byte[] preface) { 18.super(); 19.this.bookId = bookId; 20.this.name = name; 21.this.author = author; 22.this.price = price; 23.this.isbn = isbn; 24.this.pubName = pubName; 25.this.preface = preface; 26. 27.} 28. 29.public int getBookId() { 30.return bookId; 31.} 32. 33.public void setBookId(int bookId) { 34.this.bookId = bookId; 35.} 36. 37.public String getName() { 38.return name; 39.} 40. 41.public void setName(String name) { 42.this.name = name; 43.} 44. 45.public String getAuthor() { 46.return author; 47.} 48. 49.public void setAuthor(String author) { 50.this.author = author; 51.} 52. 53.public float getPrice() { 54.return price; 55.} 56. 57.public void setPrice(float price) { 58.this.price = price; 59.} 60. 61.public String getIsbn() { 62.return isbn; 63.} 64. 65.public void setIsbn(String isbn) { 66.this.isbn = isbn; 67.} 68. 69.public String getPubName() { 70.return pubName; 71.} 72. 73.public void setPubName(String pubName) { 74.this.pubName = pubName; 75.} 76. 77.public byte[] getPreface() { 78.return preface; 79.} 80. 81.public void setPreface(byte[] preface) { 82.this.preface = preface; 83.} 84. 85.}
上面这两个类一目了然,就是两个简单的javabean风格的类。再看下面真正的重点类: Java代码
1. package org.leno.export.util; 2. import java.io.*; 3. import java.lang.reflect.*; 4. import java.util.*; 5. import java.util.regex.Matcher; 6. import java.util.regex.Pattern; 7. import java.text.SimpleDateFormat; 8. import javax.swing.JOptionPane; 9. import org.apache.poi.hssf.usermodel.*; 10.import org.apache.poi.hssf.util.HSSFColor; 11. 12./** 13.* 利用开源组件POI3.0.2动态导出EXCEL文档 转载时请保留以下信息,注明出处! 14.* 15.* @author leno 16.* @version v1.0 17.* @param 18.* 应用泛型,代表任意一个符合javabean风格的类 19.* 注意这里为了简单起见,boolean型的属性xxx的get器方式为getXxx(),而不是isXxx() 20.* byte[]表jpg格式的图片数据 21.*/ 22.public class ExportExcel { 23. 24.public void exportExcel(Collection dataset, OutputStream out) { 25.exportExcel("测试POI导出EXCEL文档", null, dataset, out, "yyyy-MM-dd");