模板打印:代码实现和总结
生活随笔
收集整理的這篇文章主要介紹了
模板打印:代码实现和总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
模板打印
概述
自定義生成Excel報表文件還是有很多不盡如意的地方,特別是針對復雜報表頭,單元格樣式,字體等操作。手寫這些代碼不僅費時費力,有時候效果還不太理想。那怎么樣才能更方便的對報表樣式,報表頭進行處理呢?答案是使用已經準備好的Excel模板,只需要關注模板中的數據即可。
模板打印的操作步驟
1. 制作模版文件(模版文件的路徑)
2. 導入(加載)模版文件,從而得到一個工作簿
3. 讀取工作表
4. 讀取行
5. 讀取單元格
6. 讀取單元格樣式
7. 設置單元格內容
8. 其他單元格就可以使用讀到的樣式了
?
/*** 采用模板打印的形式完成報表生成* 模板* 參數:* 年月-月(2018-02%)** sxssf對象不支持模板打印*/@RequestMapping(value = "/export/{month}", method = RequestMethod.GET)public void export(@PathVariable String month) throws Exception {//1.獲取報表數據List<EmployeeReportResult> list = userCompanyPersonalService.findByReport(companyId,month);//2.加載模板Resource resource = new ClassPathResource("excel-template/hr-demo.xlsx");FileInputStream fis = new FileInputStream(resource.getFile());//3.通過工具類完成下載 // new ExcelExportUtil(EmployeeReportResult.class,2,2). // export(response,fis,list,month+"人事報表.xlsx");//3.根據模板創建工作簿Workbook wb = new XSSFWorkbook(fis);//4.讀取工作表Sheet sheet = wb.getSheetAt(0);//5.抽取公共樣式Row row = sheet.getRow(2);CellStyle styles [] = new CellStyle[row.getLastCellNum()];for(int i=0;i<row.getLastCellNum();i++) {Cell cell = row.getCell(i);styles[i] = cell.getCellStyle();}//6.構造單元格int rowIndex = 2;Cell cell=null;for(int i=0;i<10000;i++) {for (EmployeeReportResult employeeReportResult : list) {row = sheet.createRow(rowIndex++);// 編號,cell = row.createCell(0);cell.setCellValue(employeeReportResult.getUserId());cell.setCellStyle(styles[0]);// 姓名,cell = row.createCell(1);cell.setCellValue(employeeReportResult.getUsername());cell.setCellStyle(styles[1]);// 手機,cell = row.createCell(2);cell.setCellValue(employeeReportResult.getMobile());cell.setCellStyle(styles[2]);// 最高學歷,cell = row.createCell(3);cell.setCellValue(employeeReportResult.getTheHighestDegreeOfEducation());cell.setCellStyle(styles[3]);// 國家地區,cell = row.createCell(4);cell.setCellValue(employeeReportResult.getNationalArea());cell.setCellStyle(styles[4]);// 護照號,cell = row.createCell(5);cell.setCellValue(employeeReportResult.getPassportNo());cell.setCellStyle(styles[5]);// 籍貫,cell = row.createCell(6);cell.setCellValue(employeeReportResult.getNativePlace());cell.setCellStyle(styles[6]);// 生日,cell = row.createCell(7);cell.setCellValue(employeeReportResult.getBirthday());cell.setCellStyle(styles[7]);// 屬相,cell = row.createCell(8);cell.setCellValue(employeeReportResult.getZodiac());cell.setCellStyle(styles[8]);// 入職時間,cell = row.createCell(9);cell.setCellValue(employeeReportResult.getTimeOfEntry());cell.setCellStyle(styles[9]);// 離職類型,cell = row.createCell(10);cell.setCellValue(employeeReportResult.getTypeOfTurnover());cell.setCellStyle(styles[10]);// 離職原因,cell = row.createCell(11);cell.setCellValue(employeeReportResult.getReasonsForLeaving());cell.setCellStyle(styles[11]);// 離職時間cell = row.createCell(12);cell.setCellValue(employeeReportResult.getResignationTime());cell.setCellStyle(styles[12]);}}//7.下載//3.完成下載ByteArrayOutputStream os = new ByteArrayOutputStream();wb.write(os);new DownloadUtils().download(os,response,month+"人事報表.xlsx");}?
總結
以上是生活随笔為你收集整理的模板打印:代码实现和总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatisdb.sql
- 下一篇: 自定义工具类:工具类介绍