Skip to content

Commit 70fafca

Browse files
committedJan 11, 2022
Add jxls charts template sample
1 parent 29d1844 commit 70fafca

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed
 
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.util.ArrayList;
12+
import java.util.List;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
16+
17+
/**
18+
* Excel formula test.
19+
*
20+
* @author RJ
21+
*/
22+
public class FormulaTest {
23+
@Test
24+
public void test() throws Exception {
25+
// template
26+
InputStream template = getClass().getClassLoader().getResourceAsStream("templates/formula.xlsx");
27+
28+
// output to
29+
File out = new File("target/formula-result.xlsx");
30+
if (out.exists()) out.delete();
31+
OutputStream output = new FileOutputStream(out);
32+
33+
// template data
34+
Context context = new Context();
35+
List<Integer> rows = new ArrayList<>();
36+
rows.add(1);
37+
rows.add(2);
38+
rows.add(3);
39+
context.putVar("rows", rows);
40+
context.putVar("title", "测试");
41+
42+
// render
43+
// 必须设置 .setEvaluateFormulas(true),否则生成的 Excel 文件,
44+
// 用到公式的地方不会显示公式的结果,需要双击单元格回车才能看到公式结果。
45+
JxlsHelper.getInstance()
46+
.setEvaluateFormulas(true)
47+
.processTemplate(template, output, context);
48+
49+
// verify
50+
assertTrue(out.exists());
51+
assertThat(out.getTotalSpace()).isGreaterThan(0);
52+
}
53+
}
16.2 KB
Binary file not shown.
11 KB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.