保存文件

不同的文件保存方式

Aspose.Cells提供了Workbook,它代表Microsoft Excel文件,并提供处理Excel文件所需的方法。Workbook类提供了Save方法,用于保存Excel文件。Save方法具有许多重载,用于以不同方式保存文件。保存文件的文件格式由SaveFormat枚举决定。

将文件保存到某个位置

要将文件保存到存储位置,请在调用Workbook对象的Save方法时指定文件名(包括存储路径)和所需的文件格式(来自SaveFormat枚举)。

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Source directory path
U16String dirPath(u"..\\Data\\LoadingSavingAndConverting\\");
//Output directory path
U16String outPath(u"..\\Data\\Output\\");
//Load sample Excel file
Workbook workbook(dirPath + u"sampleExcelFile.xlsx");
//Save in Excel 97-2003 format
workbook.Save(outPath + u"outputSavingFiletoSomeLocationExcel97-2003.xls");
//OR
workbook.Save(outPath + u"outputSavingFiletoSomeLocationOrExcel97-2003.xls", SaveFormat::Excel97To2003);
//Save in Excel2007 xlsx format
workbook.Save(outPath + u"outputSavingFiletoSomeLocationXlsx.xlsx", SaveFormat::Xlsx);
Aspose::Cells::Cleanup();

将文件保存到流

要将文件保存到流,请创建MemoryStream或FileStream对象,并通过调用Workbook对象的Save方法将文件保存到该流对象。在调用Save方法时使用SaveFormat枚举指定所需的文件格式。

Aspose::Cells::Startup();
//Source directory path
U16String dirPath(u"");
//Output directory path
U16String outPath(u"");
//Load sample Excel file
Workbook workbook(dirPath + u"wdd.xlsx");
//Create FileStream object
std::ofstream fileStream(outPath.ToUtf8() + "wdd2.xlsx", std::ios::binary);
//Save the Workbook to Stream
Vector<uint8_t> stream = workbook.Save(SaveFormat::Xlsx);
fileStream.write(reinterpret_cast<char*>(stream.GetData()), stream.GetLength());
fileStream.close();
Aspose::Cells::Cleanup();

将文件保存为 PDF

要使用Aspose.Cells for C++库将所需内容保存到PDF文件,请创建新的Workbook对象或通过读取现有的Excel文件来构建Workbook对象,然后通过调用Workbook对象的Save方法将文件保存为PDF。在调用Save方法时,使用SaveFormat枚举来指定所需的文件格式。

Aspose::Cells::Startup();
//load/creat the Excel file
Workbook wb;
//Set cell value
wb.GetWorksheets().Get(0).GetCells().Get(u"A1").PutValue(u"test");
//Set security options using PdfSecurityOptions class
PdfSecurityOptions securityOptions;
securityOptions.SetOwnerPassword(u"123");
securityOptions.SetPrintPermission(true);
securityOptions.SetAccessibilityExtractContent(true);
//Set saving PDF parameters
PdfSaveOptions pdfSaveOptions;
pdfSaveOptions.SetSecurityOptions(securityOptions);
//Save encrypted PDF files
wb.Save("security.pdf", pdfSaveOptions);
Aspose::Cells::Cleanup();