原 jxls(一):jxls简单的使用
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1439
官网:https://jxls.sourceforge.net/index.html
github:https://github.com/jxlsteam/jxls
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>2.12.0</version>
</dependency>
如果需要基于Apache POI API进行转换需要引入
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>2.12.0</version>
</dependency>
如果需要基于Java Excel API进行转换需要引入
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-jexcel</artifactId>
<version>1.0.9</version>
</dependency>
package com.mszl.blog.controller;
import com.mszl.blog.entity.User;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/jxls")
public class TestJxlsController {
@GetMapping("/export")
public void testExport(HttpServletResponse response) throws IOException {
List<User> ageList = new ArrayList<>();
User s1=new User();
s1.setName("张三");
s1.setNickName("张3");
s1.setPhone("13111111111");
User s2=new User();
s2.setName("李四");
s2.setNickName("李4");
s2.setPhone("13222222222");
ageList.add(s1);
ageList.add(s2);
String templatePath = "E:/data/excel/666.xlsx";
File file = new File(templatePath);
FileInputStream is = new FileInputStream(file);
String filePath= System.currentTimeMillis() + ".xlsx";
response.reset();
response.addHeader("Content-Disposition", "attachment; filename=\"" + filePath + "\"");
OutputStream os=response.getOutputStream();
Context context = new Context();
context.putVar("ageList", ageList);
JxlsHelper.getInstance().processTemplate(is, os, context);
os.close();
}
}
3.1)选中A1区域,点击右键,插入批注。
3.2)插入公式:jx:area(lastCell="C2") 表示单元格从A1到C2
jx:area官网解释:
XLS Area is a major concept in JxlsPlus. Basically it represents a rectangular area in an Excel file which needs to be transformed. Each XLS Area may have a list of transformation Commands associated with it and a set of nested child areas. Each child area is also an XLS Area with its own set of Commands and nested areas. A top-level XLS Area is an area which does not have a parent area (it is not nested into any other XLS Area)
我自己的理解:jx:area是JxlsPlus中的一个主要概念,他是一个顶级元素不能被其他元素所包含。
3.3)选中A2,区域,插入批注,插入公式:jx:each(items="ageList" var="a" lastCell="C2")
items:表示要遍历的集合
var:变量的名称
lastCell:最后一个单元格的位置
direction:表示循环的方向,属性值为DOWN或RIGHT,默认为:DOWN
groupBy:用于分组
groupOrder:用于分组排序
orderBy:用于排序
4.1)请求接口:http://localhost:8002/jxls/export
4.2)下载的excel效果图
2023-06-26 11:03:52 阅读(800)
名师出品,必属精品 https://www.91mszl.com
博主信息