使用apache POI把list集合里面的实体写入Excel(java)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                使用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)的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: css-结构伪类选择器
 - 下一篇: 前端_网页编程 节流