This repository was archived by the owner on Apr 11, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathWriterExample.java
More file actions
165 lines (134 loc) · 5.44 KB
/
Copy pathWriterExample.java
File metadata and controls
165 lines (134 loc) · 5.44 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package io.github.biezhi.excel.plus.examples;
import io.github.biezhi.excel.plus.BaseTest;
import io.github.biezhi.excel.plus.Writer;
import io.github.biezhi.excel.plus.enums.ExcelType;
import io.github.biezhi.excel.plus.exception.WriterException;
import io.github.biezhi.excel.plus.model.Book;
import io.github.biezhi.excel.plus.model.PerformanceTestModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
/**
* @author biezhi
* @date 2018-12-11
*/
@Slf4j
public class WriterExample extends BaseTest {
@Test
public void testWrite100wRows() throws WriterException {
List<PerformanceTestModel> rows = readyData();
long start = System.currentTimeMillis();
writeTestExcel(rows);
long end = System.currentTimeMillis();
log.info("Write " + testCount + " rows, time consuming: " + (end - start) + "ms");
// If you want to open the file view, please comment this line
deleteTempFile(testFileName);
}
@Test
public void testWriteSample() throws WriterException {
String fileName = "sample_test.xlsx";
Writer.create()
.headerTitle("一份简单的Excel表格")
.withRows(buildData())
.to(new File("sample_test.xlsx"));
deleteTempFile(fileName);
}
@Test
public void testWriteCustomStyle() throws WriterException {
String fileName = "sample_custom_style_test.xlsx";
Writer.create()
.headerTitle("一份自定义样式的Excel表格")
.withRows(buildData())
.titleStyle((wb, style) -> {
Font font = wb.createFont();
font.setFontHeightInPoints((short) 40);
font.setColor(HSSFColor.HSSFColorPredefined.RED.getIndex());
style.setFont(font);
return style;
})
.headerStyle((wb, style) -> {
Font font = wb.createFont();
font.setFontHeightInPoints((short) 20);
font.setColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
style.setFont(font);
return style;
})
.cellStyle((wb, style) -> {
Font font = wb.createFont();
font.setFontHeightInPoints((short) 20);
font.setColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());
style.setFont(font);
return style;
})
.to(new File(fileName));
deleteTempFile(fileName);
}
@Test
public void testWriteCustom() throws WriterException {
String fileName = "sample_custom_with_raw.xlsx";
Writer.create()
.withRaw()
.createRow(sheet -> {
sheet.setDefaultColumnWidth(30);
for (int r = 0; r < 3; r++) {
Row row = sheet.createRow(r);
for (int c = 0; c < 5; c++) {
Cell cell = row.createCell(c);
cell.setCellValue("row:" + r + ", cell:" + c);
}
}
})
.to(new File(fileName));
deleteTempFile(fileName);
}
@Ignore
public void testWriteByTemplate() throws WriterException {
String fileName = "sample_custom_with_template.xlsx";
Writer.create()
.withTemplate(classPath() + "/template.xls")
.withRows(buildData())
.to(new File(fileName));
deleteTempFile(fileName);
}
@Test
public void testWriteToOutputStream() throws FileNotFoundException, WriterException {
String fileName = "sample_test_to_stream.xlsx";
Writer.create()
.headerTitle("一份简单的Excel表格")
.withRows(buildData())
.to(new FileOutputStream(new File(fileName)));
deleteTempFile(fileName);
}
@Test
public void testWriteByCSV() throws WriterException {
String fileName = "write_as_csv.csv";
Writer.create(ExcelType.CSV)
.withRows(buildData())
.to(new File(fileName));
deleteTempFile(fileName);
}
@Test
public void testWriteBookXLSX() throws WriterException {
List<Book> books = new ArrayList<>();
books.add(new Book("新名字的故事", "埃莱娜·费兰特", 59.0D, LocalDate.of(2017, 4, 1)));
books.add(new Book("鱼王", "Царь-рыба", 78.0D, LocalDate.of(2017, 4, 1)));
books.add(new Book("不可思议的朋友", "[日] 田岛征彦", 45.0D, LocalDate.of(2017, 7, 1)));
books.add(new Book("杀死一只知更鸟", "[美] 哈珀·李", 48.0D, LocalDate.of(2017, 2, 1)));
books.add(new Book("现代艺术150年", " [英] 威尔·贡培兹", 65.0D, LocalDate.of(2017, 3, 1)));
Writer.create()
.withRows(books)
.headerTitle("书籍列表 V1")
.to(new File("book.xlsx"));
deleteTempFile("book.xlsx");
}
}