91名师指路-头部
91名师指路

jxls(一):jxls简单的使用

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

简介:导出excel的方式有多种,如:poi,easy-poi,easyexcel,jxls等等,如果对于复杂的样式,颜色,格式等等,采用传统的导出方式,将会变得异常的复杂,而且代码将难以维护,jxls的出现解决了这个难题。


官网:https://jxls.sourceforge.net/index.html

github:https://github.com/jxlsteam/jxls


一:引入pom

<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:用于排序 

 


四:请求接口下载excel

4.1)请求接口:http://localhost:8002/jxls/export


4.2)下载的excel效果图





2023-06-26 11:03:52     阅读(801)

名师出品,必属精品    https://www.91mszl.com

用户登录遮罩层
x

账号登录

91名师指路-底部