JXLS根據(jù)模板導(dǎo)出Excel實(shí)例教程
本文實(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)文章
springboot自動(dòng)裝配TypeNotPresentExceptionProxy異常排查解決
這篇文章主要為大家介紹了springboot自動(dòng)裝配TypeNotPresentExceptionProxy異常排查解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Java中關(guān)于char類型變量能夠輸出中文的問(wèn)題
這篇文章主要介紹了Java中關(guān)于char類型變量能夠輸出中文的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12詳解SpringBoot啟動(dòng)代碼和自動(dòng)裝配源碼分析
這篇文章主要介紹了SpringBoot啟動(dòng)代碼和自動(dòng)裝配源碼分析,使用SpringBoot很簡(jiǎn)單,在主類中添加一個(gè)@SpringBootApplication,以及調(diào)用SpringApplication.run()并傳入主類,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07Spring CGLlB動(dòng)態(tài)代理實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了Spring CGLlB動(dòng)態(tài)代理實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10SpringCloud分布式項(xiàng)目下feign的使用示例詳解
這篇文章主要介紹了SpringCloud分布式項(xiàng)目下feign的使用,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07java 基礎(chǔ)知識(shí)之網(wǎng)絡(luò)通信(TCP通信、UDP通信、多播以及NIO)總結(jié)
這篇文章主要介紹了java 基礎(chǔ)知識(shí)之網(wǎng)絡(luò)通信總結(jié)的相關(guān)資料,包括TCP通信、UDP通信、多播以及NIO,需要的朋友可以參考下2017-03-03Java實(shí)現(xiàn)平鋪列表(List)互轉(zhuǎn)樹(shù)形(Tree)結(jié)構(gòu)
本文主要介紹了Java實(shí)現(xiàn)平鋪列表(List)互轉(zhuǎn)樹(shù)形(Tree)結(jié)構(gòu),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08