Commit fc9cb9c9 authored by yanzg's avatar yanzg

导出数据

parent 6458d22a
......@@ -2,13 +2,12 @@ package com.yanzuoguang.cloud.excel;
import com.yanzuoguang.cloud.helper.HttpFileHelper;
import com.yanzuoguang.excel.ExcelConsole;
import com.yanzuoguang.excel.ExcelRow;
import com.yanzuoguang.excel.ExportBase;
import com.yanzuoguang.excel.ExportData;
import com.yanzuoguang.excel.*;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.thread.ThreadHelper;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
/**
......@@ -43,7 +42,7 @@ public class ExcelHttp<T extends Object> extends ExcelConsole<T> {
*
* @param response 输出流
*/
public ExcelHttp down(HttpServletResponse response) throws IOException {
public boolean down(HttpServletResponse response) throws IOException {
return this.down(response, StringHelper.EMPTY);
}
......@@ -53,15 +52,29 @@ public class ExcelHttp<T extends Object> extends ExcelConsole<T> {
* @param response 输出流
* @param downFileName 下载文件名
*/
public ExcelHttp down(HttpServletResponse response, String downFileName) throws IOException {
public boolean down(HttpServletResponse response, String downFileName) throws IOException {
if (StringHelper.isEmpty(downFileName)) {
downFileName = this.getConfig().getDownFileName();
}
if (StringHelper.isEmpty(downFileName)) {
downFileName = this.getConfig().getFileName();
}
// 正式文件
String fileName = this.getFileName();
File file = new File(fileName);
// 临时文件
String fileNameTemp = this.getFileNameTemp();
File fileTemp = new File(fileNameTemp);
boolean isDown = file.exists();
if (isDown) {
HttpFileHelper.localToDown(this.getFileName(), downFileName, response);
return this;
} else {
ExportTempRes res = new ExportTempRes();
res.setFileName(fileName);
res.setSize(fileTemp.length());
}
return isDown;
}
/**
......@@ -79,27 +92,48 @@ public class ExcelHttp<T extends Object> extends ExcelConsole<T> {
if (StringHelper.isEmpty(config.getServerPath())) {
config.setServerPath("/home/cache/");
}
if (StringHelper.isEmpty(config.getFileName())) {
boolean isEmptyFile = StringHelper.isEmpty(config.getFileName());
if (isEmptyFile) {
config.setFileName(String.format("%s.xlsx", StringHelper.getNewID()));
}
if (StringHelper.isEmpty(config.getDownFileName())) {
config.setDownFileName(String.format("%s-%s.xlsx", config.getTitle(), config.getSubTitle()));
}
ExcelHttp<M> excel = new ExcelHttp<>(config);
boolean isDown = false;
try {
excel.open();
excelDao.export(excel, req.getCond());
if (excelDao instanceof ExcelDaoFinish) {
((ExcelDaoFinish<T, M>) excelDao).finish(excel);
if (config.getExportType() == ExportData.EXPORT_TYPE_COMMON) {
// 普通模式生成文件并下载
releaseExcel(req, excelDao, excel);
} else if (isEmptyFile) {
// 等待下载模式,开启线程生成文件
ThreadHelper.runThread(new Runnable() {
@Override
public void run() {
releaseExcel(req, excelDao, excel);
}
excel.save();
excel.down(response);
});
}
// 等待下载模式,下载文件
isDown = excel.down(response);
} catch (IOException e) {
isDown = true;
e.printStackTrace();
throw new RuntimeException("下载失败", e);
} finally {
if (isDown) {
// 删除生成的临时文件
excel.remove();
}
}
}
private static <T, M> void releaseExcel(ExportBase<T> req, ExcelDao<T, M> excelDao, ExcelHttp<M> excel) {
excel.open();
excelDao.export(excel, req.getCond());
if (excelDao instanceof ExcelDaoFinish) {
((ExcelDaoFinish<T, M>) excelDao).finish(excel);
}
excel.save();
}
}
......@@ -372,7 +372,9 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
}
try {
OutputStream out = new FileOutputStream(this.getFileName());
String fileNameTemp = this.getFileNameTemp();
// 保存为临时文件
OutputStream out = new FileOutputStream(fileNameTemp);
try {
workbook.write(out);
out.flush();
......@@ -382,10 +384,14 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
} finally {
out.close();
}
File file = new File(this.getFileName());
// 判断临时文件是否存在
File file = new File(fileNameTemp);
if (!file.exists()) {
throw new RuntimeException("保存路径" + this.getFileName() + "失败");
}
// 重命名成正式文件
String fileName = this.getFileName();
file.renameTo(new File(fileName));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("保存失败");
......@@ -422,6 +428,15 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
return fileName;
}
/**
* 获取保存文件名(全路径)
*
* @return 文件名
*/
public String getFileNameTemp() {
return getFileName() + ".tmp";
}
/**
* 创建单元格
*
......
......@@ -12,19 +12,6 @@ import io.swagger.annotations.ApiModelProperty;
*/
@ApiModel(description = "订单导出基本请求信息")
public class ExportBase<T extends Object> extends BaseVo {
/**
* 导出类型,0-实时导出,1-等待生成文件后下载(该类型需要请求多次:
* 第一次请求会返回导出路径,后续几次会返回正在生成中,并且返回文件的大小,当生成完成后最后一次会下载文件)
*/
@ApiModelProperty(notes = "导出类型,0-实时导出,1-等待生成文件后下载(该类型需要请求多次:" +
"第一次请求会返回导出路径,后续几次会返回正在生成中,并且返回文件的大小,当生成完成后最后一次会下载文件)", required = true)
private int ExportType;
/**
* 导出下载文件的服务器路径、第一次导出不需要传。在到处类型为1时有效
*/
@ApiModelProperty(notes = "导出下载文件的服务器路径、第一次导出不需要传。在到处类型为1时有效")
private String ExportPath;
/**
* 导出格式
*/
......@@ -36,22 +23,6 @@ public class ExportBase<T extends Object> extends BaseVo {
@ApiModelProperty(notes = "查询条件", required = true)
private T cond;
public int getExportType() {
return ExportType;
}
public void setExportType(int exportType) {
ExportType = exportType;
}
public String getExportPath() {
return ExportPath;
}
public void setExportPath(String exportPath) {
ExportPath = exportPath;
}
public ExportData getConfig() {
return config;
}
......
......@@ -14,6 +14,12 @@ import java.util.List;
@ApiModel(description = "订单导出数据配置信息")
public class ExportData {
@ApiModelProperty(notes = "普通导出模式")
public static final int EXPORT_TYPE_COMMON = 0;
@ApiModelProperty(notes = "等待下载模式")
public static final int EXPORT_TYPE_WAIT_DOWN = 1;
@ApiModelProperty(notes = "行默认高度")
public static final short ROW_HEIGHT_UNIT = 10;
......@@ -31,25 +37,28 @@ public class ExportData {
@ApiModelProperty(notes = "列默认宽度")
public static final short COLUMN_WIDTH = 0;
/**
* 导出类型,0-实时导出,1-等待生成文件后下载(该类型需要请求多次:
* 第一次请求会返回导出路径,后续几次会返回正在生成中,并且返回文件的大小,当生成完成后最后一次会下载文件)
*/
@ApiModelProperty(notes = "导出类型,0-实时导出,1-等待生成文件后下载(该类型需要请求多次:" +
"第一次请求会返回导出路径,后续几次会返回正在生成中,并且返回文件的大小,当生成完成后最后一次会下载文件)", required = true)
private int ExportType;
/**
* 服务器保存路径
*/
@ApiModelProperty(notes = "服务器保存路径")
private String serverPath;
/**
* 文件名
*/
@ApiModelProperty(notes = "文件名")
@ApiModelProperty(notes = "文件名,在为实时导出时,可以不传输文件名.在为等待下载模式时,第一次保存时可以不传文件名,会自动生成,但是后续需要传输文件名")
private String fileName;
/**
* 下载文件名
*/
@ApiModelProperty(notes = "下载文件名")
private String downFileName;
/**
* 标题
*/
......@@ -92,6 +101,15 @@ public class ExportData {
@ApiModelProperty(notes = "包含列")
private List<ExportColumn> columns = new ArrayList<>();
public int getExportType() {
return ExportType;
}
public void setExportType(int exportType) {
ExportType = exportType;
}
public String getServerPath() {
return serverPath;
}
......
package com.yanzuoguang.excel;
import com.yanzuoguang.util.vo.BaseVo;
import io.swagger.annotations.ApiModelProperty;
/**
* 导出文件实时大小
*
* @author 颜佐光
*/
public class ExportTempRes extends BaseVo {
/**
* 文件名
*/
@ApiModelProperty(notes = "文件名,在为实时导出时,可以不传输文件名.在为等待下载模式时,第一次保存时可以不传文件名,会自动生成,但是后续需要传输文件名")
private String fileName;
/**
* 已生成文件大小
*/
@ApiModelProperty(notes = "已生成文件大小")
private long size;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment