打开不同格式的文件
使用 Aspose.Cells 您可以打开不同格式的文件。Aspose.Cells可以打开一系列文件格式,例如 Microsoft Excel 电子表格(XLS、XLSX、XLSM、XLSB)、SpreadsheetML、逗号分隔值 (CSV)、制表符分隔或制表符分隔值 (TSV) 文件等。
如果您需要了解所有支持的文件格式,请参阅以下页面: 支持的文件格式
打开不同格式的文件
Aspose.Cells 允许开发人员打开不同格式的电子表格文件,例如 SpreadsheetML、逗号分隔值 (CSV)、制表符分隔或制表符分隔值 (TSV)、ODS 文件。要打开此类文件,开发人员可以使用与打开不同 Microsoft Excel 版本的文件相同的方法。
打开 SpreadsheetML 文件
SpreadsheetML 文件是电子表格的 XML 表示,包括有关它的所有信息,例如格式、公式等。自 Microsoft Excel XP 起,XML 导出选项添加到 Microsoft Excel,可将电子表格导出到 SpreadsheetML 文件。
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() |
打开 HTML 文件
Aspose.Cells 允许您将 HTML 文件打开到工作簿对象中。 HTML 文件应该是面向 Microsoft Excel 的,即 MS-Excel 应该能够打开它。
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() |
打开 CSV 文件
逗号分隔值 (CSV) 文件包含值以逗号分隔的记录。数据存储为表格,其中每列由逗号分隔并由双引号引起来。如果字段值包含双引号字符,则使用一对双引号字符进行转义。也可以用Microsoft Excel导出电子表格数据到CSV。
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() |
打开 CSV 文件并替换无效字符
在 Excel 中,打开带有特殊字符的 CSV 文件时,会自动替换这些字符。 Aspose.Cells API 完成了同样的操作,在下面给出的代码示例中进行了演示。
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() |
可以从以下链接下载示例源文件以测试此功能。
使用自定义分隔符打开文本文件
文本文件用于保存不带格式的电子表格数据。该文件是一种纯文本文件,可以有一些自定义的分隔符。
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() |
可以从以下链接下载示例源文件以测试此功能。
打开制表符分隔的文件
制表符分隔(文本)文件包含电子表格数据,但没有任何格式。数据以行和列的形式排列,就像在表格和电子表格中一样。基本上,制表符分隔文件是一种特殊的纯文本文件,每列之间有一个制表符。
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() |
可以从以下链接下载示例源文件以测试此功能。
打开制表符分隔值 (TSV) 文件
制表符分隔值 (TSV) 文件包含电子表格数据,但没有任何格式。这与制表符分隔文件相同,其中数据按行和列排列,就像在表格和电子表格中一样。
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() |
打开 SXC 文件
StarSuite Calc 类似于 Microsoft Excel,支持公式、图表、函数和宏。使用此软件创建的电子表格以 SXC 扩展名保存。 SXC 文件也用于 OpenOffice.org Calc 电子表格文件。 Aspose.Cells 可以读取 SXC 文件,如以下代码示例所示。
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() |
打开 FODS 文件
FODS 文件是以 OpenDocument XML 格式保存的电子表格,没有任何压缩。 Aspose.Cells 可以读取 FODS 文件,如以下代码示例所示。
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() |