freemarker export complex style of Excel
Code Address:
https://gitee.com/suveng/demo/tree/master/chapter.002
Code to be placed at the following chapter.002 demo directory, each module is independent springboot open application, application can be run directly
surroundings
- springboot 2.1.2
- Freemarker 2.3.28
- JDK1.8
step
1. Locate the corresponding Excel template
I find the Internet a Web site to download an Excel template, address
The downloaded file is 2018 inventory table
2.Excel template Export to xml format
Export it to xml format; you can directly save the file as
Remove the extra data, the template variable to fill in, this variable is the need to comply with the rules freemarker of the variable;
Details refer to the file
3. Replace variables freemarker
The key modifications:
<#list products as product>
<Row>
<Cell>
<Data ss:Type="String">${product.name!}</Data>
</Cell>
<Cell>
<Data ss:Type="String">${product.number!}</Data>
</Cell>
<Cell>
<Data ss:Type="String">${product.type!}</Data>
</Cell>
<Cell>
<Data ss:Type="String">${product.unit!}</Data>
</Cell>
<Cell>
<Data ss:Type="String">${product.left!}</Data>
</Cell>
<Cell>
<Data ss:Type="String">${product.monthNumber!}</Data>
</Cell>
<Cell>
<Data ss:Type="String">${product.in!}</Data>
</Cell>
<Cell>
<Data ss:Type="String">${product.out!}</Data>
</Cell>
<Cell ss:StyleID="s54">
<Data ss:Type="String">${product.date?string('yyyy/MM/dd')}</Data>
</Cell>
</Row>
</#list>
You can get the file, compare.
Freemarker specific syntax, refer to the links
4. coding, variable substitution
Here I use my own scaffolding, in fact, a quick-start server program, using the built springboot are interested can have a look Links
Write web interface here: Excel export templates
The data here is their analog, meaningless random data is generated using the toolkit randomUtil hutool
AppController.java
@Controller
public class AppController {
@Autowired
private Configuration configuration;
@RequestMapping("/export")
public void export(HttpServletResponse response) throws Exception {
//自己封装号数据实体
ArrayList<Product> products = new ArrayList<>();
//构造数据
for (int i = 0; i < 100; i++) {
Product e = new Product();
e.setName(RandomUtil.randomString(5));
e.setNumber(RandomUtil.randomString(2));
e.setOut(RandomUtil.randomString(2));
e.setIn(RandomUtil.randomString(2));
e.setType(RandomUtil.randomString(5));
e.setUnit(RandomUtil.randomString(4));
e.setMonthNumber(RandomUtil.randomString(1));
e.setDate(new Date());
products.add(e);
}
HashMap<String, Object> map = new HashMap<>();
map.put("products", products);
//构造输出流
Template template = configuration.getTemplate("2018库存表.xml", "UTF-8");
String fileName = "/data/files/" + DateUtil.now() + ".xlsx";
File file = new File(fileName);
FileWriter out = new FileWriter(fileName);
//变量替换
template.process(map, out);
//将文件输出到response,返回给客户端
FileInputStream in = new FileInputStream(file);
byte[] buffer = new byte[in.available()];
in.read(buffer);
in.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=file.xlsx");
ServletOutputStream outputStream = response.getOutputStream();
response.setContentType("application/octet-stream");
outputStream.write(buffer);
outputStream.flush();
outputStream.close();
}
}
5. The results show
There is a problem
- Variable substitution, consuming CPU and memory has not been tested, in the end which is better compared to the POI these components, there is a question here?
Here are just used as a complex pattern of Excel data export, is not suitable for use as a large number of data export .hutool Kit and easyExcel are exporting made the corresponding optimized for large amounts of data Excel, there is a need to see the corresponding document