日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用apache POI把list集合里面的实体写入Excel(java)

發布時間:2024/9/27 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用apache POI把list集合里面的实体写入Excel(java) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、導入maven依賴包

<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency>

二、案例代碼

public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {/*** 創建一個Excel文件*/HSSFWorkbook workbook = new HSSFWorkbook();/*** 創建一個sheet*/HSSFSheet sheet = workbook.createSheet("員工表一");/*** 在sheet表中添加0行*/HSSFRow row = sheet.createRow(0);/*** 創建單元格,設置表頭*/HSSFCell cell = row.createCell(0);cell.setCellValue("員工工號");cell = row.createCell(1);cell.setCellValue("省份證號");cell = row.createCell(2);cell.setCellValue("姓名");cell = row.createCell(3);cell.setCellValue("性別");cell = row.createCell(4);cell.setCellValue("出生日期");cell = row.createCell(5);cell.setCellValue("聯系方式");cell = row.createCell(6);cell.setCellValue("家庭地址");cell = row.createCell(7);cell.setCellValue("擔任職務");cell = row.createCell(8);cell.setCellValue("角色名稱");cell = row.createCell(9);cell.setCellValue("所屬科室");/*** 把list集合里面的數據寫入工作表*/List<Doctor> list = doctorService.findAll();SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");for (int i = 0; i < list.size(); i++) {/*** 創建行*/HSSFRow row1 = sheet.createRow(i + 1);Doctor doctor = list.get(i);/*** 為單元格寫入值,從0開始*/row1.createCell(0).setCellValue(doctor.getDocid());row1.createCell(1).setCellValue(doctor.getIdcard());row1.createCell(2).setCellValue(doctor.getDocname());row1.createCell(3).setCellValue(doctor.getDocsex());row1.createCell(4).setCellValue(simpleDateFormat.format(doctor.getDocbirthday()));row1.createCell(5).setCellValue(doctor.getDocphone());row1.createCell(6).setCellValue(doctor.getDocaddress());row1.createCell(7).setCellValue(doctor.getDuty());row1.createCell(8).setCellValue(doctor.getRolename());row1.createCell(9).setCellValue(doctor.getDptname());}File file = new File("E://hospital//doctor.xls");if (file.exists()) {file.delete();}/*** 將文件保存到指定的位置*/try {file.createNewFile();workbook.write(file);System.out.println("導出到成功");System.out.println("默認位置:E://hospital//doctor.xls");workbook.close();} catch (IOException e) {e.printStackTrace();}request.setAttribute("msg","導出成功--已保存到:E://hospital//doctor.xls");request.getRequestDispatcher("/success.jsp").forward(request, response);}

三、運行截圖

總結

以上是生活随笔為你收集整理的使用apache POI把list集合里面的实体写入Excel(java)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。