歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> POI實現導出EXCEL詳解

POI實現導出EXCEL詳解

日期:2017/3/1 9:38:38   编辑:Linux編程

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

POI 的詳細介紹:請點這裡
POI 的下載地址:請點這裡

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[] preface) {
this.preface = preface;
}

}

上面這兩個類一目了然,就是兩個簡單的javabean風格的類。再看下面真正的重點類:

Copyright © Linux教程網 All Rights Reserved