[]
        
(Showing Draft Content)

将工作表导出为PDF

GcExcel提供了一个选项,可以在导出PDF文件时,根据页面边界自动对工作表分页。

PrintManager类的GetPaginationInfo方法获取用于水平和垂直分页的页面边界数组。对于水平和垂直分页,需要分别调用该方法。检索到分页信息设置。还可以定义工作表的打印区域。如果未定义默认区域,则从单元格A1到存在任何单元格数据的最后一个单元格将作为打印区域。

除此之外,还可以定义一片区域并通过重载GetPaginationInfo方法来设置合并打印和重复打印。

代码用例

请参阅以下示例代码,以便在根据页面设置在导出PDF文件时为工作表分页。

 IWorkbook workbook = new Workbook();
 IWorksheet worksheet = workbook.getWorksheets().get(0);

 // The row and column headings are printed
 worksheet.getPageSetup().setPrintHeadings(true);
 
 // The range "B6:N80" will be printed
 worksheet.getPageSetup().setPrintArea("B6:N80");

 // Set data
 worksheet.getRange("B6:S8").setValue(10);
 // Add a table
worksheet.getTables().add(worksheet.getRange("B6:N20"), true);

 PrintManager printManager = new PrintManager();
 
           
 // The columnIndexs is [9, 13], this means that the horizontal direction is split after the column 10th and 14th
 List columnIndexs = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Horizontal);
 
 // The rowIndexs is [50, 79], this means that the vertical direction is split after the row 51th and 80th
 List rowIndexs = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Vertical);
    
 worksheet.save("Export.pdf", SaveFileFormat.Pdf);

请参阅以下示例代码,以便在导出为PDF文件时根据页面设置来设置合并打印和重复打印。

   // create a new workbook
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.getWorksheets().get(0);

// The row and column headings are printed
worksheet.getPageSetup().setPrintHeadings(true);
// The range "B6:N80" will be printed
worksheet.getPageSetup().setPrintArea("B6:N80");
// Set data
worksheet.getRange("B60:N80").setValue(1);
// Add a table
worksheet.getTables().add(worksheet.getRange("B6:N20"), true);
// The row 6th will be printed at the top of each page
ArrayList repeatSettings = new ArrayList();

RepeatSetting repeatSetting = new RepeatSetting();
repeatSetting.setTitleRowStart(5);
repeatSetting.setTitleRowEnd(5);
repeatSetting.setRange(worksheet.getRange("B6:N80"));
repeatSettings.add(repeatSetting);
// The rows from 25th to 60th should be paged to one page
ArrayList keepTogetherRanges = new ArrayList();

keepTogetherRanges.add(worksheet.getRange("$25:$60"));
PrintManager printManager = new PrintManager();
// The columnIndexs is [9, 13], this means that the horizontal direction is
// split after the column 10th and 14th.
List columnIndexs = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Horizontal,
keepTogetherRanges, repeatSettings);
// The rowIndexs is [23, 66, 79], this means that the vertical direction is
// split after the row 24th, 67th and 80th.
List rowIndexs = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Vertical,
keepTogetherRanges, repeatSettings);

List pages = printManager.paginate(worksheet, keepTogetherRanges, repeatSettings);
printManager.savePageInfosToPDF("GetPaginationRangesRepeatSettings.pdf", pages);