导出 excel

依赖包

        <!-- POI包 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.8</version>
        </dependency>

Controller 层定义

@RestController
public class StdandardDirectoryController {
    /**
     * Excel批量导出标准化字典数据
     * @Param catalogId 目录主键
     * @Param dictCode 标准数据编码
     * @Param dictName 标准数据名称
     * @Param HttpServletRequest 输入流
     * @Param HTTPServletResponse 输出流
     * @return Object 状态码
     */
    @GetMapping("/download/exportStdDictData")
    @ResponseBody
    public void exportStdDictData(@RequestParam int catalogId,@RequestParam(required = false)String dictCode,@RequestParam(required = false)String dictName,HttpServletRequest request,HttpServletResponse response) throws Exception{

        log.info("调用/download/exportStdDictData");
        log.info("参数:catalogId="+catalogId+", dictCode="+dictCode+", dictName="+dictName);

        standardDirectoryService.exportStdDictData(request,response,catalogId,dictCode,dictName);

    }
}

service 层接口定义

public interface IStandardDirectoryService {


    /**
     * Excel批量导出标准化字典数据
     * @param request
     * @param response 响应流
     * @param catalogId  目录id
     * @param dictCode  标准数据编码
     * @param dictName  标准数据名称
     */
    void exportStdDictData(HttpServletRequest request, HttpServletResponse response,int catalogId,String dictCode,String dictName) throws Exception;



}

service 层定义

@Service
public class StandardDirectoryServiceImpl implements IStandardDirectoryService {

    /**
     * Excel批量导出标准化字典数据
     * @param request
     * @param response 响应流
     * @param catalogId  目录id
     * @param dictCode  标准数据编码
     * @param dictName  标准数据名称
     */
    @Override
    public void exportStdDictData(HttpServletRequest request, HttpServletResponse response,int catalogId,String dictCode,String dictName) throws Exception{

        /**
         * 参数校验
         */
        Catalog checkCatalogId = catalogMapper.findByCatalogId(catalogId);
        if(checkCatalogId == null ){
            throw new BusinessParamCheckingException(ExceptionConstants.DEVOPS_SERVICE_PARAMS_UNAVAILABLE_CODE,
                    ExceptionConstants.DEVOPS_SERVICE_PARAMS_UNAVAILABLE_MSG);
        }
        /**
         * 创建文件,获取目录名
         */
        Catalog catalog = catalogMapper.findByCatalogId(catalogId);
        String catalogName = catalog.getCatalogName();

        /**
         * 创建表格,填充数据
         */
        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFSheet sheet = workBook.createSheet(catalogName+"标准目录导出");
        sheet.setColumnWidth(0, 256*13);
        sheet.setColumnWidth(1, 256*13);
        HSSFRow headRow = sheet.createRow(0);
        String[] title = {"标准数据编码","标准数据名称"};
        HSSFCell cell = null;
        //填充表头
        for (int i = 0; i < title.length; i++) {
            cell = headRow.createCell(i);
            // 添加样式
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            if (title[i] != null) {
                cell.setCellValue(title[i]);
            } else {
                cell.setCellValue("-");
            }
        }

        /**
         * 获取数据,填充数据
         */
        List<StandardDirectory> list = standardDirectoryMapper.findList(catalogId, dictCode, dictName);
        for (int i = 1; i < list.size()+1; i++) {
            HSSFRow row = sheet.createRow(i);
            cell = row.createCell(0);
            cell.setCellValue(list.get(i-1).getDictCode());
            cell = row.createCell(1);
            cell.setCellValue(list.get(i-1).getDictName());
        }

        String fileName = catalogName+"标准数据导出";
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"),"ISO-8859-1" )+ ".xls");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");

        OutputStream out = response.getOutputStream();
        workBook.write(out);
        out.close();
    }


}