poi技術導出海量數據到excel

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) { 
        }
    }

發佈了19 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章