微调
This commit is contained in:
@@ -0,0 +1,87 @@
|
|||||||
|
package com.njcn.web.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
|
import com.njcn.common.pojo.exception.BusinessException;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
|
||||||
|
import javax.servlet.ServletOutputStream;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author hongawen
|
||||||
|
* @version 1.0.0
|
||||||
|
* @date 2022年04月01日 20:10
|
||||||
|
*/
|
||||||
|
public class PoiUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据数值返回适当文字描述
|
||||||
|
* 比如:3.14159 - 暂无数据
|
||||||
|
*/
|
||||||
|
public static String dealData(Double value) {
|
||||||
|
return value == 3.14159 ? "暂无数据" : value + "%";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* excel包装数字返回的
|
||||||
|
*/
|
||||||
|
public static String dealPOIData(Double value) {
|
||||||
|
|
||||||
|
return " : (" + dealData(value) + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注意:此方法下载完文件后,会删除当前文件
|
||||||
|
*
|
||||||
|
* @param filePath 文件绝对路径
|
||||||
|
* @param response 响应
|
||||||
|
*/
|
||||||
|
public static void exportFileByAbsolutePath(String filePath, HttpServletResponse response) {
|
||||||
|
File file = new File(filePath);
|
||||||
|
String fileName = file.getName();
|
||||||
|
try (ServletOutputStream outputStream = response.getOutputStream(); FileInputStream fileInputStream = new FileInputStream(file)) {
|
||||||
|
fileName = URLEncoder.encode(fileName, CharsetUtil.UTF_8);
|
||||||
|
response.reset();
|
||||||
|
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
||||||
|
response.setContentType("application/octet-stream;charset=" + CharsetUtil.UTF_8);
|
||||||
|
int len = 0;
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
while ((len = fileInputStream.read(bytes)) != -1) {
|
||||||
|
outputStream.write(bytes, 0, len);
|
||||||
|
outputStream.flush();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new BusinessException(CommonResponseEnum.EXPORT_FILE_ERROR);
|
||||||
|
}
|
||||||
|
FileUtil.del(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按工作蒲导出文件
|
||||||
|
*
|
||||||
|
* @param workbook 工作蒲
|
||||||
|
* @param fileName 文件名
|
||||||
|
* @param response 响应
|
||||||
|
*/
|
||||||
|
public static void exportFileByWorkbook(Workbook workbook, String fileName, HttpServletResponse response) {
|
||||||
|
response.reset();
|
||||||
|
try (ServletOutputStream outputStream = response.getOutputStream()) {
|
||||||
|
fileName = URLEncoder.encode(fileName, CharsetUtil.UTF_8);
|
||||||
|
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
||||||
|
response.setContentType("application/octet-stream;charset=" + CharsetUtil.UTF_8);
|
||||||
|
workbook.write(outputStream);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new BusinessException(CommonResponseEnum.EXPORT_FILE_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user