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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值