目前,仅使用 GemBox.Spreadsheet 是不可能的,但您可以与 GemBox.Pdf 一起完成。
如果每个工作表都导出为单个页面,那么您可以这样做:
var workbook = ExcelFile.Load("input.xlsx");
// Set each worksheet to be exported as a single page.
foreach (var worksheet in workbook.Worksheets)
{
worksheet.PrintOptions.FitWorksheetWidthToPages = 1;
worksheet.PrintOptions.FitWorksheetHeightToPages = 1;
}
// Save the whole workbook as a PDF file.
var options = new GemBox.Spreadsheet.PdfSaveOptions();
options.SelectionType = SelectionType.EntireFile;
var stream = new MemoryStream();
workbook.Save(stream, options);
using (var document = PdfDocument.Load(stream))
{
// Add bookmark (or outline) for each PDF page (or Excel sheet).
for (int i = 0; i < document.Pages.Count; i++)
{
var page = document.Pages[i];
var sheet = workbook.Worksheets[i];
document.Outlines.AddLast(sheet.Name).SetDestination(page, PdfDestinationViewType.FitPage);
}
document.Save("output.pdf");
}
如果每个工作表都可以导出为多个页面,那么您可以这样做:
var workbook = ExcelFile.Load("input.xlsx");
var options = new GemBox.Spreadsheet.PdfSaveOptions();
using (var document = new PdfDocument())
{
foreach (var worksheet in workbook.Worksheets)
{
// Save each worksheet as a PDF file.
workbook.Worksheets.ActiveWorksheet = worksheet;
var stream = new MemoryStream();
workbook.Save(stream, options);
using (var temp = PdfDocument.Load(stream))
{
// Copy PDF pages to destination PDF and add a bookmark.
var firstPage = true;
foreach (var tempPage in temp.Pages)
{
var page = document.Pages.AddClone(tempPage);
if (firstPage)
{
document.Outlines.AddLast(worksheet.Name).SetDestination(page, PdfDestinationViewType.FitPage);
firstPage = false;
}
}
}
}
document.Save("output.pdf");
}