Spara filer

Olika sätt att spara filer

Aspose.Cells tillhandahåller Workbook som representerar en Microsoft Excel-fil och tillhandahåller de metoder som behövs för att arbeta med Excel-filer. Klassen Workbook tillhandahåller metoden Save som används för att spara Excel-filer. Metoden Save har många överbelastningar som används för att spara filer på olika sätt. Filformatet som filen sparas till bestäms av uppräkningen SaveFormat.

Spara fil till en plats

För att spara filer till en lagringsplats, ange filnamnet (kompletterat med lagringsvägen) och önskat filformat (från uppräkningen SaveFormat) när du anropar Workbook objektets Save metod.

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();

Spara fil till ström

För att spara filer till en ström, skapa ett MemoryStream- eller FileStream objekt och spara filen i det strömobjektet genom att anropa Workbook objektets Save metod. Ange det önskade filformatet med uppräkningen SaveFormat vid anrop av Save metoden.

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();

Spara fil till PDF

För att spara önskat innehåll till en PDF-fil med hjälp av biblioteket Aspose.Cells for C++, skapa en ny Workbook objekt eller konstruera ett Workbook objekt genom att läsa in en befintlig Excel-fil och sedan spara filen som PDF genom att anropa Save-metoden för Workbook objektet. När du anropar Save-metoden, använd uppräkningen SaveFormat för att ange det önskade filformatet.

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();