Apertura dei file con formati diversi

Apertura di file con formati diversi

Aspose.Cells per Python via .NET consente agli sviluppatori di aprire file di fogli di calcolo con formati diversi come SpreadsheetML, valori separati da virgola (CSV), delimitati da tabulazione o valori separati da tabulazione (TSV), file ODS. Per aprire tali file, gli sviluppatori possono usare la stessa metodologia che usano per aprire file di diverse versioni di Microsoft Excel.

Apertura dei file SpreadsheetML

I file SpreadsheetML sono rappresentazioni XML di fogli di calcolo che includono tutte le informazioni ad esse relative, come formattazione, formule, ecc. Dall’introduzione di Microsoft Excel XP, è stata aggiunta un’opzione di esportazione XML a Microsoft Excel che esporta i fogli di calcolo in file SpreadsheetML.

from aspose.cells import LoadFormat, LoadOptions, 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.
dataDir = RunExamples.GetDataDir(".")
# Opening SpreadsheetML Files
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions3 = LoadOptions(LoadFormat.SPREADSHEET_ML)
# Create a Workbook object and opening the file from its path
wbSpreadSheetML = Workbook(dataDir + "Book3.xml", loadOptions3)
print("SpreadSheetML file opened successfully!")

Apertura dei file HTML

Aspose.Cells per Python via .NET ti consente di aprire un file HTML in un oggetto Workbook. Il file HTML dovrebbe essere orientato a Microsoft Excel, cioè MS-Excel dovrebbe essere in grado di aprirlo.

from aspose.cells import HtmlLoadOptions, LoadFormat, 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.
dataDir = RunExamples.GetDataDir(".")
filePath = dataDir + "Book1.html"
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = HtmlLoadOptions(LoadFormat.HTML)
# Create a Workbook object and opening the file from its path
wb = Workbook(filePath, loadOptions)
# Save the MHT file
wb.save(filePath + "output.xlsx")

Apertura dei file CSV

I file di valori separati da virgola (CSV) contengono record in cui i valori sono separati da virgole. I dati sono memorizzati come una tabella in cui ciascuna colonna è separata dal carattere virgola e citata dal carattere virgoletta doppia. Se un valore di campo contiene un carattere di virgoletta doppia, viene eseguito l’escape con una coppia di caratteri virgoletta doppia. È anche possibile utilizzare Microsoft Excel per esportare i dati del foglio di calcolo in CSV.

from aspose.cells import LoadFormat, LoadOptions, 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.
dataDir = RunExamples.GetDataDir(".")
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions4 = LoadOptions(LoadFormat.CSV)
# Create a Workbook object and opening the file from its path
wbCSV = Workbook(dataDir + "Book_CSV.csv", loadOptions4)
print("CSV file opened successfully!")

Apertura dei file CSV e sostituzione dei caratteri non validi

In Excel, quando si apre un file CSV con caratteri speciali, i caratteri vengono automaticamente sostituiti. Lo stesso avviene con l’API Aspose.Cells per Python via .NET, come dimostrato nell’esempio di codice sottostante.

from aspose.cells import LoadDataFilterOptions, LoadFilter, TxtLoadOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
filename = sourceDir + "[20180220142533][ASPOSE_CELLS_TEST].csv"
options = TxtLoadOptions()
options.separator = ';'
options.load_filter = LoadFilter(LoadDataFilterOptions.CELL_DATA)
options.check_excel_restriction = False
options.convert_numeric_data = False
options.convert_date_time_data = false
# Load CSV file
workbook = Workbook(filename, options)
print(workbook.worksheets[0].name)
print(len(workbook.worksheets[0].name))
print("CSV file opened successfully!")

Apertura dei file delimitati da tabulazione

Il file delimitato da tabulazione (Testo) contiene dati del foglio elettronico ma senza alcuna formattazione. I dati sono disposti in righe e colonne come nelle tabelle e nei fogli elettronici. Fondamentalmente, un file delimitato da tabulazione è un tipo speciale di file di testo semplice con una tabulazione tra ogni colonna.

from aspose.cells import LoadFormat, LoadOptions, 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.
dataDir = RunExamples.GetDataDir(".")
# Opening Tab Delimited Files
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions5 = LoadOptions(LoadFormat.TAB_DELIMITED)
# Create a Workbook object and opening the file from its path
wbTabDelimited = Workbook(dataDir + "Book1TabDelimited.txt", loadOptions5)
print("Tab delimited file opened successfully!")

Apertura dei file di valori separati da tabulazione (TSV)

Il file di valori separati da tabulazione (TSV) contiene dati del foglio elettronico ma senza alcuna formattazione. È lo stesso con il file delimitato da tabulazione dove i dati sono disposti in righe e colonne come nelle tabelle e nei fogli elettronici.

from aspose.cells import LoadFormat, LoadOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = LoadOptions(LoadFormat.TSV)
# Create a Workbook object and opening the file from its path
workbook = Workbook(sourceDir + "SampleTSVFile.tsv", loadOptions)
# Using the Sheet 1 in Workbook
worksheet = workbook.worksheets[0]
# Accessing a cell using its name
cell = worksheet.cells.get("C3")
print("Cell Name: " + cell.name + " Value: " + cell.string_value)

Apertura dei file SXC

StarOffice Calc è simile a Microsoft Excel e supporta formule, grafici, funzioni e macro. I fogli di calcolo creati con questo software vengono salvati con l’estensione SXC. Il file SXC è anche utilizzato per i file di spreadsheet di OpenOffice.org Calc. Aspose.Cells per Python via .NET può leggere i file SXC come mostrato nell’esempio di codice seguente.

from aspose.cells import LoadFormat, LoadOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = LoadOptions(LoadFormat.SXC)
# Create a Workbook object and opening the file from its path
workbook = Workbook(sourceDir + "SampleSXC.sxc", loadOptions)
# Using the Sheet 1 in Workbook
worksheet = workbook.worksheets[0]
# Accessing a cell using its name
cell = worksheet.cells.get("C3")
print("Cell Name: " + cell.name + " Value: " + cell.string_value)

Apertura dei file FODS

Il file FODS è un foglio di calcolo salvato in OpenDocument XML senza compressione. Aspose.Cells per Python via .NET può leggere i file FODS come dimostrato dall’esempio di codice seguente.

from aspose.cells import LoadFormat, LoadOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = LoadOptions(LoadFormat.FODS)
# Create a Workbook object and opening the file from its path
workbook = Workbook(sourceDir + "SampleFods.fods", loadOptions)
print("FODS file opened successfully!")