打开不同格式的文件

打开具有不同格式的文件

Aspose.Cells for Python via .NET 允许开发者以不同格式打开电子表格文件,如SpreadsheetML、CSV、TSV、ODS文件。开发者可以用与打开不同Microsoft Excel版本文件相同的方法打开这些文件。

打开电子表格 ML 文件

电子表格 ML 文件是电子表格的 XML 表示,包括有关其所有信息的格式、公式等等。 自 Microsoft Excel XP 以来,已向 Microsoft Excel 添加了导出到电子表格 ML 文件的 XML 导出选项。

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!")

打开 HTML 文件

Aspose.Cells for Python via .NET 支持将HTML文件导入为Workbook对象。HTML文件应为Microsoft Excel格式,即应能用MS-Excel打开。

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")

打开 CSV 文件

逗号分隔值(CSV)文件包含记录,其中值由逗号分隔。 数据存储为表格,其中每列由逗号字符分隔并由双引号字符引用。 如果字段值包含双引号字符,则用一对双引号字符进行转义。 您还可以使用 Microsoft Excel 将电子表格数据导出到 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!")

打开 CSV 文件并替换无效字符

在Excel中,当打开带有特殊字符的CSV文件时,字符会被自动替换。Aspose.Cells for Python via .NET API 也会这样,以下的示例代码演示了这一点。

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!")

打开制表符分隔文件

制表符分隔(文本)文件包含电子表格数据,但没有任何格式。数据按行和列排列,就像在表格和电子表格中一样。基本上,制表符分隔文件是一种带有每个列之间制表符的特殊类型的纯文本文件。

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!")

打开制表符分隔数值(TSV)文件

制表符分隔数值(TSV)文件包含电子表格数据,但没有任何格式。它与制表符分隔文件相同,其中数据按行和列排列,就像在表格和电子表格中一样。

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)

打开SXC文件

StarOffice Calc类似于Microsoft Excel,支持公式、图表、函数和宏。用此软件创建的电子表格使用 SXC 扩展名保存。SXC 文件也用于OpenOffice.org Calc电子表格。Aspose.Cells for Python via .NET可以读取SXC文件,示例代码如下。

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)

打开FODS文件

FODS文件是以OpenDocument XML格式保存的电子表格,没有任何压缩。Aspose.Cells for Python via .NET可以读取FODS文件,示例代码如下。

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!")