初始化
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package com.njcn.poi.excel;
|
||||
|
||||
import cn.afterturn.easypoi.excel.ExcelExportUtil;
|
||||
import cn.afterturn.easypoi.excel.entity.ExportParams;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import com.njcn.web.utils.HttpServletUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2022年04月01日 10:48
|
||||
*/
|
||||
@Slf4j
|
||||
public class ExcelUtil {
|
||||
|
||||
/**
|
||||
* 指定名称、数据下载报表
|
||||
*
|
||||
* @param fileName 文件名
|
||||
*/
|
||||
public static void exportExcel(String fileName, Class<?> pojoClass, Collection<?> dataSet) {
|
||||
HttpServletResponse response = HttpServletUtil.getResponse();
|
||||
try (ServletOutputStream outputStream = response.getOutputStream()) {
|
||||
fileName = URLEncoder.encode(fileName, CharsetUtil.UTF_8);
|
||||
response.reset();
|
||||
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
||||
response.setContentType("application/octet-stream;charset=UTF-8");
|
||||
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), pojoClass, dataSet);
|
||||
workbook.write(outputStream);
|
||||
} catch (IOException e) {
|
||||
log.error(">>> 导出数据异常:{}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param excel 目标excel
|
||||
* @param statisticsType 统计类型
|
||||
* @param statisticsName 统计名称
|
||||
* @param pojoClass 目标class
|
||||
* @param dataSet 数据集合
|
||||
*/
|
||||
public static void exportExcelWithTargetFile(File excel, String statisticsType, String statisticsName, Class<?> pojoClass, Collection<?> dataSet) {
|
||||
try (FileOutputStream outputStream = new FileOutputStream(excel)) {
|
||||
if (!excel.getParentFile().exists()) {
|
||||
excel.getParentFile().mkdirs();
|
||||
}
|
||||
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(statisticsType, statisticsName), pojoClass, dataSet);
|
||||
workbook.write(outputStream);
|
||||
} catch (IOException e) {
|
||||
log.error(">>> 导出数据异常:{}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param excel 目标excel
|
||||
* @param pojoClass 目标class
|
||||
* @param dataSet 数据集合
|
||||
*/
|
||||
public static void exportExcelWithTargetFile(File excel, Class<?> pojoClass, Collection<?> dataSet) {
|
||||
try (FileOutputStream outputStream = new FileOutputStream(excel)) {
|
||||
if (!excel.getParentFile().exists()) {
|
||||
excel.getParentFile().mkdirs();
|
||||
}
|
||||
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), pojoClass, dataSet);
|
||||
workbook.write(outputStream);
|
||||
} catch (IOException e) {
|
||||
log.error(">>> 导出数据异常:{}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.njcn.poi.pojo.bo;
|
||||
|
||||
import cn.afterturn.easypoi.excel.annotation.Excel;
|
||||
import com.njcn.poi.pojo.constant.DeviceInfoConstant;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2022年03月31日 16:28
|
||||
*/
|
||||
@Data
|
||||
public class BaseLineExcelBody implements Serializable {
|
||||
|
||||
@Excel(name = "统计名称", width = 20, mergeVertical = true)
|
||||
private String statisticsName;
|
||||
|
||||
@Excel(name = DeviceInfoConstant.UNIT_NAME, width = 25, mergeVertical = true)
|
||||
private String gdName;
|
||||
|
||||
@Excel(name = DeviceInfoConstant.DEPARTMENT_NAME, width = 25, mergeVertical = true)
|
||||
private String subName;
|
||||
|
||||
@Excel(name = DeviceInfoConstant.DEPARTMENT_NAME_SCALE, width = 20)
|
||||
private String subScale;
|
||||
|
||||
@Excel(name = "终端名称", width = 15, mergeVertical = true)
|
||||
private String deviceName;
|
||||
|
||||
@Excel(name = "网络参数", width = 15, mergeVertical = true)
|
||||
private String networkParam;
|
||||
|
||||
@Excel(name = "通讯状态", replace = {"中断_0", "正常_1"}, width = 15)
|
||||
private String comState;
|
||||
|
||||
@Excel(name = "终端厂家", width = 15)
|
||||
private String factoryName;
|
||||
|
||||
@Excel(name = "最新更新数据时间", width = 18)
|
||||
private String time;
|
||||
|
||||
@Excel(name = "母线电压等级", width = 15, mergeVertical = true)
|
||||
private String subvScale;
|
||||
|
||||
@Excel(name = "监测点名称", width = 20)
|
||||
private String lineName;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.njcn.poi.pojo.bo;
|
||||
|
||||
import cn.afterturn.easypoi.excel.annotation.Excel;
|
||||
import com.njcn.poi.pojo.constant.DeviceInfoConstant;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2022年04月01日 15:07
|
||||
*/
|
||||
@Data
|
||||
public class BaseLineProExcelBody implements Serializable {
|
||||
|
||||
@Excel(name = "统计名称", width = 20, mergeVertical = true)
|
||||
private String statisticsName;
|
||||
|
||||
@Excel(name = DeviceInfoConstant.PROJECT_NAME, width = 25, mergeVertical = true)
|
||||
private String provincialName;
|
||||
|
||||
@Excel(name = DeviceInfoConstant.UNIT_NAME, width = 25, mergeVertical = true)
|
||||
private String gdName;
|
||||
|
||||
@Excel(name = DeviceInfoConstant.DEPARTMENT_NAME, width = 25, mergeVertical = true)
|
||||
private String subName;
|
||||
|
||||
@Excel(name = DeviceInfoConstant.DEPARTMENT_NAME_SCALE, width = 20)
|
||||
private String subScale;
|
||||
|
||||
@Excel(name = "终端名称", width = 15, mergeVertical = true)
|
||||
private String deviceName;
|
||||
|
||||
@Excel(name = "网络参数", width = 15, mergeVertical = true)
|
||||
private String networkParam;
|
||||
|
||||
@Excel(name = "通讯状态", replace = {"中断_0", "正常_1"}, width = 15)
|
||||
private String comState;
|
||||
|
||||
@Excel(name = "终端厂家", width = 15)
|
||||
private String factoryName;
|
||||
|
||||
@Excel(name = "最新更新数据时间", width = 18)
|
||||
private String time;
|
||||
|
||||
@Excel(name = "母线电压等级", width = 15, mergeVertical = true)
|
||||
private String subvScale;
|
||||
|
||||
@Excel(name = "监测点名称", width = 25)
|
||||
private String lineName;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.njcn.poi.pojo.constant;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2022年03月31日 18:39
|
||||
*/
|
||||
public interface DeviceInfoConstant {
|
||||
|
||||
/**
|
||||
* 【工程名称】-【单位】-【部门】
|
||||
* 在数据中心、电网省公司为:【省级】-【供电公司】-【变电站】
|
||||
*
|
||||
* 企业用户:【企业】-【区域】-【单位】
|
||||
**/
|
||||
String PROJECT_NAME="省级";
|
||||
String UNIT_NAME = "供电公司";
|
||||
String DEPARTMENT_NAME = "变电站";
|
||||
String DEPARTMENT_NAME_SCALE = "变电站电压等级";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.njcn.poi.util;
|
||||
|
||||
import cn.afterturn.easypoi.excel.ExcelExportUtil;
|
||||
import cn.afterturn.easypoi.excel.entity.ExportParams;
|
||||
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.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @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