Diversi modi per salvare i file
Diversi modi per salvare i file
Aspose.Cells fornisce il Workbook che rappresenta un file di Microsoft Excel e fornisce le proprietà e i metodi necessari per lavorare con i file di Excel. La classe Workbook fornisce il metodo Save utilizzato per salvare i file di Excel. Il metodo Save ha molte sovraccarichi che vengono utilizzati per salvare file in modi diversi.
Il formato del file in cui il file viene salvato è deciso dall’enumerazione SaveFormat
Tipi di formato file | Descrizione |
---|---|
CSV | Rappresenta un file CSV |
Excel97To2003 | Rappresenta un file Excel 97 - 2003 |
Xlsx | Rappresenta un file Excel 2007 XLSX |
Xlsm | Rappresenta un file Excel 2007 XLSM |
Xltx | Rappresenta un modello di Excel 2007 XLTX |
Xltm | Rappresenta un file XLTM abilitato per macro di Excel 2007 |
Xlsb | Rappresenta un file XLSB binario di Excel 2007 |
SpreadsheetML | Rappresenta un file XML di fogli di calcolo |
TSV | Rappresenta un file di valori separati da tabulazione |
TabDelimited | Rappresenta un file di testo delimitato da tabulazioni |
ODS | Rappresenta un file ODS |
Html | Rappresenta file HTML |
MHtml | Rappresenta file MHTML |
Rappresenta un file PDF | |
XPS | Rappresenta un documento XPS |
TIFF | Rappresenta il formato di file immagine TIFF (Tagged Image File Format) |
Come Salvare File in Diversi Formati
Per salvare i file in una posizione di archiviazione, specificare il nome del file (completo di percorso di archiviazione) e il formato desiderato del file (dall’enumerazione SaveFormat) durante la chiamata del metodo Save dell’oggetto Workbook.
// 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); | |
string filePath = dataDir + "Book1.xls"; | |
// Load your source workbook | |
Workbook workbook = new Workbook(filePath); | |
// Save in Excel 97 to 2003 format | |
workbook.Save(dataDir + ".output.xls"); | |
// OR | |
workbook.Save(dataDir + ".output.xls", new XlsSaveOptions()); | |
// Save in Excel2007 xlsx format | |
workbook.Save(dataDir + ".output.xlsx", SaveFormat.Xlsx); | |
// Save in Excel2007 xlsb format | |
workbook.Save(dataDir + ".output.xlsb", SaveFormat.Xlsb); | |
// Save in ODS format | |
workbook.Save(dataDir + ".output.ods", SaveFormat.Ods); | |
// Save in Pdf format | |
workbook.Save(dataDir + ".output.pdf", SaveFormat.Pdf); | |
// Save in Html format | |
workbook.Save(dataDir + ".output.html", SaveFormat.Html); | |
// Save in SpreadsheetML format | |
workbook.Save(dataDir + ".output.xml", SaveFormat.SpreadsheetML); | |
Come Salvare un Workbook in Pdf
Il formato di file Portable Document Format (PDF) è un tipo di documento creato da Adobe negli anni ‘90. Lo scopo di questo formato di file era introdurre uno standard per la rappresentazione di documenti e di altro materiale di riferimento in un formato indipendente dal software dell’applicazione, dall’hardware e dal Sistema Operativo. Il formato di file PDF ha la piena capacità di contenere informazioni come testo, immagini, collegamenti ipertestuali, campi modulo, media ricca, firme digitali, allegati, metadati, funzionalità geospaziali e oggetti 3D che possono diventare parte del documento di origine.
I seguenti codici mostrano come salvare il workbook in formato pdf con Aspose.Cells:
// Instantiate the Workbook object | |
Workbook workbook = new Workbook(); | |
//Set value to Cell. | |
workbook.Worksheets[0].Cells["A1"].PutValue("Hello World!"); | |
workbook.Save("pdf1.pdf"); | |
// Save as Pdf format compatible with PDFA-1a | |
PdfSaveOptions saveOptions = new PdfSaveOptions(); | |
saveOptions.Compliance = PdfCompliance.PdfA1a; | |
workbook.Save("pdfa1a.pdf"); |
Come Salvare un Workbook in Formato Testo o CSV
A volte si desidera convertire o salvare un workbook con più fogli di lavoro in formato testo. Per i formati di testo (ad esempio TXT, TabDelim, CSV, ecc.), sia Microsoft Excel che Aspose.Cells di default salvano solo i contenuti del foglio di lavoro attivo.
L’esempio di codice seguente spiega come salvare un intero workbook in formato testo. Carica il workbook di origine che potrebbe essere un file di fogli di calcolo Microsoft Excel o OpenOffice (quindi XLS, XLSX, XLSM, XLSB, ODS e così via) con un qualsiasi numero di fogli di lavoro.
Quando il codice viene eseguito, converte i dati di tutti i fogli nel workbook nel formato TXT.
Puoi modificare lo stesso esempio per salvare il tuo file in CSV. Per default, TxtSaveOptions.Separator è una virgola, quindi non specificare un separatore se si salva nel formato CSV. Nota: Se stai usando la versione di valutazione e anche se la proprietà TxtSaveOptions.ExportAllSheets è impostata su true, il programma esporterà comunque solo un foglio di lavoro.
// 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); | |
// Load your source workbook | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Text save options. You can use any type of separator | |
TxtSaveOptions opts = new TxtSaveOptions(); | |
opts.Separator = '\t'; | |
opts.ExportAllSheets = true; | |
// Save entire workbook data into file | |
workbook.Save(dataDir + "out.txt", opts); |
Come salvare un file in file di testo con un separatore personalizzato
I file di testo contengono dati del foglio di calcolo senza formattazione. Il file è una sorta di file di testo semplice che può avere alcuni delimitatori personalizzati tra i suoi dati.
// 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); | |
string filePath = dataDir + "Book1.xlsx"; | |
// Create a Workbook object and opening the file from its path | |
Workbook wb = new Workbook(filePath); | |
// Instantiate Text File's Save Options | |
TxtSaveOptions options = new TxtSaveOptions(); | |
// Specify the separator | |
options.Separator = Convert.ToChar(";"); | |
// Save the file with the options | |
wb.Save(dataDir + "output.csv", options); | |
Come salvare un file in uno stream
Per salvare i file in uno stream, creare un oggetto MemoryStream o FileStream e salvare il file su quell’oggetto stream chiamando il metodo Save dell’oggetto Workbook. Specificare il formato del file desiderato utilizzando l’enumerazione SaveFormat al momento della chiamata del metodo Save.
public async Task<IActionResult> DownloadExcel() | |
{ | |
// 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); | |
string filePath = dataDir + "Book1.xlsx"; | |
// Load your source workbook | |
Workbook workbook = new Workbook(filePath); | |
// Save the workbook to a memory stream | |
var stream = new MemoryStream(); | |
workbook.Save(stream, SaveFormat.Xlsx); | |
// Reset the position of the stream to 0 | |
stream.Position = 0; | |
// Set the content type and file name | |
var contentType = "application/octet-stream"; | |
var fileName = "output.xlsx"; | |
// Set the response headers | |
Response.Headers.Add("Content-Disposition", $"attachment; filename=\"{fileName}\""); | |
Response.ContentType = contentType; | |
// Write the file contents to the response body stream | |
await stream.CopyToAsync(Response.Body); | |
// Close the file stream | |
stream.Dispose(); | |
// Return the response | |
return new EmptyResult(); | |
} |
Come salvare un file di Excel in file Html e Mht
Aspose.Cells può semplicemente salvare un file di Excel, JSON, CSV o altri file che potrebbero essere caricati da Aspose.Cells come file .html e .mht.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Load your source workbook | |
Workbook workbook = new Workbook("Book1.xlsx"); | |
//save file to mhtml format | |
workbook.Save("out.mht"); |
Come salvare un file di Excel in OpenOffice (ODS, SXC, FODS, OTS)
Possiamo salvare i file in formato open office: ODS, SXC, FODS, OTS ecc.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Load your source workbook | |
Workbook workbook = new Workbook("book1.xlsx"); | |
// Save as ods file | |
workbook.Save("Out.ods"); | |
// Save as sxc file | |
workbook.Save("Out.sxc"); | |
// Save as fods file | |
workbook.Save("Out.fods"); |
Come salvare un file di Excel in JSON o XML
JSON (JavaScript Object Notation) è un formato file standard aperto per la condivisione di dati che utilizza testo leggibile dall’uomo per memorizzare e trasmettere dati. I file JSON sono memorizzati con l’estensione .json. JSON richiede meno formattazione ed è una buona alternativa per XML. JSON deriva da JavaScript, ma è un formato di dati indipendente dal linguaggio. La generazione e l’analisi di JSON sono supportate da molti linguaggi di programmazione moderni. application/json è il tipo di supporto usato per JSON.
Aspose.Cells supporta il salvataggio dei file in JSON o XML.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Load your source workbook | |
Workbook workbook = new Workbook("Book1.xlsx"); | |
// convert the workbook to json file. | |
workbook.Save(dir + "book1.json"); |