POI导出人事报表:代码实现
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                POI导出人事报表:代码实现
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                人事報(bào)表導(dǎo)出
步驟分析
構(gòu)造Excel表格數(shù)據(jù)
 創(chuàng)建工作簿
 創(chuàng)建sheet
 創(chuàng)建行對(duì)象
 創(chuàng)建單元格對(duì)象
 填充數(shù)據(jù),設(shè)置樣式
 下載
代碼實(shí)現(xiàn)
(1)配置controller
/*** 當(dāng)月人事報(bào)表導(dǎo)出* 參數(shù):* 年月-月(2018-02%)*/ @RequestMapping(value = "/export/{month}", method = RequestMethod.GET) public void export(@PathVariable String month) throws Exception {//1.獲取報(bào)表數(shù)據(jù)List<EmployeeReportResult> list = userCompanyPersonalService.findByReport(companyId,month);//2.構(gòu)造Excel//創(chuàng)建工作簿//SXSSFWorkbook : 百萬(wàn)數(shù)據(jù)報(bào)表//Workbook wb = new XSSFWorkbook();SXSSFWorkbook wb = new SXSSFWorkbook(100); //閾值,內(nèi)存中的對(duì)象數(shù)量最大數(shù)量//構(gòu)造sheetSheet sheet = wb.createSheet();//創(chuàng)建行//標(biāo)題String [] titles = "編號(hào),姓名,手機(jī),最高學(xué)歷,國(guó)家地區(qū),護(hù)照號(hào),籍貫,生日,屬相,入職時(shí)間,離職類(lèi)型,離職原因,離職時(shí)間".split(",");//處理標(biāo)題Row row = sheet.createRow(0);int titleIndex=0;for (String title : titles) {Cell cell = row.createCell(titleIndex++);cell.setCellValue(title);}int rowIndex = 1;Cell cell=null;for(int i=0;i<10000;i++){for (EmployeeReportResult employeeReportResult : list) {row = sheet.createRow(rowIndex++);// 編號(hào),cell = row.createCell(0);cell.setCellValue(employeeReportResult.getUserId());// 姓名,cell = row.createCell(1);cell.setCellValue(employeeReportResult.getUsername());// 手機(jī),cell = row.createCell(2);cell.setCellValue(employeeReportResult.getMobile());// 最高學(xué)歷,cell = row.createCell(3);cell.setCellValue(employeeReportResult.getTheHighestDegreeOfEducation());// 國(guó)家地區(qū),cell = row.createCell(4);cell.setCellValue(employeeReportResult.getNationalArea());// 護(hù)照號(hào),cell = row.createCell(5);cell.setCellValue(employeeReportResult.getPassportNo());// 籍貫,cell = row.createCell(6);cell.setCellValue(employeeReportResult.getNativePlace());// 生日,cell = row.createCell(7);cell.setCellValue(employeeReportResult.getBirthday());// 屬相,cell = row.createCell(8);cell.setCellValue(employeeReportResult.getZodiac());// 入職時(shí)間,cell = row.createCell(9);cell.setCellValue(employeeReportResult.getTimeOfEntry());// 離職類(lèi)型,cell = row.createCell(10);cell.setCellValue(employeeReportResult.getTypeOfTurnover());// 離職原因,cell = row.createCell(11);cell.setCellValue(employeeReportResult.getReasonsForLeaving());// 離職時(shí)間cell = row.createCell(12);cell.setCellValue(employeeReportResult.getResignationTime());}}//3.完成下載ByteArrayOutputStream os = new ByteArrayOutputStream();wb.write(os);new DownloadUtils().download(os,response,month+"人事報(bào)表.xlsx"); } package com.learn.common.utils;import org.apache.poi.ss.usermodel.Workbook;import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder;public class DownloadUtils {public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException {response.setContentType("application/octet-stream");returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1")); //保存的文件名,必須和頁(yè)面編碼一致,否則亂碼response.addHeader("content-disposition","attachment;filename=" + returnName);response.setContentLength(byteArrayOutputStream.size());ServletOutputStream outputstream = response.getOutputStream(); //取得輸出流byteArrayOutputStream.writeTo(outputstream); //寫(xiě)到輸出流byteArrayOutputStream.close(); //關(guān)閉outputstream.flush(); //刷數(shù)據(jù)} }?
總結(jié)
以上是生活随笔為你收集整理的POI导出人事报表:代码实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
                            
                        - 上一篇: POI文件导入:代码实现-解析Excel
 - 下一篇: 自定义工具类:导入工具类测试