Konvertieren Sie CSV, TSV und TXT in Excel
Öffnen von CSV-Dateien
Durch Kommas getrennte Werte (CSV)-Dateien enthalten Datensätze, bei denen die Werte durch Kommas getrennt sind. Daten werden als Tabelle gespeichert, wobei jeder Spalte das Kommazeichen trennt und durch das doppelte Anführungszeichen gekennzeichnet ist. Enthält ein Feldwert ein doppeltes Anführungszeichen, wird es mit einem Paar doppelter Anführungszeichen maskiert. Sie können auch Microsoft Excel verwenden, um Tabellendaten in CSV zu exportieren.
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!") |
Öffnen von CSV-Dateien und Ersetzen ungültiger Zeichen
In Excel werden beim Öffnen von CSV-Dateien mit Sonderzeichen die Zeichen automatisch ersetzt. Das gleiche geschieht durch die Aspose.Cells API, wie im folgenden Beispielcode dargestellt.
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!") |
Öffnen von tabstoppgetrennten Dateien
Eine tabstoppgetrennte (Text)Datei enthält Tabellendaten, jedoch ohne jegliche Formatierung. Die Daten sind in Zeilen und Spalten wie in Tabellen und Tabellenkalkulationen angeordnet. Im Grunde genommen ist eine tabstoppgetrennte Datei eine spezielle Art einer einfachen Textdatei, bei der ein Tabulator zwischen jeder Spalte steht.
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!") |
Öffnen von tabstoppgetrennten Werten (TSV) Dateien
Eine tabstoppgetrennte Werte (TSV) Datei enthält Tabellendaten, jedoch ohne jegliche Formatierung. Es ist dasselbe wie bei einer tabstoppgetrennten Datei, bei der die Daten in Zeilen und Spalten wie in Tabellen und Tabellenkalkulationen angeordnet sind.
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) |