Öffnen von Dateien mit verschiedenen Formaten

Öffnen von Dateien mit verschiedenen Formaten

Aspose.Cells ermöglicht Entwicklern, Tabellendateien mit verschiedenen Formaten wie SpreadsheetML, durch Kommas getrennte Werte (CSV), Tab- oder Tabstopp-getrennte Werte (TSV), ODS-Dateien zu öffnen. Um solche Dateien zu öffnen, können Entwickler die gleiche Methodik verwenden wie beim Öffnen von Dateien verschiedener Microsoft Excel-Versionen.

Öffnen von SpreadsheetML-Dateien

SpreadsheetML-Dateien sind XML-Repräsentationen von Tabellen, die alle Informationen darüber enthalten, wie z.B. Formatierung, Formeln usw. Seit Microsoft Excel XP gibt es eine XML-Exportoption in Microsoft Excel, die Ihre Tabellen als SpreadsheetML-Dateien exportiert.

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook, LoadOptions, LoadFormat
# The path to the documents directory.
dataDir = ""
# Opening SpreadsheetML Files
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = LoadOptions(LoadFormat.SPREADSHEET_ML)
# Create a Workbook object and opening the file from its path
workbook = Workbook(dataDir + "Input.xml", loadOptions)
print("SpreadSheetML file opened successfully!")
jpype.shutdownJVM()

Öffnen von HTML-Dateien

Aspose.Cells ermöglicht es Ihnen, eine HTML-Datei in ein Arbeitsmappenobjekt zu öffnen. Die HTML-Datei sollte auf Microsoft Excel ausgerichtet sein, d.h. MS-Excel sollte in der Lage sein, sie zu öffnen.

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook, LoadOptions, LoadFormat
# The path to the documents directory.
dataDir = ""
# Opening HTML Files
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = LoadOptions(LoadFormat.HTML)
# Create a Workbook object and opening the file from its path
workbook = Workbook(dataDir + "Input.html", loadOptions)
print("HTML file opened successfully!")
jpype.shutdownJVM()
view raw OpenHTMLFile.py hosted with ❤ by GitHub

Ö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.

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook, LoadOptions, LoadFormat
# The path to the documents directory.
dataDir = ""
# Opening CSV Files
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = LoadOptions(LoadFormat.CSV)
# Create a Workbook object and opening the file from its path
workbook = Workbook(dataDir + "Input.csv", loadOptions)
print("CSV file opened successfully!")
jpype.shutdownJVM()
view raw OpenCSVFile.py hosted with ❤ by GitHub

Ö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.

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook, TxtLoadOptions, LoadFormat
# The path to the documents directory.
dataDir = ""
# Opening CSV Files
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = TxtLoadOptions(LoadFormat.CSV)
loadOptions.setSeparator(';')
# Create a Workbook object and opening the file from its path
workbook = Workbook(dataDir + "InvalidCharacters.csv", loadOptions)
print("CSV file opened successfully!")
# Save for check
workbook.save("Output.xlsx")
jpype.shutdownJVM()

Die Beispielquelle kann von den folgenden Links heruntergeladen werden, um diese Funktion zu testen.

InvalidCharacters.csv

Öffnen von Textdateien mit benutzerdefiniertem Trennzeichen

Textdateien werden verwendet, um Tabellendaten ohne Formatierung zu halten. Die Datei ist eine Art reine Textdatei, die einige benutzerdefinierte Trennzeichen haben kann.

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook, TxtLoadOptions
# The path to the documents directory.
dataDir = ""
# Set for TxtLoadOptions
loadOptions = TxtLoadOptions()
loadOptions.setSeparator(',')
# Create a Workbook object and opening the file from its path
workbook = Workbook(dataDir + "CustomSeparator.txt", loadOptions)
print("TXT file opened successfully!")
# Save for check
workbook.save("Output.xlsx")
jpype.shutdownJVM()

Die Beispielquelle kann von den folgenden Links heruntergeladen werden, um diese Funktion zu testen.

CustomSeparator.txt

Ö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.

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook, LoadFormat, LoadOptions
# The path to the documents directory.
dataDir = ""
# Set LoadOptions
loadOptions = LoadOptions(LoadFormat.TAB_DELIMITED)
# Create a Workbook object and opening the file from its path
workbook = Workbook(dataDir + "TabDelimited.txt", loadOptions)
print("TabDelimited file opened successfully!")
# Save for check
workbook.save("Output.xlsx")
jpype.shutdownJVM()

Die Beispielquelle kann von den folgenden Links heruntergeladen werden, um diese Funktion zu testen.

TabDelimited.txt

Ö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.

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook, LoadFormat, LoadOptions
# The path to the documents directory.
dataDir = ""
# Set LoadOptions
loadOptions = LoadOptions(LoadFormat.TSV)
# Create a Workbook object and opening the file from its path
workbook = Workbook(dataDir + "Input.tsv", loadOptions)
print("TSV file opened successfully!")
# Save for check
workbook.save("Output.xlsx")
jpype.shutdownJVM()
view raw OpenTSVFile.py hosted with ❤ by GitHub

Öffnen von SXC Dateien

StarOffice Calc ist ähnlich wie Microsoft Excel und unterstützt Formeln, Diagramme, Funktionen und Makros. Die mit dieser Software erstellten Tabellenkalkulationen werden mit der SXC-Erweiterung gespeichert. Die SXC-Datei wird auch für OpenOffice.org Calc-Tabellenkalkulationsdateien verwendet. Aspose.Cells kann SXC-Dateien lesen, wie im folgenden Codebeispiel demonstriert wird.

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook, LoadFormat, LoadOptions
# The path to the documents directory.
dataDir = ""
# Set LoadOptions
loadOptions = LoadOptions(LoadFormat.SXC)
# Create a Workbook object and opening the file from its path
workbook = Workbook(dataDir + "Input.sxc", loadOptions)
print("SXC file opened successfully!")
# Save for check
workbook.save("Output.xlsx")
jpype.shutdownJVM()
view raw OpenSXCFile.py hosted with ❤ by GitHub

Öffnen von FODS Dateien

Eine FODS-Datei ist eine in OpenDocument XML ohne jegliche Komprimierung gespeicherte Tabelle. Aspose.Cells kann FODS-Dateien lesen, wie im folgenden Codebeispiel demonstriert wird.

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook, LoadFormat, LoadOptions
# The path to the documents directory.
dataDir = ""
# Set LoadOptions
loadOptions = LoadOptions(LoadFormat.FODS)
# Create a Workbook object and opening the file from its path
workbook = Workbook(dataDir + "Input.fods", loadOptions)
print("FODS file opened successfully!")
# Save for check
workbook.save("Output.xlsx")
jpype.shutdownJVM()
view raw OpenFODSFile.py hosted with ❤ by GitHub