web 导出数据到 exls 中

// 文件下载:导出excel表
    @RequestMapping(value = "/exportExcel",  method = {RequestMethod.GET})
    @ResponseBody
    public String exportExcel(HttpServletRequest request, HttpServletResponse response)
            throws UnsupportedEncodingException {
        // 一、从后台拿数据
        if (null == request || null == response) {
            return "";
        }
        
        // 二、 数据转成excel
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/x-download");

        String fileName = "deviceInfo.xlsx";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
        // 第一步:定义一个新的工作簿
        XSSFWorkbook wb = new XSSFWorkbook();
        // 第二步:创建一个Sheet页
        XSSFSheet sheet = wb.createSheet("startTimeendTime");
        sheet.setDefaultRowHeight((short) (3 * 256));// 设置行高
        sheet.setColumnWidth(0, 4000);// 设置列宽
        sheet.setColumnWidth(1, 5500);
        sheet.setColumnWidth(2, 5500);
        sheet.setColumnWidth(3, 5500);
        XSSFFont font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 16);

        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("ID ");
        cell = row.createCell(1);
        cell.setCellValue("厂家 ");
        cell = row.createCell(2);
        cell.setCellValue("SN");
        cell = row.createCell(3);
        cell.setCellValue("配置信息");
        XSSFRow rows;
        XSSFCell cells;
        for (int i = 0; i < 10; i++) {
            // 第三步:在这个sheet页里创建一行
            rows = sheet.createRow(i + 1);
            // 第四步:在该行创建一个单元格
            cells = rows.createCell(0);
            // 第五步:在该单元格里设置值
            cells.setCellValue("123");

            cells = rows.createCell(1);
            cells.setCellValue("123");
            cells = rows.createCell(2);
            cells.setCellValue("123");
            cells = rows.createCell(3);
            cells.setCellValue("123");
        }

        try {
            OutputStream out = response.getOutputStream();
            wb.write(out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return"article-list";
    }

2.  所需要的 jar  包都包括:poi-ooxml-3.9.jar   poi-3.9-20121203.jar   xmlbeans-5.2.4.jar   poi-ooxml-schemas-3.9.jar   dom4j-1.6.jar

  jar 包:  链接:https://pan.baidu.com/s/18dNlDP8t0KLmR0BHbq9snQ 密码:dqna

原文地址:https://www.cnblogs.com/redhat0019/p/9364454.html