poi技术导出海量数据到excel

本文介绍了一种使用Java POI库从数据库查询数据并将其导出到Excel的方法。文章详细展示了如何根据查询结果创建Excel表格,并设置了单元格样式。此外,还介绍了如何将导出的文件返回给客户端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://poi.apache.org/download.html这是包的下载网站

---------------------------------------------------------------------------------------------------------

//导出数据到EXCEL表格
public static void ExportLgExcel(HttpServletRequest request, HttpServletResponse resp) throws Exception {
        
  //读取指定目录下面的excel导出模板
        String excelDir = request.getServletContext().getRealPath("/upload/ExcelStencil");

        Workbook wb = null;
        try {
            wb = new XSSFWorkbook(new FileInputStream(new File(excelDir + "/lightList.xlsx")));
        } catch (Exception e) {
            wb = new HSSFWorkbook(new FileInputStream(new File(excelDir + "/lightList.xls")));
        }
        // 创建字体,设置其为红色、粗体: 
        Font font = wb.createFont();
        font.setColor(Font.COLOR_RED);
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        //创建格式 
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFont(font);
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);

        CellStyle cellStyleBorder = wb.createCellStyle();
        cellStyleBorder.setBorderBottom(CellStyle.BORDER_THIN);
        cellStyleBorder.setBorderLeft(CellStyle.BORDER_THIN);
        cellStyleBorder.setBorderRight(CellStyle.BORDER_THIN);
        cellStyleBorder.setBorderTop(CellStyle.BORDER_THIN);
  
  //这个是从数据库里面查询出来的数据,存放到列表中
  //Class log:数据库查询出数据的一个对象
  List<Class log> result = new ArrayList<Class log>();
  
        //获得第一个工作区
        Sheet sheet = wb.getSheetAt(0);
  
        if (!CommUtils.isNull(result)) {
  
            //循环遍历查询出来的结果对象,写入到excel表格里面去
            Integer listSize = result.size();
   
            for (int i = 0; i < listSize; i++) {
   
                Class log = result.get(i);
    
    //因为第一行存放的是列标题,所以从第二行开始写入
    
                //从第二行开始写入
                Row row = sheet.createRow(i + 1);
    
                //往表格里面填充数据
                Integer cellLeng = log.getClass().getDeclaredFields().length;
    
    
                for (int cellIndex = 0; cellIndex < cellLeng; cellIndex++) {
    
     //设置excel表格的样式
                    Cell cell = row.createCell(cellIndex);
                    if (cellIndex < cellLeng - 1) {
                        cell.setCellStyle(cellStyleBorder);
                    }
                    cell.setCellType(Cell.CELL_TYPE_STRING);
     
     //将第一个数据保存到第一个单元格,依次类推
                    if (cellIndex == 0) {
                        String one = log.getXXXXX();
                        cell.setCellValue(one);
                    } else if (cellIndex == 1) {
                        String two = log.getXXXX();
                        cell.setCellValue(two);
                    } else if (cellIndex == 2) {
                        Integer three = log.getXXXXX();
                        cell.setCellValue(three.toString());
                    }  else if (cellIndex > 2) {
                        break;
                    }
                }
            }
        }
        //生成导出文件日期
        Calendar c = Calendar.getInstance();
        String time = c.get(Calendar.YEAR) + "_" + c.get(Calendar.MONTH) + "_" + c.get(Calendar.DATE) + "_" + c.get(Calendar.HOUR) + "_" + c.get(Calendar.MINUTE) + "_" + c.get(Calendar.SECOND);
        
  //生成文件名(以下涉及到文件的输入和输出流)
  File excelFile = new File("XXXXX_" + time + ".xls");
        FileOutputStream fos = new FileOutputStream(excelFile);
        wb.write(fos);
        if (!CommUtils.isNull(fos)) {
            fos.close();
        }
        // 读到流中
        InputStream inStream = new FileInputStream(excelFile);
        // 设置输出的格式
        resp.reset();
        resp.setContentType("application/x-download; charset=utf-8");
        resp.addHeader("Content-Disposition", "attachment; filename=\"" + excelFile + "\"");
        // 循环取出流中的数据
        byte[] b = new byte[1024];
        int len;
        try {
            OutputStream out = resp.getOutputStream();
            while (inStream.read(b) > 0) {
                out.write(b);
            }
            inStream.close();
            out.close();
            excelFile.delete();
        } catch (IOException ex) { 
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值