Converti CSV, TSV e TXT in Excel
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 è fatto dall’API Aspose.Cells, come dimostrato nell’esempio di codice qui sotto.
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) |