表格和范围

介绍

有时您在Microsoft Excel中创建一个表格,但不想继续使用它带来的表格功能。相反,您想要看起来像表格的东西。为了在不丢失格式的情况下保留表格中的数据,可以将表格转换为普通数据范围。 Aspose.Cells for Python via .NET支持Microsoft Excel中关于表格和列表对象的相关功能。

使用Microsoft Excel

使用转换为范围功能快速将表格转换为常规数据范围,而不丢失格式。在Microsoft Excel 2007/2010中:

  1. 单击表中的任意位置,确保活动单元格位于表列中。
  2. 设计选项卡的工具组中,单击转换为范围

使用Aspose.Cells for Python via .NET

from aspose.cells import 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(".")
# Open an existing file that contains a table/list object in it
wb = Workbook(dataDir + "book1.xlsx")
# Convert the first table/list object (from the first worksheet) to normal range
wb.worksheets[0].list_objects[0].convert_to_range()
# Save the file
wb.save(dataDir + "output.xlsx")

使用选项将表格转换为范围

Aspose.Cells for Python via .NET通过TableToRangeOptions类提供了将表格转换为范围的其他选项。TableToRangeOptions类提供了last_row属性,可以设置表格行的最后索引。到达指定行索引后,表格格式将被保留,其他格式将被移除。

以下给出的示例代码演示了TableToRangeOptions类的使用。

from aspose.cells import Workbook
from aspose.cells.tables import TableToRangeOptions
# 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(".")
# Open an existing file that contains a table/list object in it
workbook = Workbook(dataDir + "book1.xlsx")
options = TableToRangeOptions()
options.last_row = 5
# Convert the first table/list object (from the first worksheet) to normal range
workbook.worksheets[0].list_objects[0].convert_to_range(options)
# Save the file
workbook.save(dataDir + "output.xlsx")