Saving Files
Different Ways to Save Files
Aspose.Cells provides the Workbook which represents a Microsoft Excel file and provides methods necessary to work with Excel files. The Workbook class provides the Save method used to save Excel files. The Save method has many overloads that are used to save files in different ways. The file format that the file is saved to is decided by the SaveFormat enumeration.
Saving File to Some Location
To save files to a storage location, specify the file name (complete with storage path) and the desired file format (from the SaveFormat enumeration) when calling the Workbook object’s Save method.
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(); |
Saving File to Stream
To save files to a stream, create a MemoryStream or FileStream object and save the file to that stream object by calling the Workbook object’s Save method. Specify the desired file format using the SaveFormat enumeration when calling the Save method.
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(); |
Saving File to PDF
To save the desired content to a PDF file using the Aspose.Cells for C++ library, create a new Workbook object or construct a Workbook object by reading an existing Excel file, and then Save the file to PDF by calling the Save method of the Workbook object. When calling the Save method, use the SaveFormat enumeration to specify the desired file format.
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(); |