java导出excel

import com.ronsafe.wlw.query.LogQuery;
import com.ronsafe.wlw.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author R0137 csy
 * @Date 2021/11/8 15:29
 */
@RestController
@RequestMapping("/log")
@Api(tags = "日志管理")
public class LogController {
    @Autowired
    private LogService logService;

    @PostMapping("exportExl")
    @ApiOperation("导出excel")
    public ResponseEntity<byte[]> exportExl(@RequestBody LogQuery query) {
        return logService.excel(query);
    }
}
import com.baomidou.mybatisplus.extension.service.IService;
import com.ronsafe.wlw.entity.Log;
import com.ronsafe.wlw.query.LogQuery;
import org.springframework.http.ResponseEntity;

/**
 * @Author R0137 csy
 * @Date 2021/11/8 17:14
 */
public interface LogService extends IService<Log> {
    ResponseEntity<byte[]> excel(LogQuery query);
}
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ronsafe.wlw.entity.Log;
import com.ronsafe.wlw.mapper.AutoLogMapper;
import com.ronsafe.wlw.query.LogQuery;
import com.ronsafe.wlw.service.LogService;
import com.ronsafe.wlw.util.DateUtils;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @Author R0137 csy
 * @Date 2021/11/8 17:16
 */
@Service
public class LogServiceImpl extends ServiceImpl<AutoLogMapper, Log> implements LogService {

    @Override
    public ResponseEntity<byte[]> excel(LogQuery query) {
        List<Log> list = autoLogMapper.getAll(query,ShiroUtil.getCurrentUser().getCompanyId());
        XSSFWorkbook book = new XSSFWorkbook();

        XSSFCellStyle cellStyle = book.createCellStyle();
        cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);

        XSSFSheet sheet = book.createSheet("日志");
        sheet.setDefaultColumnWidth(20);
        //表头行创建
        XSSFRow header = sheet.createRow(0);
        createCell(book,header,(short)0,"用户名称",cellStyle);
        createCell(book,header,(short)1,"所属公司",cellStyle);
        createCell(book,header,(short)2,"操作类型",cellStyle);
        createCell(book,header,(short)3,"操作对象",cellStyle);
        createCell(book,header,(short)4,"操作时间",cellStyle);
        createCell(book,header,(short)5,"ip",cellStyle);
        createCell(book,header,(short)6,"操作账号",cellStyle);
        createCell(book,header,(short)7,"账号权限",cellStyle);
        for (int i = 0; i < list.size(); i++) {
            XSSFRow row = sheet.createRow(i + 1);
            createCell(book,row,(short)0,list.get(i).getRealName(),cellStyle);
            createCell(book,row,(short)1,list.get(i).getCompany(),cellStyle);
            createCell(book,row,(short)2,list.get(i).getOperateType(),cellStyle);
            createCell(book,row,(short)3,list.get(i).getOperateContent(),cellStyle);
            createCell(book,row,(short)4,DateUtils.Date2String(list.get(i).getCreateTime()),cellStyle);
            createCell(book,row,(short)5,list.get(i).getIp(),cellStyle);
            createCell(book,row,(short)6,list.get(i).getUserName(),cellStyle);
            createCell(book,row,(short)7,list.get(i).getUserType(),cellStyle);
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            book.write(bos);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentDispositionFormData("attachment", new String("日志.xlsx".getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<byte[]>(bos.toByteArray(), headers, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private void createCell(XSSFWorkbook wb, XSSFRow row, short col, String val,XSSFCellStyle cellStyle) {
        row.setHeight((short) 480);
        XSSFCell cell = row.createCell(col);
        cell.setCellValue(val);
        cell.setCellStyle(cellStyle);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值