导出大文件到excel(1)

news/2024/7/21 5:30:08 标签: excel, 导入导出

excel_0">导出大文件到excel

  1. 实体类
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_log")
public class Log implements Serializable {
	private static final long serialVersionUID = 1L;

	@TableId
	private Long id;

	/**
	 * 日志分类(1-系统日志)
	 */
	@Excel(name = "日志分类", replace = {"系统_1", "业务_2", "审计_3", "安全_4", "告警_5", "登录_6"},
			suffix = "日志", isImportField = "true_st")
	@ApiModelProperty(value = "日志分类")
	private Integer logCategory;

	/**
	 * 日志类别(1-信息   2-警告)
	 */
	@Excel(name = "日志类别", replace = {"信息_1", "警告_2"}, isImportField = "true_st")
	@ApiModelProperty(value = "日志类别")
	private Integer logType;

	/**
	 * 事件类型(1-系统事件  2-业务事件)
	 */
	@Excel(name = "事件类型", replace = {"系统事件_1", "业务事件_2"}, isImportField = "true_st")
	@ApiModelProperty(value = "事件类型")
	private Integer eventType;
	/**
	 * 日志级别(1-高   2-中  3-低)
	 */
	@Excel(name = "日志级别", replace = {"高_1", "中_2", "低_3"}, isImportField = "true_st")
	@ApiModelProperty(value = "日志级别")
	private Integer logLevel;
	/**
	 * 操作类型
	 */
	@Excel(name = "操作类型描述",isImportField = "true_st")
	@ApiModelProperty(value = "操作类型描述")
	private String operation;
	/**
	 * 操作用户名
	 */
	@Excel(name = "用户名", isImportField = "true_st")
	@ApiModelProperty(value = "用户名")
	private String userName;
	/**
	 * 客户端请求IP地址
	 */
	@Excel(name = "客户端IP", width = 25, isImportField = "true_st")
	@ApiModelProperty(value = "客户端IP")
	private String clientIp;
	/**
	 * 日志请求地址
	 */
	@Excel(name = "请求路径", width = 50, isImportField = "true_st")
	@ApiModelProperty(value = "日志请求地址")
	private String uri;
	/**
	 * 请求方式method,post,get等
	 */
	@Excel(name = "请求方式", isImportField = "true_st")
	@ApiModelProperty(value = "请求方式")
	private String method;
	/**
	 * 请求参数内容,json
	 */
	@ApiModelProperty(value = "请求参数内容")
	private String paramData;

	/**
	 * 请求接口唯一session标识'
	 */
	@ApiModelProperty(value = "请求接口唯一session标识")
	private String sessionId;
	/**
	 * 接口返回时间
	 */
	@ApiModelProperty(value = "接口返回时间")
	private String returmTime;
	/**
	 * 接口返回数据json
	 */
	@ApiModelProperty(value = "接口返回数据json")
	private String returnData;
	/**
	 * 请求时httpStatusCode代码,如:200,400,404等
	 */
	@ApiModelProperty(value = "请求时httpStatusCode代码")
	private String httpStatusCode;
	/**
	 * 请求耗时(毫秒单位)
	 */
	@Excel(name = "响应时间(毫秒)", suffix = "ms", isImportField = "true_st")
	@ApiModelProperty(value = "响应时间(毫秒)")
	private Integer timeConsuming;
	/**
	 * 请求结果(1-成功   2-失败)
	 */
	@Excel(name = "请求结果", replace = {"成功_1", "失败_2"}, isImportField = "true_st")
	@ApiModelProperty(value = "请求结果")
	private Integer requestResult;
	/**
	 * 创建时间
	 */
	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	@ApiModelProperty(value = "创建时间")
	private Date createTime;

	/**
	 * 浏览器
	 */
	private String browser;
	/**
	 * 操作系统
	 */
	private String os;
	/**
	 * 用户代理
	 */
	private String userAgent;
	/**
	 * 备注
	 */
	@ApiModelProperty(value = "备注")
	private String remark;


	/** 修改时间 */
	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	private Date updateTime;

  1. 查询条件
@Data
public class SearchLogDto {

    private String userName;

    private String clientIp;

    private String eventType;

    private Integer logLevel;

    private Integer operation;

    private Integer requestResult;

    private Integer logCategory;

    /**
     * 日志ids
     */
    private List<Long> ids;


    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date startTime;

    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date endTime;

  1. 控制器

    private static final int LIST_SIZE = 10000;
    
 /**
     * 导出日志
     *
     * @param dto      查询日志Dto
     * @param response {@link HttpServletResponse}
     * @throws
     */
    @PostMapping("/export")
    public void export(@RequestBody SearchLogDto dto, HttpServletResponse response) {
        ExportParams exportParams = new ExportParams();
        exportParams.setSheetName(Log.category(logCategory));
        QueryWrapper<Log> logQueryWrapper = new QueryWrapper<Log>().like(StringUtils.isNotBlank(dto.getUserName()), "user_name", dto.getUserName()).
                like(StringUtils.isNotBlank(dto.getClientIp()), "client_ip", dto.getClientIp()).eq(dto.getRequestResult() != null, "request_result", dto.getRequestResult()).eq(dto.getLogLevel() != null, "log_level", dto.getLogLevel())
                .in(dto.getIds().size()>0,"id",dto.getIds());
        if (dto.getStartTime() != null){
            logQueryWrapper .ge(dto.getStartTime() != null, "create_time",dto.getStartTime());
        }
        if (dto.getEndTime() != null){
            logQueryWrapper.le(dto.getEndTime() != null, "create_time", dto.getEndTime());
        }
        List<Log> list = logService.list(logQueryWrapper);
        GetModelNameAndType.disposeLogList(list);
        Workbook workbook = null;
        int listSize = list.size();
        //子集合的长度
        int toIndex = 10000;
        for (int i = 0; i < listSize; i += LIST_SIZE) {
            if (i + LIST_SIZE > listSize) {
                toIndex = listSize - i;
            }
            List<Log> newList = list.subList(i, i + toIndex);
            workbook = ExcelExportUtil.exportBigExcel(exportParams, Log.class, newList);
        }
        if (workbook == null) {
            workbook = ExcelExportUtil.exportBigExcel(exportParams, Log.class, new ArrayList<Log>(0));
        }
        ExcelExportUtil.closeExportBigExcel();
        response.setContentType("application/x-download;charset=UTF-8");
        String category = Log.category(logCategory);
        try {
            RequestUtils.setDownloadHeader(response, new String(category.getBytes(), "ISO8859-1") + ".xls");
        } catch (UnsupportedEncodingException e) {
            System.err.println(e.getMessage());
        }
        FileOutputStream fos = null;
        InputStream input = null;
        OutputStream output = null;
        File file = null;
        try {
            fos = new FileOutputStream("/" + category + ".xls");
            workbook.write(fos);
            file = new File("/" + category + ".xls");
            input = new FileInputStream(file);
            output = response.getOutputStream();
            byte[] buff = new byte[1024];
            int len = 0;
            while ((len = input.read(buff)) > -1) {
                output.write(buff, 0, len);
            }
        } catch (IOException e) {
            System.err.println(e.getMessage());
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                System.err.println(e.getMessage());
            }
            try {
                if (output != null) {
                    output.close();
                }
            } catch (IOException e) {
                System.err.println(e.getMessage());
            }
            try {
                if (input != null) {
                    input.close();
                }
            } catch (IOException e) {
                System.err.println(e.getMessage());

            }
        }
        if (file != null) {
            file.delete();
        }
    }

RequestUtils

public class RequestUtils {

    /**
     * 设置让浏览器弹出下载对话框的Header.
     *
     * @param filename
     *            下载后的文件名.
     */
    public static void setDownloadHeader(HttpServletResponse response, String filename) {
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"");
    }

}

http://www.niftyadmin.cn/n/1302238.html

相关文章

excel导出大文件到Excel(2)

excel导出大文件到Excel(2) 实体类 /*** 日志实体类** author xiaohui* version 1.0* date 2019-05-29 16:45:54*/ Entity Table(name "jc_sys_log") public class SysLog extends AbstractDomain<Integer> implements Serializable {private static final …

redis 安全漏洞防御

redis 安全漏洞防御 一、端口安全(限制客户端ip以及修改密码) 置redis密码 找到requirepass&#xff0c;设置新的密码将 redis 启动在 127.0.0.1 上 一般建议方案是将 redis 启动在 127.0.0.1 上 但是这种方式不能跨服务器访问&#xff0c;一般方式是将 Redis 放到内网&…

记录一个奇葩问题-----微信转码空格

记录一个奇葩问题----微信转码空格 微信发送文本信息 有的时候&#xff0c;可能通过微信发送某些请求的信息&#xff0c;这个时候问题就来了 自己在本地&#xff0c;用postman测试&#xff0c;接口完全没问题&#xff0c;但是&#xff0c;通过微信发送给他人&#xff0c;他人测…

springboot学习(五十一) springboot中使用openfeign实现调用本地接口访问远程服务

文章目录前言一、Feign是什么&#xff1f;二、使用步骤1.引入库2.编写远程服务示例3.本地编写访问接口1.GET请求&#xff0c;使用RequestParam传参2.DELETE请求&#xff0c;使用PathVariable传参3.POST请求&#xff0c;使用RequestBody传参4.PUT请求&#xff0c;使用RequestBod…

java poi导出带有下拉框的模板(1)

java poi导出带有下拉框的模板(1) 实体类 Data EqualsAndHashCode(callSuper false) Accessors(chain true) public class ImportCommonUser {Excel(name "账号类型",width 25,isImportField "true_st",replace {"长期_1", "临时_2…

管理员已阻止你运行此应用有关详细信息请与管理员联系

安装应用程序提示&#xff1a;管理员已阻止你运行此应用有关详细信息请与管理员联系 转载&#xff1a;https://blog.csdn.net/wsyzxss/article/details/79171084 我的电脑是win10 家庭版&#xff0c;没有策略组

springboot学习(五十二) springboot中使用retrofit实现调用本地接口访问远程服务

文章目录前言一、retrofit是什么&#xff1f;二、使用步骤1.引入库2.编写远程测试接口3.编写本地接口和测试接口3.1. retrofit的配置信息3.2. 本地和测试接口3.3. 测试4.编写拦截器5.自定义注解拦截器总结前言 上一篇文章我们学习了使用openfeign来调用本地接口访问远程服务&am…

fastjson序列化出现StackOverflowError

fastjson序列化出现StackOverflowError 看到StackOverflowError这个就会想到死循环 所以解决该问题的步骤 是否用了递归 若是用了递归&#xff0c;百分之八十估计就是递归的问题若是没有用递归&#xff0c;可能是返回的数据过多