设为首页 加入收藏

TOP

POI实现导出EXCEL详解(一)
2014-11-23 19:38:35 】 浏览:694
Tags:POI 实现 导出 EXCEL 详解

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格式档案的功能。


3.Busy Developers' Guide to HSSF and XSSF Features


4.实例


在web开发中,有一个经典的功能,就是数据的导入导出。特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作。而数据导出的格式一般是EXCEL或者PDF,我这里就用两篇文章分别给大家介绍下。(注意,我们这里说的数据导出可不是数据库中的数据导出!么误会啦^_^)


呵呵,首先我们来导出EXCEL格式的文件吧。现在主流的操作Excel文件的开源工具有很多,用得比较多的就是Apache的POI及JExcelAPI。这里我们用Apache POI!我们先去Apache的大本营下载POI的jar包:http://poi.apache.org/ ,我这里使用的是3.0.2版本。


将3个jar包导入到classpath下,什么?忘了怎么导包?不会吧!好,我们来写一个导出Excel的实用类(所谓实用,是指基本不用怎么修改就可以在实际项目中直接使用的!)。我一直强调做类也好,做方法也好,一定要通用性和灵活性强。下面这个类就算基本贯彻了我的这种思想。那么,熟悉许老师风格的人应该知道,这时候该要甩出一长串代码了。没错,大伙请看:


Student.java


package org.leno.export.util;


import java.util.Date;


public class Student {
private long id;
private String name;
private int age;
private boolean sex;
private Date birthday;


public Student() {
super();
// TODO Auto-generated constructor stub
}


public Student(long id, String name, int age, boolean sex, Date birthday) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.birthday = birthday;
}


public long getId() {
return id;
}


public void setId(long 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 boolean getSex() {
return sex;
}


public void setSex(boolean sex) {
this.sex = sex;
}


public Date getBirthday() {
return birthday;
}


public void setBirthday(Date birthday) {
this.birthday = birthday;
}


}


Book.java


package org.leno.export.util;


public class Book {
private int bookId;
private String name;
private String author;
private float price;
private String isbn;
private String pubName;
private byte[] preface;


public Book() {
super();
}


public Book(int bookId, String name, String author, float price,
String isbn, String pubName, byte[] preface) {
super();
this.bookId = bookId;
this.name = name;
this.author = author;
this.price = price;
this.isbn = isbn;
this.pubName = pubName;
this.preface = preface;


}


public int getBookId() {
return bookId;
}


public void setBookId(int bookId) {
this.bookId = bookId;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getAuthor() {
return author;
}


public void setAuthor(String author) {
this.author = author;
}


public float getPrice() {
return price;
}


public void setPrice(float price) {
this.price = price;
}


public String getIsbn() {
return isbn;
}


public void setIsbn(String isbn) {
this.isbn = isbn;
}


public String getPubName() {
return pubName;
}


public void setPubName(String pubName) {
this.pubName = pubName;
}


public byte[] getPreface() {
return preface;
}


public void setPreface(byte[] prefac

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android调用JNI方法 及 代码 下一篇Android - JNI静态(static)加载Op..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目