将每个工作表保存到不同的 PDF 文件
Contents
[
Hide
]
Aspose.Cells 支持将 XLS 文件(包含图像、图表等)转换为 PDF 文档。 Aspose.Cells for .NET 可以独立工作,将电子表格转换为 PDF,您不需要使用 Aspose.PDF for .NET 进行转换。转换不需要软件创建或使用任何临时文件,因为整个过程可以在内存中完成。
将每个工作表保存到不同的 PDF 文件
如果您需要将每个工作表保存在模板 Excel 文件中以生成不同的 PDF 文件,您可以轻松实现。您可以尝试隐藏文件中的工作表并一次显示一张工作表以渲染到 PDF。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Get the Excel file path | |
string filePath = dataDir + "input.xlsx"; | |
// Instantiage a new workbook and open the Excel, File from its location | |
Workbook workbook = new Workbook(filePath); | |
// Get the count of the worksheets in the workbook | |
int sheetCount = workbook.Worksheets.Count; | |
// Define PdfSaveOptions | |
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
// Take Pdfs of each sheet | |
for (int j = 0; j < sheetCount; j++) | |
{ | |
Worksheet ws = workbook.Worksheets[j]; | |
//set worksheet to output | |
SheetSet sheetSet = new SheetSet(new int[] { ws.Index }); | |
pdfSaveOptions.SheetSet = sheetSet; | |
workbook.Save(dataDir + "worksheet-" + ws.Name + ".out.pdf", pdfSaveOptions); | |
} |
如果您的电子表格包含公式,最好调用工作簿.CalculateFormula()就在将电子表格呈现为 PDF 格式之前。这样做将确保重新计算公式相关值,并在 PDF 中呈现正确的值。