1
+ package tech .simter .jxls ;
2
+
3
+ import org .junit .jupiter .api .Test ;
4
+ import org .jxls .common .Context ;
5
+ import org .jxls .util .JxlsHelper ;
6
+
7
+ import java .io .File ;
8
+ import java .io .FileOutputStream ;
9
+ import java .io .InputStream ;
10
+ import java .io .OutputStream ;
11
+ import java .time .OffsetDateTime ;
12
+ import java .time .temporal .ChronoUnit ;
13
+ import java .util .ArrayList ;
14
+ import java .util .List ;
15
+
16
+ import static java .time .temporal .ChronoUnit .SECONDS ;
17
+ import static org .assertj .core .api .Assertions .assertThat ;
18
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
19
+
20
+ /**
21
+ * Excel charts with fixed size collection test.
22
+ *
23
+ * @author RJ
24
+ */
25
+ public class ChartsTest {
26
+ @ SuppressWarnings ("ResultOfMethodCallIgnored" )
27
+ @ Test
28
+ public void test () throws Exception {
29
+ // template
30
+ InputStream template = getClass ().getClassLoader ().getResourceAsStream ("templates/charts.xlsx" );
31
+
32
+ // output to
33
+ File out = new File ("target/charts-result.xlsx" );
34
+ if (out .exists ()) out .delete ();
35
+ OutputStream output = new FileOutputStream (out );
36
+
37
+ // template data
38
+ Context context = new Context ();
39
+ List <Item > rows = new ArrayList <>();
40
+ rows .add (new Item ("Derek" , 3000 , 2000 ));
41
+ rows .add (new Item ("Elsa" , 1500 , 500 ));
42
+ rows .add (new Item ("Oleg" , 2300 , 1300 ));
43
+ rows .add (new Item ("Neil" , 2500 ,1500 ));
44
+ rows .add (new Item ("Maria" , 1700 , 700 ));
45
+ rows .add (new Item ("John" , 2800 , 2000 ));
46
+ rows .add (new Item ("Leonid" , 1700 , 1000 ));
47
+ context .putVar ("items" , rows );
48
+ context .putVar ("title" , "X-Y" );
49
+ context .putVar ("ts" , OffsetDateTime .now ().truncatedTo (SECONDS ).toString ());
50
+
51
+ // render
52
+ // 必须设置 .setEvaluateFormulas(true),否则生成的 Excel 文件,
53
+ // 用到公式的地方不会显示公式的结果,需要双击单元格回车才能看到公式结果。
54
+ JxlsHelper .getInstance ()
55
+ .setEvaluateFormulas (true )
56
+ .processTemplate (template , output , context );
57
+
58
+ // verify
59
+ assertTrue (out .exists ());
60
+ assertThat (out .getTotalSpace ()).isGreaterThan (0 );
61
+ }
62
+
63
+ public static class Item {
64
+ private final String x ;
65
+ private final int y1 ;
66
+ private final int y2 ;
67
+
68
+ public Item (String x , int y1 , int y2 ) {
69
+ this .x = x ;
70
+ this .y1 = y1 ;
71
+ this .y2 = y2 ;
72
+ }
73
+
74
+ public String getX () {
75
+ return x ;
76
+ }
77
+
78
+ public int getY1 () {
79
+ return y1 ;
80
+ }
81
+
82
+ public int getY2 () {
83
+ return y2 ;
84
+ }
85
+ }
86
+ }
0 commit comments