1. 程式人生 > >Springboot/SpringMVC+POI 實現Excel匯出功能(點選下載方式實現)

Springboot/SpringMVC+POI 實現Excel匯出功能(點選下載方式實現)

 @RequestMapping("/download")
    public void downstudents(HttpServletRequest request, HttpServletResponse response,@RequestParam String startTime, @RequestParam String endTime)throws IOException
    {  //我這是根據前端傳來的起始時間來查詢資料庫裡的資料,如果沒有輸入變數要求,保留前兩個就行
   
        String[] headers = { "ID", "主題", "姓名", "手機","建立時間","開始時間","結束時間"};//匯出的Excel頭部,這個要根據自己專案改一下
     
        List dataset = videoMeetInfoMapper.selectByTime(startTime,endTime);//查詢出來的資料,根據自己專案改一下
        
		//下面的完全不動就行了(Excel資料中不包含圖片)
		
        // 宣告一個工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 生成一個表格
        HSSFSheet sheet = workbook.createSheet();
        // 設定表格預設列寬度為15個位元組
        sheet.setDefaultColumnWidth((short) 18);
        HSSFRow row = sheet.createRow(0);
        for (short i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //遍歷集合資料,產生資料行
        Iterator it = dataset.iterator();
        int index = 0;
        while (it.hasNext()) {
            index++;
            row = sheet.createRow(index);
            demo t = (demo) it.next();
            //利用反射,根據javabean屬性的先後順序,動態呼叫getXxx()方法得到屬性值
            Field[] fields = t.getClass().getDeclaredFields();
            for (short i = 0; i < fields.length; i++) {
                HSSFCell cell = row.createCell(i);
                Field field = fields[i];
                String fieldName = field.getName();
                String getMethodName = "get"
                        + fieldName.substring(0, 1).toUpperCase()
                        + fieldName.substring(1);
                try {
                    Class tCls = t.getClass();
                    Method getMethod = tCls.getMethod(getMethodName,
                            new Class[]{});
                    Object value = getMethod.invoke(t, new Object[]{});
                    String textValue = null;


                    if (value instanceof Date)
                    {
                        Date date = (Date) value;
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        textValue = sdf.format(date);
                    }
                    else
                    {
                        //其它資料型別都當作字串簡單處理
                        textValue = value.toString();
                    }       
					
                            HSSFRichTextString richString = new HSSFRichTextString(textValue);
                            HSSFFont font3 = workbook.createFont();
                            font3.setColor(HSSFColor.BLUE.index);//定義Excel資料顏色
                            richString.applyFont(font3);
                            cell.setCellValue(richString);
                     
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=createList.xls");//預設Excel名稱
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }