欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JXLS根据模板导出Excel实例教程

程序员文章站 2024-03-04 08:54:05
本文实例为大家分享了jxls根据模板导出excel实例的具体方法,供大家参考,具体内容如下 先做模板,做成想要的格式样子保存,然后通过程序根据模板生成对应样式的excel...

本文实例为大家分享了jxls根据模板导出excel实例的具体方法,供大家参考,具体内容如下

先做模板,做成想要的格式样子保存,然后通过程序根据模板生成对应样式的excel文件,代码简单。什么连接数据库查询然后将结果生成excel文件就不讲了,放入list里面,然后套一下就行了,照老虎花猫。

准备:

1、相关jar包:

JXLS根据模板导出Excel实例教程

2、模板文件 :

JXLS根据模板导出Excel实例教程

开始:

1、 先实体类:staff.java

package myjxls;
/**
 * 2014-3-17
 * 8dou
 * 实体
 */
public class staff {
 
  /**
  * 名称
  */
  private string name;
 
  /**
  * 薪资
  */
  private double payment;
 
  /**
  * 年终奖
  */
  private double bonus;
 
  public string getname() {
   return name;
  }
 
  public void setname(string name) {
   this.name = name;
  }
 
  public double getpayment() {
   return payment;
  }
 
  public void setpayment(double payment) {
   this.payment = payment;
  }
 
  public double getbonus() {
   return bonus;
  }
 
  public void setbonus(double bonus) {
   this.bonus = bonus;
  }
  public staff(string name, double payment, double bonus) {
  super();
  this.name = name;
  this.payment = payment;
  this.bonus = bonus;
  }
}

2、测试类 charttest.java 

package myjxls;
/**
 * 2014-3-17
 * 8dou
 * 测试jxls根据模板样式导出excel
 */
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
 
import net.sf.jxls.transformer.xlstransformer;
public class charttest {
 
 /**
  * @param args
  */
 public static void main(string[] args) throws exception {
  list<staff> staffs = new arraylist<staff>();
   
  staff s1 = new staff("张三", 6000d, 3000d);
  staffs.add(s1);
   
  staff s2 = new staff("李四", 5000d, 2000d);
  staffs.add(s2);
   
  staff s3 = new staff("王五", 4000d, 1000d);
  staffs.add(s3);
   
  string srcfilepath = "e:/simple.xlsx";
  string destfilepath = "e:/template-simple.xlsx";
  map<string, list<staff>> beanparams = new hashmap<string, list<staff>>();
  beanparams.put("staffs", staffs);
   
  xlstransformer former = new xlstransformer();
  former.transformxls(srcfilepath, beanparams, destfilepath);
  
  system.out.println("the end !!!");
 }
 
}

运行结束后看生成的excel文件,template-simple.xlsx

JXLS根据模板导出Excel实例教程

如果是web,需要下载可以看

 // 下载
 public static void dodownload(string path, string name,
   httpservletresponse response) {
  try {
   response.reset();
   response.setheader("content-disposition",
     "attachment;success=true;filename ="
       + urlencoder.encode(name, "utf-8"));
   bufferedinputstream bis = null;
   bufferedoutputstream bos = null;
   outputstream fos = null;
   inputstream fis = null;
   file uploadfile = new file(path);
   fis = new fileinputstream(uploadfile);
   bis = new bufferedinputstream(fis);
   fos = response.getoutputstream();
   bos = new bufferedoutputstream(fos);
   // 弹出下载对话框
   int bytesread = 0;
   byte[] buffer = new byte[8192];
   while ((bytesread = bis.read(buffer, 0, 8192)) != -1) {
    bos.write(buffer, 0, bytesread);
   }
   bos.flush();
   fis.close();
   bis.close();
   fos.close();
   bos.close();
  } catch (exception e) {
   e.printstacktrace();
  }
 }

最后补充下excel知识:在单元格里面将日期和时间显示在同一个单元格里面,自定义单元格式→yyyy-m-d hh:mm:ss

JXLS根据模板导出Excel实例教程

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。