POI导出EXCEL经典实现

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

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

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

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;

}

}

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<

你可能感兴趣的:(apache,Web,工作,Excel,项目管理)