本文实例为大家分享了JXLS根据模板导出Excel实例的具体方法,供大家参考,具体内容如下
先做模板,做成想要的格式样子保存,然后通过程序根据模板生成对应样式的Excel文件,代码简单。什么连接数据库查询然后将结果生成Excel文件就不讲了,放入List里面,然后套一下就行了,照老虎花猫。
准备:
1、相关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
运行结束后看生成的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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。