private void cappSpreadjsExportPdf() throws Exception {
String data = params.get("data").toString();
String path ="d:/test.pdf" ;
try {
Workbook workbook = new Workbook();
workbook.fromJson(data);
File f= new File(path);
if (f.exists()) {
f.delete();
}
List<PageInfo> pages =new ArrayList<>();
PrintManager printManager = new PrintManager();
for (IWorksheet worksheet : workbook.getWorksheets()) {
List paginate =makeworksheet(printManager,worksheet);
pages.addAll(paginate) ;
}
printManager.updatePageNumberAndPageSettings(pages);
printManager.savePageInfosToPDF(path, pages);
} catch (Exception e) {
e.printStackTrace();
}
}
public List makeworksheet (PrintManager printManager,IWorksheet worksheet){
worksheet.getPageSetup().setPaperSize(PaperSize.A4);
worksheet.getPageSetup().setPrintHeadings(false);
worksheet.getPageSetup().setPrintGridlines(false);
worksheet.getPageSetup().setIsPercentScale(false);
worksheet.getPageSetup().setFitToPagesWide(1);
worksheet.getPageSetup().setFitToPagesTall(0);
worksheet.getPageSetup().getPrintTailRows();
worksheet.getPageSetup().setRightHeader("共 &N 页 第 &P 页");
List repeatSettings = new ArrayList();//要设置表头的区域集合 (多表头功能)
if(worksheet.getTables().getCount()>0){
for (int i = 0; i <worksheet.getTables().getCount() ; i++) {
IRange iRange=worksheet.getTables().get(i).getRange();
int row=iRange.getRow();//起始行
int lastRow=iRange.getLastRow();//结束行
RepeatSetting repeatSetting = new RepeatSetting();
repeatSetting.setTitleRowStart(row-1);
repeatSetting.setTitleRowEnd(row-1);
repeatSetting.setRange(worksheet.getRange("B"+row+":H"+lastRow));
repeatSettings.add(repeatSetting);
}
}
List paginate = printManager.paginate(worksheet, null, repeatSettings);
return paginate;
} |