JXLS根據(jù)模板導(dǎo)出Excel實例教程
本文實例為大家分享了JXLS根據(jù)模板導(dǎo)出Excel實例的具體方法,供大家參考,具體內(nèi)容如下
先做模板,做成想要的格式樣子保存,然后通過程序根據(jù)模板生成對應(yīng)樣式的Excel文件,代碼簡單。什么連接數(shù)據(jù)庫查詢?nèi)缓髮⒔Y(jié)果生成Excel文件就不講了,放入List里面,然后套一下就行了,照老虎花貓。
準備:
1、相關(guān)jar包:

2、模板文件 :

開始:
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根據(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 !!!");
}
}
運行結(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);
// 彈出下載對話框
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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合RabbitMQ及生產(chǎn)全場景高級特性實戰(zhàn)
本文主要介紹了SpringBoot整合RabbitMQ及生產(chǎn)全場景高級特性實戰(zhàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
MyEclipse整合ssh三大框架環(huán)境搭載用戶注冊源碼下載
這篇文章主要為大家詳細介紹了如何使用MyEclipse整合ssh三大框架進行環(huán)境搭載,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
在 Spring Boot 中使用異步線程時的 HttpServletReque
文章討論了在SpringBoot中使用異步線程時,由于HttpServletRequest復(fù)用導(dǎo)致的Cookie解析失敗問題,為了解決這個問題,文章推薦了使用HttpServletRequestWrapper創(chuàng)建請求副本、手動傳遞請求上下文和延遲請求清理等方法,感興趣的朋友一起看看吧2025-03-03

