本文实例为大家分享了jxls根据模板导出excel实例的具体方法,供大家参考,具体内容如下
先做模板,做成想要的格式样子保存,然后通过程序根据模板生成对应样式的excel文件,代码简单。什么连接数据库查询然后将结果生成excel文件就不讲了,放入list里面,然后套一下就行了,照老虎花猫。
准备:
1、相关jar包:
2、模板文件 :
开始:
1、 先实体类:staff.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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<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 !!!" ); } } |
运行结束后看生成的excel文件,template-simple.xlsx
如果是web,需要下载可以看
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
// 下载 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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/zhao50632/article/details/21397685