导出html文件数据
文章目录
- 导出html文件数据
- 前言
- 一、实现代码
- 1.controller层
- 2.接口层
- 3.接口实现类
- 4.FileUtil 工具类
- 二、文件导出效果
- 总结
前言
springBoot项目实现在线导出html文件数据的功能。
一、实现代码
1.controller层
@GetMapping("/record/_export")
@ApiOperation("查询记录导出")
public void recordExport(HttpServletResponse response, RecordSearchRequest query) throws IOException {
ruleVerifyResultService.recordExport(response, query);
}
RecordSearchRequest为请求查询条件,根据条件导出对应的记录。
2.接口层
public interface ResultService extends IService<RuleVerifyResultEntity> {
/**
* 记录导出
* @param query
* @return
*/
void recordExport(HttpServletResponse response, RecordSearchRequest query) throws IOException;
}
3.接口实现类
@Override
public void queryRecordExport(HttpServletResponse response, RuleVerifyRecordSearchRequest query) throws IOException {
LocalDateTime startExecuteTime = null;
LocalDateTime endExecuteTime = null;
//设置导出文件名称
String fileName = "检验记录列表数据";
if (ObjectUtils.isNotEmpty(query.getExecuteStartTime())) {
startExecuteTime = query.getExecuteStartTime().atStartOfDay();
endExecuteTime = query.getExecuteStartTime().atTime(23, 59, 59);
}
//查询需要导出的数据,用list集合接收
List<RuleVerifyRecordEntity> recordList = ruleVerifyRecordMapper.selectList(Wrappers.<RuleVerifyRecordEntity>lambdaQuery().
// 关键字搜索
and(StringUtils.isNotEmpty(query.getKeywords()), wrapper ->
wrapper.like(RuleVerifyRecordEntity::getTableName, query.getKeywords()).
or().like(RuleVerifyRecordEntity::getTableRemark, query.getKeywords()).
or().like(RuleVerifyRecordEntity::getDatasourceName, query.getKeywords())
)
orderByDesc(RuleVerifyRecordEntity::getExecuteStartTime)
);
//2.导出html数据
if (ObjectUtils.equals(ExportTypeEnum.HTML.getValue(), query.getExportType())) {
//构建文件信息
StringBuilder table = this.buildTableInfo(recordList);
InputStream inputStream = null;
FileInputStream fileInputStream = null;
ServletOutputStream outputStream = null;
try {
//string字符串转输入流
inputStream = FileUtil.getInputStream(table.toString());
//字节输入流转文件输入流
fileInputStream = FileUtil.convertToFileInputStream(inputStream);
//给浏览器设置响应头:Content-Disposition告诉浏览器以附件的形式打开这个文件
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".html", "UTF-8"));
//给浏览器设置响应头:文件类型
response.setContentType("application/octet-stream;charset=utf-8");
//从response中获取输出流
outputStream = response.getOutputStream();
//进行流拷贝操作
FileUtil.copyStream(fileInputStream, outputStream);
outputStream.flush();
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
} finally {
//关闭资源文件
try{
inputStream.close();
fileInputStream.close();
outputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
private StringBuilder buildTableInfo(List<RuleVerifyRecordEntity> recordList){
StringBuilder table = new StringBuilder();
// 添加table标签
table.append("<table>\n");
// 生成表头
table.append("<tr>\n");
table.append("<th>" + "表名称" + "</th>");
table.append("<th>" + "规则异常" + "</th>");
table.append("<th>" + "执行状态" + "</th>");
table.append("<th>" + "调度信息" + "</th>");
table.append("<th>" + "开始时间" + "</th>");
table.append("<th>" + " " + "</th>\n");
table.append("<th>" + "结束时间" + "</th>");
table.append("</tr>\n");
// 生成HTML表格
// 生成表格数据
for (RuleVerifyRecordEntity row : recordList) {
table.append("<tr>\n");
table.append("<td>" + row.getTableName() + "</td>\n");
table.append("<td>" + row.getRuleCount()) + "</td>\n");
table.append("<td>" + (ObjectUtils.isNotEmpty(row.getExecuteStatus()) ? row.getExecuteStatus().toString() : null) + "</td>\n");
table.append("<td>" + row.getSchedulerInfo() + " " + "</td>\n");
table.append("<td>" + DateTimeUtil.toDateTimeStr(ObjectUtils.isNotEmpty(row.getExecuteStatus()) ? row.getExecuteStartTime() : LocalDateTime.now()) + "</td>\n");
table.append("<td>" + " " + "</td>\n");
table.append("<td>" + DateTimeUtil.toDateTimeStr(ObjectUtils.isNotEmpty(row.getExecuteEndTime()) ? row.getExecuteEndTime() : LocalDateTime.now()) + "</td>\n");
table.append("</tr>\n");
}
table.append("</table>");
return table;
}
}
4.FileUtil 工具类
import javax.servlet.ServletOutputStream;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class FileUtil {
//string字符串转输入流
public static InputStream getInputStream(String str) {
if (str != null && !"".equals(str.trim())) {
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
return inputStream;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
//字节输入流转文件输入流
public static FileInputStream convertToFileInputStream(InputStream inputStream) throws IOException {
File tempFile = File.createTempFile("temp", ".tmp");
tempFile.deleteOnExit();
try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
return new FileInputStream(tempFile);
}
//数据写入输出流
public static void copyStream(FileInputStream fileInputStream, ServletOutputStream outputStream) throws IOException {
int len = 0;
byte[] arr = new byte[1024];
while ((len = fileInputStream.read(arr)) != -1) {
outputStream.write(arr, 0, len);
}
}
}
二、文件导出效果
总结
本次实现了html文件的导出功能,下节接着实现xml文件的导出功能。