easyui poi 一个模板导出多个模板数据,在一个sheet中导出
1.示例代码
@ResponseExcel @ApiOperation ( value = "导出excel表格" , notes = "导出详情excel表格" ) @GetMapping ( "/export" ) public void export ( HttpServletResponse response) { try { Map < String , Object > map = new HashMap < > ( ) ; List < PlanningDetailVo > list= new ArrayList < > ( ) ; list. addAll ( PlanningDetailVo . testList1 ( ) ) ; list. addAll ( PlanningDetailVo . testList2 ( ) ) ; map. put ( "list" , list) ; String path = "excelFile/重点工作计划考核指标.xlsx" ; TemplateExportParams params = new TemplateExportParams ( path) ; Workbook workbook = ExcelExportUtil . exportExcel ( params, map) ; Sheet sheet = workbook. getSheet ( "sheet1" ) ; for ( int i = 0 ; i < list. size ( ) ; i++ ) { PlanningDetailVo vo = list. get ( i) ; if ( vo. getIndex ( ) != null ) { if ( vo. getIndex ( ) . length ( ) >= 5 ) { CellRangeAddress cell= new CellRangeAddress ( i, i, 0 , 2 ) ; sheet. addMergedRegion ( cell) ; setFontAlign ( workbook, i, 0 ) ; } if ( "序号" . equals ( vo. getIndex ( ) ) || vo. getIndex ( ) . length ( ) == 1 ) { for ( int j = 0 ; j < 3 ; j++ ) { setBorder ( workbook, i, j) ; } } } } response. setHeader ( "Content-Disposition" , "attachment;fileName=" + URLEncoder . encode ( "重点工作计划考核指标.xlsx" , "UTF-8" ) ) ; OutputStream os = new BufferedOutputStream ( response. getOutputStream ( ) ) ; workbook. write ( os) ; os. flush ( ) ; os. close ( ) ; os. close ( ) ; } catch ( Exception e) { e. printStackTrace ( ) ; } }
private void setFontAlign ( Workbook workbook, int rowIndex, int columnIndex) { Sheet sheet = workbook. getSheet ( "sheet1" ) ; CellStyle cellStyle = workbook. createCellStyle ( ) ; Font font = workbook. createFont ( ) ; font. setBold ( true ) ; cellStyle. setFont ( font) ; cellStyle. setAlignment ( HorizontalAlignment . CENTER) ; cellStyle. setVerticalAlignment ( VerticalAlignment . CENTER) ; Row row = sheet. getRow ( rowIndex) ; Cell cell = row. getCell ( columnIndex) ; cell. setCellStyle ( cellStyle) ;
}
private void setBorder ( Workbook workbook, int rowIndex, int columnIndex) { Sheet sheet = workbook. getSheet ( "sheet1" ) ; CellStyle cellStyle = workbook. createCellStyle ( ) ; cellStyle. setAlignment ( HorizontalAlignment . CENTER) ; cellStyle. setVerticalAlignment ( VerticalAlignment . CENTER) ; cellStyle. setBorderBottom ( BorderStyle . THIN) ; cellStyle. setBorderLeft ( BorderStyle . THIN) ; cellStyle. setBorderRight ( BorderStyle . THIN) ; cellStyle. setBorderTop ( BorderStyle . THIN) ; Row row = sheet. getRow ( rowIndex) ; Cell cell = row. getCell ( columnIndex) ; cell. setCellStyle ( cellStyle) ;
}
2.模拟数据
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PlanningDetailVo { private String index; private String typeName; private String remark; public static List < PlanningDetailVo > testList1 ( ) { List < PlanningDetailVo > list= new ArrayList < > ( ) ; list. add ( new PlanningDetailVo ( "信息管理部" , "" , "" ) ) ; list. add ( new PlanningDetailVo ( "序号" , "类型" , "备注" ) ) ; list. add ( new PlanningDetailVo ( "1" , "指标1" , "备注1" ) ) ; list. add ( new PlanningDetailVo ( "2" , "指标2" , "备注2" ) ) ; list. add ( new PlanningDetailVo ( "3" , "指标3" , "备注3" ) ) ; list. add ( new PlanningDetailVo ( "4" , "指标4" , "备注4" ) ) ; list. add ( new PlanningDetailVo ( "5" , "指标5" , "备注5" ) ) ; return list; } public static List < PlanningDetailVo > testList2 ( ) { List < PlanningDetailVo > list= new ArrayList < > ( ) ; list. add ( new PlanningDetailVo ( "科技管理部" , "" , "" ) ) ; list. add ( new PlanningDetailVo ( "序号" , "类型" , "备注" ) ) ; list. add ( new PlanningDetailVo ( "1" , "考核1" , "备注0001" ) ) ; list. add ( new PlanningDetailVo ( "2" , "考核2" , "备注0002" ) ) ; list. add ( new PlanningDetailVo ( "3" , "考核3" , "备注0003" ) ) ; list. add ( new PlanningDetailVo ( "4" , "考核4" , "备注0004" ) ) ; list. add ( new PlanningDetailVo ( "5" , "考核5" , "备注0005" ) ) ; return list; }
}
3.导出模板
4.导出效果