在后端controller中导出excel文件

时间:2023-3-28    作者:老大夫    分类: JAVA


代码

package com.example.demo1.controller;

import com.example.demo1.modal.DzBmdjT;
import com.example.demo1.modal.DzKaoChangT;
import com.example.demo1.service.DzbmdjService;
import com.example.demo1.service.DzkaochangService;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
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.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

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

/**
 * 导出excel控制层
 */
@Controller
public class ExportController {
    //注入服务层
    @Resource
    private DzbmdjService dzBmdjService;
    @Resource
    private DzkaochangService dzKaoChangService;

    /**
     * 考生信息导出逻辑
     *
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "ksxxExport", method = RequestMethod.GET)
    public ResponseEntity<byte[]> ksxxExport() throws IOException {
        //创建工作薄
        XSSFWorkbook wb = new XSSFWorkbook();
        //分页名称
        XSSFSheet sheet = wb.createSheet("结果");
        //生成一种样式
        XSSFCellStyle style = wb.createCellStyle();
        //设值样式
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //生成一种字体
        XSSFFont font = wb.createFont();
        //设值字体
        font.setFontName("微软雅黑");
        //设值字体大小
        font.setFontHeightInPoints((short) 12);
        //字体加粗
        font.setBold(true);
        //在样式中引用这种字体
        style.setFont(font);
        //
        //生成并设值另一个样式
        XSSFCellStyle style2 = wb.createCellStyle();
        style2.setAlignment(HorizontalAlignment.CENTER);
        style2.setVerticalAlignment(VerticalAlignment.CENTER);

        //生成另一种字体
        XSSFFont font2 = wb.createFont();
        //设值字体
        font2.setFontName("微软雅黑");
        //设值字体大小
        font2.setFontHeightInPoints((short) 12);
        //在样式2中引用这种字体
        style2.setFont(font2);

        //设值列宽
        sheet.setColumnWidth(0, 25 * 256);//身份证号列宽
        sheet.setColumnWidth(1, 22 * 256);//考生号列宽
        sheet.setColumnWidth(2, 11 * 256);//姓名列宽
        sheet.setColumnWidth(3, 9 * 256);//性别列宽
        sheet.setColumnWidth(4, 18 * 256);//政治面貌列宽
        sheet.setColumnWidth(5, 28 * 256);//毕业高中列宽
        sheet.setColumnWidth(6, 18 * 256);//联系电话列宽

        String[] excelHeaderArr = {"身份证号码", "考生号", "姓名", "性别", "政治面貌", "毕业高中", "联系电话"};
        //生成表格第一行 表头
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = null;
        for (int i = 0; i < excelHeaderArr.length; i++) {
            cell = row.createCell(i);
            //设值数据
            cell.setCellValue(excelHeaderArr[i]);
            cell.setCellStyle(style);
        }

        //报名登记信息列表
        DzBmdjT dzBmdjT = new DzBmdjT();
        List<DzBmdjT> bmdjList = dzBmdjService.getBmdjList(dzBmdjT);
        //循环报名登记列表并创建行与列
        for (int i = 0; i < bmdjList.size(); i++) {
            XSSFRow row1 = sheet.createRow(i + 1);
            cell = row1.createCell(0);
            cell.setCellValue(bmdjList.get(i).getSfzh());
            cell.setCellStyle(style2);

            cell = row1.createCell(1);
            cell.setCellValue(String.valueOf(bmdjList.get(i).getKsh()));
            cell.setCellStyle(style2);

            cell = row1.createCell(2);
            cell.setCellValue(bmdjList.get(i).getXm());
            cell.setCellStyle(style2);

            cell = row1.createCell(3);
            cell.setCellValue(bmdjList.get(i).getXb());
            cell.setCellStyle(style2);

            cell = row1.createCell(4);
            cell.setCellValue(bmdjList.get(i).getZzmm());
            cell.setCellStyle(style2);

            cell = row1.createCell(5);
            cell.setCellValue(bmdjList.get(i).getBygz());
            cell.setCellStyle(style2);

            cell = row1.createCell(6);
            cell.setCellValue(bmdjList.get(i).getLxdh());
            cell.setCellStyle(style2);

        }

        //创建输出流对象
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            wb.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            outputStream.close();
        }

        //下载文件
        HttpHeaders httpHeaders = new HttpHeaders();
        String fileName = new String("考生信息表.xlsx".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        httpHeaders.setContentDispositionFormData("attachment", fileName);
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        ResponseEntity<byte[]> filebyte = new ResponseEntity<byte[]>(outputStream.toByteArray(), httpHeaders, HttpStatus.CREATED);
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            outputStream.close();
        }

        return filebyte;
    }

    /**
     * 考场信息导出
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "kcExport", method = RequestMethod.GET)
    public ResponseEntity<byte[]> kcExport() throws IOException {
        //创建工作薄
        XSSFWorkbook wb = new XSSFWorkbook();
        //分页名称
        XSSFSheet sheet = wb.createSheet("结果");
        //生成一种样式
        XSSFCellStyle style = wb.createCellStyle();
        //设值样式
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //生成一种字体
        XSSFFont font = wb.createFont();
        //设值字体
        font.setFontName("微软雅黑");
        //设值字体大小
        font.setFontHeightInPoints((short) 12);
        //字体加粗
        font.setBold(true);
        //在样式中引用这种字体
        style.setFont(font);

        //生成并设值另一个样式
        XSSFCellStyle style2 = wb.createCellStyle();
        style2.setAlignment(HorizontalAlignment.CENTER);
        style2.setVerticalAlignment(VerticalAlignment.CENTER);

        //生成另一种字体
        XSSFFont font2 = wb.createFont();
        //设值字体
        font2.setFontName("微软雅黑");
        //设值字体大小
        font2.setFontHeightInPoints((short) 12);
        //在样式2中引用这种字体
        style2.setFont(font2);

        //设值列宽
        sheet.setColumnWidth(0, 25 * 256);//考场编号列宽
        sheet.setColumnWidth(1, 22 * 256);//考场地点列宽
        sheet.setColumnWidth(2, 11 * 256);//容纳人数列宽

        String[] excelHeaderArr = {"考场编号", "考场地点", "容纳人数"};
        //生成表格第一行 表头
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = null;
        for (int i = 0; i < excelHeaderArr.length; i++) {
            cell = row.createCell(i);
            //设值数据
            cell.setCellValue(excelHeaderArr[i]);
            cell.setCellStyle(style);
        }

        //考场信息列表
        List<DzKaoChangT> kaochangT = dzKaoChangService.selectkaochang();
        for (int i = 0; i < kaochangT.size(); i++) {
            XSSFRow row1 = sheet.createRow(i + 1);
            cell = row1.createCell(0);
            cell.setCellValue(kaochangT.get(i).getKcbh());
            cell.setCellStyle(style2);

            cell = row1.createCell(1);
            cell.setCellValue(kaochangT.get(i).getKcdd());
            cell.setCellStyle(style2);

            cell = row1.createCell(2);
            cell.setCellValue(String.valueOf(kaochangT.get(i).getRnrs()));
            cell.setCellStyle(style2);

        }

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            wb.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        HttpHeaders httpHeaders = new HttpHeaders();
        String fileName = new String("考场信息表.xlsx".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        httpHeaders.setContentDispositionFormData("attachment", fileName);
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        ResponseEntity<byte[]> filebyte = new ResponseEntity<byte[]>(outputStream.toByteArray(), httpHeaders, HttpStatus.CREATED);
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return filebyte;
    }
}

版权所有:伸手党盘
文章标题:在后端controller中导出excel文件
文章链接:https://ssdpan.cn/?post=239
本站文章来源于网络搜集和原创,仅供学习使用,未经授权请勿用于任何商业用途,如有侵权及时联系删除

推荐阅读:


扫描二维码,在手机上阅读