[ActFramework] 使用act-excel做excel下载

后台+前端都需要处理

  1. pom.xml添加jar包(注意actframework版本,版本太低会不支持act-excel)
  <dependency>
      <groupId>org.actframework</groupId>
      <artifactId>act-excel</artifactId>
      <version>1.9.2</version>
  </dependency>
  1. 前端传参数_accept=xlsx可以直接在页面下载excel,若不添加此参数,则直接在页面显示excel, 这里展示下载excel步骤
  • 后端
// StudentVO
public class StudentVO implements SimpleBean {
	public long id;
	public String name;
	public int age;
	public String score;
	public int rank;
   // 有冗余字段没关系,excel导出哪些字段,由Controller里@PropertySpec控制
   public String sex;
}
// Controller
@GetAction(""exportStu")
@PropertySpec("id as 学生ID,name as 姓名,age as 年龄,totalScore as 总分,rank as 排名")
public List<StudentVO> exportStudent(long stuId) {
	List<StudentVO>  stu;
	// 查询出stu......
	return  stu;
}
  • 前端:<a> <form>都可以,只要能访问到Controller的exportStudent链接就行
// 1、<a>下载
<a href="/exportStu?_accept=xlsx?stuId=12">导出数据</a>

// 2、<form>下载
<form action="/exportStu" method="get">
	<button class="ui button small button_tiny_blue" type="button" onclick="$(form).submit();">导出数据</button>
	<input id="exportTag" type="hidden" name="_accept" value="xlsx"/>
	<input name="stuId" value="12" />
	//其他参数......
</form>

重要说明:
有时候只是查询,并不想下载excel,可以给_accept字段的<input>增加disabled属性,如下所示:

 <input id="exportTag" type="hidden" name="_accept" value="xlsx" disabled/>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值