欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JXLS根據(jù)模板導(dǎo)出Excel實(shí)例教程

 更新時(shí)間:2018年12月15日 11:58:05   作者:南山猛士  
這篇文章主要為大家詳細(xì)介紹了JXLS根據(jù)模板導(dǎo)出Excel實(shí)例教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了JXLS根據(jù)模板導(dǎo)出Excel實(shí)例的具體方法,供大家參考,具體內(nèi)容如下

先做模板,做成想要的格式樣子保存,然后通過(guò)程序根據(jù)模板生成對(duì)應(yīng)樣式的Excel文件,代碼簡(jiǎn)單。什么連接數(shù)據(jù)庫(kù)查詢?nèi)缓髮⒔Y(jié)果生成Excel文件就不講了,放入List里面,然后套一下就行了,照老虎花貓。

準(zhǔn)備:

1、相關(guān)jar包:

2、模板文件 :

開(kāi)始:

1、 先實(shí)體類:Staff.java

package myjxls;
/**
 * 2014-3-17
 * 8dou
 * 實(shí)體
 */
public class Staff {
 
  /**
  * 名稱
  */
  private String name;
 
  /**
  * 薪資
  */
  private Double payment;
 
  /**
  * 年終獎(jiǎng)
  */
  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、測(cè)試類 ChartTest.java 

package myjxls;
/**
 * 2014-3-17
 * 8dou
 * 測(cè)試JXLS根據(jù)模板樣式導(dǎo)出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 !!!");
 }
 
}

運(yùn)行結(jié)束后看生成的Excel文件,template-simple.xlsx

如果是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);
   // 彈出下載對(duì)話框
   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();
  }
 }

最后補(bǔ)充下Excel知識(shí):在單元格里面將日期和時(shí)間顯示在同一個(gè)單元格里面,自定義單元格式→yyyy-m-d hh:mm:ss

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論