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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

模板打印:代码实现和总结

發布時間:2024/4/13 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 模板打印:代码实现和总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

模板打印

概述

自定義生成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");}

?

總結

以上是生活随笔為你收集整理的模板打印:代码实现和总结的全部內容,希望文章能夠幫你解決所遇到的問題。

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