1. 程式人生 > 其它 >資料庫和Excel相互轉換

資料庫和Excel相互轉換

1、實現把資料庫的資料輸入到Excel表格中

依賴

 <!--poi-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>

工具類

 @RequestMapping("/excelXz")
    @ResponseBody
    public String excelXz(HttpServletResponse response) throws JsonProcessingException {
        Map<String, Object> map = new HashMap<String, Object>();
        List<StudyUser> studyUsers = studyCircularService.selSearch(map);
        // 需要匯出的資料
        System.out.println("studyUsers:" + studyUsers);
        if (studyUsers != null && studyUsers.size() > 0) {
            String fileName = "studyUsers.xls";
            try {
                response.setHeader(
                        "Content-disposition",
                        "attachment;filename="
                                + new String(fileName.getBytes("gb2312"),
                                "ISO8859-1"));
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }// 設定檔案頭編碼格式
            response.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8");// 設定型別
            response.setHeader("Cache-Control", "no-cache");// 設定頭
            response.setDateHeader("Expires", 0);// 設定日期頭

            // 這裡是表格的頭部
            String[] titles = {"序號", "學號", "姓名", "班級", "專業", "違紀時間",
                    "違紀型別", "處分級別", "處分名稱"};

            try {
                // 第一步,建立一個workbook,對應一個Excel檔案
                HSSFWorkbook workbook = new HSSFWorkbook();

                // 第二步,在webbook中新增一個sheet,對應Excel檔案中的sheet
                HSSFSheet hssfSheet = workbook.createSheet("sheet1");

                // 第三步,在sheet中新增表頭第0行,注意老版本poi對Excel的行數列數有限制short

                HSSFRow row = hssfSheet.createRow(0);
                // 第四步,建立單元格,並設定值表頭 設定表頭居中
                HSSFCellStyle hssfCellStyle = workbook.createCellStyle();

                // 居中樣式
                hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

                HSSFCell hssfCell = null;
                for (int i = 0; i < titles.length; i++) {
                    hssfCell = row.createCell(i);// 列索引從0開始
                    hssfCell.setCellValue(titles[i]);// 列名1
                    hssfCell.setCellStyle(hssfCellStyle);// 列居中顯示
                }
                // 第五步,寫入實體資料

                for (int i = 0; i < studyUsers.size(); i++) {
                    row = hssfSheet.createRow(i + 1);
                    StudyUser studyUser = studyUsers.get(i);
                    // 第六步,建立單元格,並設定值
                    row.createCell(0).setCellValue(i + 1);
                    row.createCell(1).setCellValue(studyUser.getSno());
                    row.createCell(2).setCellValue(studyUser.getName());
                    row.createCell(3).setCellValue(studyUser.getGrad());
                    row.createCell(4).setCellValue(studyUser.getMajor());
                    row.createCell(5).setCellValue(studyUser.getTimes());
                    row.createCell(6).setCellValue(studyUser.getType());
                    row.createCell(7).setCellValue(studyUser.getStudyCircular().getPunLevel());
                    row.createCell(8).setCellValue(studyUser.getStudyCircular().getPunName());
                }

                // 第七步,將檔案輸出到客戶端瀏覽器
                try {
                    workbook.write(response.getOutputStream());
                    response.getOutputStream().flush();
                    response.getOutputStream().close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                System.out.println("匯出資訊失敗!");
                e.printStackTrace();
            }
        } else {
            System.out.println("查詢結果為空!");
        }
        return "成功";
    }

2、實現把資料庫的資料輸入到Excel表格中

推薦部落格:https://blog.csdn.net/qq_27328375/article/details/111641363