jxls能把html转成excel吗,JXLS根据模板导出Excel实例教程

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

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

准备:

1、相关jar包:

9a6b164b580eda8be9d8d98f275a39c6.png

2、模板文件 :

890dc6683a2ab568f3b575faae7325f4.png

开始:

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 staffs = new ArrayList();

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> beanParams = new HashMap>();

beanParams.put("staffs", staffs);

XLSTransformer former = new XLSTransformer();

former.transformXLS(srcFilePath, beanParams, destFilePath);

System.out.println("the end !!!");

}

}

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

a78c7b41f7a0f5b74f49b70cbd95c2e8.png

如果是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

2b37d0e77916ec6be9e1c3ad4c8bcd83.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值