poi 导出 excel 文件 工具类测试

xiaoxiao2025-05-27  36

    /**      * 导出 问题-导出excel      */     @Override     public JSONObject exportExcelQuestion(HttpServletRequest request, HttpServletResponse response, JSONObject param)             throws Exception {         JSONObject result = new JSONObject();         String type = param.getString("type");         // 开始导出excel表格         HSSFWorkbook exportQuestionnaire = ExportExcelQuestion.exportQuestionnaire(param);

        // 写出文件 //        response.setHeader("content-Type", "application/vnd.ms-excel"); //        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(type, "utf-8")); //        String userAgent = request.getHeader("USER-AGENT"); //        OutputStream fos = response.getOutputStream();         OutputStream fos = new FileOutputStream("ceshidaochu.xls");         try { //            if (StringUtils.contains(userAgent, "Mozilla")) { //                type = new String(type.getBytes(), "ISO8859-1"); //            } else { //                type = URLEncoder.encode(type, "utf8"); //            } //            response.setCharacterEncoding("UTF-8"); //            response.setContentType("application/vnd.ms-excel;charset=utf-8");// 设置contentType为excel格式 //            response.setHeader("Content-Disposition", "Attachment;Filename=" + type + ".xls"); //            response.flushBuffer();             exportQuestionnaire.write(fos);             fos.close();             exportQuestionnaire.close();         } catch (UnsupportedEncodingException e) {             e.printStackTrace();             result.put("success", false);             result.put("message", e.getMessage());             return result;         }         result.put("success", true);         result.put("message", "");         return result;     } public static void main(String[] args) {     JSONObject param = new JSONObject();     String str = "{\"type\":\"最受欢迎的车\",\"param\":[[\"选项\",\"选项内容\",\"数量\",\"比例\"],[\"A\",\"IZOA\",\"0\",\"0.00%\"],[\"B\",\"CROWN\",\"0\",\"0.00%\"],[\"C\",\"RAV4\",\"0\",\"0.00%\"]]}";     param = JSON.parseObject(str);     try {         JSONObject exportExcelQuestion = new QuestionnaireServiceImpl().exportExcelQuestion(null, null, param);         System.out.println(exportExcelQuestion);     } catch (Exception e) {         e.printStackTrace();     } }

转载请注明原文地址: https://www.6miu.com/read-5030807.html

最新回复(0)