Diversi modi per salvare i file

Diversi modi per salvare i file

Aspose.Cells fornisce il**Workbook** che rappresenta un file Excel Microsoft e fornisce le proprietà e i metodi necessari per lavorare con i file Excel. Il**Workbook** la classe fornisce il**Salva** metodo utilizzato per salvare i file Excel. Il**Salva**Il metodo ha molti overload che vengono utilizzati per salvare i file in modi diversi.

Il formato di file in cui viene salvato il file è deciso dal file**SaveFormat**enumerazione

Tipi di formati di 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 file modello XLTX di Excel 2007
Xltm Rappresenta un file XLTM abilitato per le macro di Excel 2007
Xlsb Rappresenta un file binario XLSB di Excel 2007
SpreadsheetML Rappresenta un file XML di foglio di calcolo
TSV Rappresenta un file di valori separati da tabulazioni
TabDelimited Rappresenta un file di testo delimitato da tabulazioni
ODS Rappresenta un file ODS
HTML Rappresenta HTML file(s)
HTML Rappresenta uno o più file MHTML
PDF Rappresenta un file PDF
XPS Rappresenta un documento XPS
TIFF Rappresenta il formato file immagine con tag (TIFF)

Salvataggio di 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 file desiderato (da**SaveFormat** enumerazione) quando si chiama il**Workbook** dell’oggetto**Salva**metodo.

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

Salvataggio cartella di lavoro in pdf

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 altro materiale di riferimento in un formato indipendente dal software applicativo, dall’hardware e dal sistema operativo. Il formato file PDF ha la piena capacità di contenere informazioni come testo, immagini, collegamenti ipertestuali, campi modulo, rich media, firme digitali, allegati, metadati, caratteristiche geospaziali e oggetti 3D che possono diventare parte del documento di origine.

seguenti codici mostrano come salvare la cartella di lavoro come file 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");
view raw Save-As-Pdf.cs hosted with ❤ by GitHub

Salvataggio della cartella di lavoro in formato testo o CSV

A volte, vuoi convertire o salvare una cartella di lavoro con più fogli di lavoro in formato testo. Per i formati di testo (ad esempio TXT, TabDelim, CSV, ecc.), per impostazione predefinita sia Microsoft Excel che Aspose.Cells salvano solo il contenuto del foglio di lavoro attivo.

L’esempio di codice seguente spiega come salvare un’intera cartella di lavoro in formato testo. Carica la cartella di lavoro di origine che potrebbe essere qualsiasi file di foglio di calcolo Excel o OpenOffice Microsoft (quindi XLS, XLSX, XLSM, XLSB, ODS e così via) con qualsiasi numero di fogli di lavoro.

Quando il codice viene eseguito, converte i dati di tutti i fogli nella cartella di lavoro nel formato TXT.

È possibile modificare lo stesso esempio per salvare il file in CSV. Per impostazione predefinita,**TxtSaveOptions.Separator**è una virgola, quindi non specificare un separatore se si salva nel formato CSV.

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

Salvataggio di file di testo con 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);

Salvataggio di file in un flusso

Per salvare i file in un flusso, creare un fileMemoryStream oFileStream oggetto e salva il file in quell’oggetto flusso chiamando il metodo**Workbook** dell’oggetto**Salva** metodo. Specificare il formato di file desiderato utilizzando il file**SaveFormat** enumerazione quando si chiama il**Salva**metodo.

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

Salvataggio di file come file Html e Mht

Aspose.Cells può semplicemente salvare un file 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");

Salvataggio come OpenOffice (ODS, SXC, FODS, OTS)

Possiamo salvare i file come 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");

Salvataggio del file Excel come JSON o XML

JSON (JavaScript Object Notation) è un formato di file standard aperto per la condivisione di dati che utilizza testo leggibile dall’uomo per archiviare e trasmettere dati. I file JSON vengono archiviati con l’estensione .json. JSON richiede meno formattazione ed è una buona alternativa per XML. JSON deriva da JavaScript ma è un formato di dati indipendente dalla lingua. La generazione e l’analisi di JSON è supportata da molti linguaggi di programmazione moderni. application/json è il tipo di supporto utilizzato 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");

Argomenti avanzati