whycxzp
2021-11-24 1afe70fac3969b6fec64c669ac7838e18e2f57f6
引入excel依赖,编写接口导出操作事件记录
4个文件已修改
1个文件已添加
128 ■■■■■ 已修改文件
pom.xml 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/UserLogController.java 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/UserLogService.java 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/util/DateUtil.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/util/ExcelUtil.java 85 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml
@@ -138,6 +138,12 @@
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <!--poi-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.16</version>
        </dependency>
    </dependencies>
    <build>
src/main/java/com/whyc/controller/UserLogController.java
@@ -4,10 +4,15 @@
import com.whyc.dto.Response;
import com.whyc.pojo.UserLog;
import com.whyc.service.UserLogService;
import com.whyc.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 用户操作日志
@@ -27,4 +32,17 @@
        return new Response<PageInfo<UserLog>>().set(1,pageInfo);
    }
    @PostMapping("exportExcel")
    @ApiOperation(value = "导出excel",produces = "application/octet-stream")
    public void exportExcel(HttpServletResponse response,@RequestBody List<List<String>> valueList){
        String[][] value = new String[valueList.size()][valueList.get(0).size()];
        for (int i = 0; i < valueList.size(); i++) {
            for (int j = 0; j < valueList.get(i).size(); j++) {
                value[i][j]=valueList.get(i).get(j);
            }
        }
        service.exportExcel(response,value);
    }
}
src/main/java/com/whyc/service/UserLogService.java
@@ -7,9 +7,16 @@
import com.whyc.constant.UserOperation;
import com.whyc.mapper.UserLogMapper;
import com.whyc.pojo.UserLog;
import com.whyc.util.DateUtil;
import com.whyc.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
@@ -44,4 +51,11 @@
        return pageInfo;
    }
    public void exportExcel(HttpServletResponse response, String[][] value) {
        String[] columnTitleArr = new String[]{"操作人姓名","操作类型","操作时间","终端IP","操作事件","具体参数"};
        String now = DateUtil.YYYY_MM_DD_HH_MM_SS_UNION.format(new Date());
        ExcelUtil.exportExcel("UserLog-"+now,"测试sheet1",columnTitleArr,value,new HSSFWorkbook(),response);
    }
}
src/main/java/com/whyc/util/DateUtil.java
@@ -8,8 +8,9 @@
 **/
public class DateUtil {
    public static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat sdfwithOut=new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat YYYY_MM_DD_HH_MM_SS=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat YYYY_MM_DD_HH_MM_SS_UNION=new SimpleDateFormat("yyyyMMddHHmmss");
    public static SimpleDateFormat YYYY_MM_DD=new SimpleDateFormat("yyyy-MM-dd");
src/main/java/com/whyc/util/ExcelUtil.java
New file
@@ -0,0 +1,85 @@
package com.whyc.util;
import org.apache.poi.hssf.usermodel.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
/**
 * @Description: 表格工具类
 */
public class ExcelUtil {
    //excel导出
    public static void exportExcel(String fileName, String sheetName, String[] title, Object[][] values, HSSFWorkbook wb, HttpServletResponse response) {
        //第一步,创建一个webbook,即excel的文档对象
        if (wb == null) {
            wb = new HSSFWorkbook();
        }
        //第二步,在webbook中添加一个sheet,即excel的表单
        HSSFSheet sheet = wb.createSheet(sheetName);
        //设置列宽度
        //判断是否为空
        if (values != null && values.length > 0) {
            for (int i = 0; i < values[0].length; i++) {
                sheet.setColumnWidth(i, 256 * 15);
            }
        }
        //第三步,在sheet中添加表头第0行,即excel的行
        HSSFRow row = sheet.createRow(0);
        //第四步,创建单元格,并设置值表头,设置表头居中,即excel格子单元
        HSSFCellStyle style = wb.createCellStyle();
        //居中格式
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //边框填充
        ///*style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        //style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        //style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        //style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框*/
        //背景颜色
        //style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//设置前景填充样式
        //style.setFillForegroundColor(HSSFColor.DARK_RED.index);//前景填充色
        HSSFCell cell;
        //创建标题
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i].toString());
            cell.setCellStyle(style);
        }
        // 创建内容
        for (int i = 0; i < values.length; i++) {
            row = sheet.createRow(i + 1);
            for (int j = 0; j < values[i].length; j++) {
                cell = row.createCell(j);
                if (values[i][j] != null) {
                    cell.setCellValue(values[i][j].toString());
                } else {
                    cell.setCellValue("");
                }
                cell.setCellStyle(style);
            }
        }
        //将文件存到指定位置
        OutputStream os = null;
        try {
            os = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            //response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes()) + ".xls");
            //不保存缓存信息与response.reset同样效果
            response.addHeader("Pragma", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            wb.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}