1. 程式人生 >實用技巧 >其它 >POI匯出失敗(使用POI匯出Excel檔案後開啟檔案提示因為檔案格式或副檔名無效而無法開啟)

POI匯出失敗(使用POI匯出Excel檔案後開啟檔案提示因為檔案格式或副檔名無效而無法開啟)


問題

當我們通過POI匯出Excel檔案後,發現檔案出錯打不開,如下所示

POI匯出失敗(使用POI匯出Excel檔案後開啟檔案提示因為檔案格式或副檔名無效而無法開啟)

原因

這是因為我們程式碼中   這三個的應用應該和我們的office表格的版本有關係。

    HSSFWorkBook:適用於Excel03版本的,副檔名只能為.xls;
    XSSFWorkBook:適用於Excel07版本及以後,副檔名為.xlsx或者.xls這兩個都行;
    SXSSFWorkBook:XSSFWorkBook升級;

所以 ,我們可以通過檢視我們電腦的版本來選擇,當我們office版本為03時,我們就用HSSFWorkBook建立,
當我們的Office版本為07以後的話我們就用XSSFWorkBook來建立物件。
  • 2003(在匯出時均需使用HSSF相關方法(字尾名僅可為.xls, 如果設定為".xlsx",會提示檔案已損壞無法開啟))

@Override
    public Workbook exportExcel() {
        // 建立新的Excel 工作簿
        Workbook workbook = new  HSSFWorkBook();

        // 在Excel工作簿中建一工作表,其名為預設值 Sheet0
        //Sheet sheet = workbook.createSheet();

        // 建立工作表
        Sheet sheet = workbook.createSheet("題庫");

        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("題目標題");
        row.createCell(1).setCellValue("題目解答");
        row.createCell(2).setCellValue("題目難度等級");
        row.createCell(3).setCellValue("排序");
        row.createCell(4).setCellValue("副標題");
        row.createCell(5).setCellValue("題目型別");
        row.createCell(6).setCellValue("是否顯示");
        List<QuestionEntity> list = this.list();
        for (int i = 0; i < list.size(); i++) {
            Row row2 = sheet.createRow(i + 1);
            //建立表格
            row2.createCell(0).setCellValue(list.get(i).getTitle());
            row2.createCell(1).setCellValue(list.get(i).getAnswer());
            row2.createCell(2).setCellValue(list.get(i).getLevel());
            row2.createCell(3).setCellValue(list.get(i).getDisplayOrder());
            row2.createCell(4).setCellValue(list.get(i).getSubTitle());
            row2.createCell(5).setCellValue(list.get(i).getType());
            row2.createCell(6).setCellValue(list.get(i).getEnable());

        }

        return workbook;
    }
 //匯出excel
    @GetMapping("exportExcel")
    public void export(String tableName, HttpServletResponse response){
        System.out.println("匯出excele");
        Workbook workbook = questionService.exportExcel();

        if (workbook != null) {
            String fileName = "uxue_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xlsx";
//            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName  );
//            response.setContentType("application/octet-stream;charset=GB2312");
            response.setContentType("application/binary;charset=ISO8859_1");
            response.setCharacterEncoding("GB2312");
            OutputStream outputStream;
            try {
                outputStream = response.getOutputStream();
                workbook.write(outputStream);
                outputStream.flush();
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                response.getWriter().print("error");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }
  • 2007(在匯出時使用XSSF相關方法(字尾名為.xls和.xlsx均可))

@Override
    public Workbook exportExcel() {
        // 建立新的Excel 工作簿
        Workbook workbook = new  XSSFWorkBook();

        // 在Excel工作簿中建一工作表,其名為預設值 Sheet0
        //Sheet sheet = workbook.createSheet();

        // 建立工作表
        Sheet sheet = workbook.createSheet("題庫");

        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("題目標題");
        row.createCell(1).setCellValue("題目解答");
        row.createCell(2).setCellValue("題目難度等級");
        row.createCell(3).setCellValue("排序");
        row.createCell(4).setCellValue("副標題");
        row.createCell(5).setCellValue("題目型別");
        row.createCell(6).setCellValue("是否顯示");
        List<QuestionEntity> list = this.list();
        for (int i = 0; i < list.size(); i++) {
            Row row2 = sheet.createRow(i + 1);
            //建立表格
            row2.createCell(0).setCellValue(list.get(i).getTitle());
            row2.createCell(1).setCellValue(list.get(i).getAnswer());
            row2.createCell(2).setCellValue(list.get(i).getLevel());
            row2.createCell(3).setCellValue(list.get(i).getDisplayOrder());
            row2.createCell(4).setCellValue(list.get(i).getSubTitle());
            row2.createCell(5).setCellValue(list.get(i).getType());
            row2.createCell(6).setCellValue(list.get(i).getEnable());

        }

        return workbook;
    }
 
 //匯出excel
    @GetMapping("exportExcel")
    public void export(String tableName, HttpServletResponse response){
        System.out.println("匯出excele");
        Workbook workbook = questionService.exportExcel();

        if (workbook != null) {
            String fileName = "uxue_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xlsx";
//這裡我們的.xls或者xlsx都可以
//            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName  );
//            response.setContentType("application/octet-stream;charset=GB2312");
            response.setContentType("application/binary;charset=ISO8859_1");
            response.setCharacterEncoding("GB2312");
            OutputStream outputStream;
            try {
                outputStream = response.getOutputStream();
                workbook.write(outputStream);
                outputStream.flush();
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                response.getWriter().print("error");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

就這樣我們的問題成功解決了!!!!