支持pom包
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.1</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.5.10</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
rest代码
@GetMapping(value = "download") public void download(String sqlString, HttpServletResponse response) throws IOException { List<HashMap<String,String>> hashMaps = checkOrderService.invokeSql(sqlString); // Map<String, Object> row1 = new LinkedHashMap<>(); // row1.put("姓名", "张三"); // row1.put("年龄", 23); // row1.put("成绩", 88.32); // row1.put("是否合格", true); // row1.put("考试日期", DateUtil.date()); // // Map<String, Object> row2 = new LinkedHashMap<>(); // row2.put("姓名", "李四"); // row2.put("年龄", 33); // row2.put("成绩", 59.50); // row2.put("是否合格", false); // row2.put("考试日期", DateUtil.date()); ArrayList<Map<String, Object>> rows = (ArrayList) hashMaps; // 通过工具类创建writer,默认创建xls格式 ExcelWriter writer = ExcelUtil.getWriter(); // 一次性写出内容,使用默认样式,强制输出标题 writer.write(rows, true); //out为OutputStream,需要写出到的目标流 //response为HttpServletResponse对象 response.setContentType("application/vnd.ms-excel;charset=utf-8"); //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码 response.setHeader("Content-Disposition","attachment;filename=test.xls"); ServletOutputStream out=response.getOutputStream(); writer.flush(out, true); // 关闭writer,释放内存 writer.close(); //此处记得关闭输出Servlet流 IoUtil.close(out); // checkOrderBlh.backup2(response); }
service
package com.mybatis.plus.service; import com.baomidou.mybatisplus.extension.service.IService; import com.mybatis.plus.entity.order.CheckOrder; import java.util.HashMap; import java.util.List; public interface CheckOrderService extends IService<CheckOrder> { List<HashMap<String,String>> invokeSql(String sqlString); }
serviceImpl
package com.mybatis.plus.service.impl; import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.mybatis.plus.entity.order.CheckOrder; import com.mybatis.plus.mapper.order.CheckOrderMapper; import com.mybatis.plus.service.CheckOrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; @Service public class CheckOrderServiceImpl extends ServiceImpl<CheckOrderMapper, CheckOrder> implements CheckOrderService{ @Autowired private CheckOrderMapper checkOrderMapper; @Override public List<HashMap<String,String>> invokeSql(String sqlString) { return checkOrderMapper.invokeSql(sqlString); } }
mapper
package com.mybatis.plus.mapper.order; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.mybatis.plus.entity.order.CheckOrder; import org.apache.ibatis.annotations.Param; import java.util.HashMap; import java.util.List; import java.util.Map; /** * <p>描述:阜外盘点单表Mapper接口</p> * <p>公司:浙江瑞华康源科技有限公司</p> * <p>版权:rivamed-2019</p> * @author 官昌洪 * @since 2019-10-23 */ public interface CheckOrderMapper extends BaseMapper<CheckOrder> { List<HashMap<String,String>> invokeSql(@Param("sqlString") String sqlString); }
xml
<select id="invokeSql" resultType="java.util.HashMap"> ${sqlString} limit 1000 </select>
效果演示
浏览器url输入 localhost:8092/checkOrder/download?sqlString=select * from t_fhvc_check_order
导出excel如下
注意:存在数据库中null字段不返回的问题 添加mysql配置 call-setters-on-nulls: true 即可解决
mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl jdbc-type-for-null: null call-setters-on-nulls: true mapper-locations: classpath*:/com/mybatis/**/*Mapper.xml