表格和范围
介绍
有时您在Microsoft Excel中创建一个表格,但不想继续使用它带来的表格功能。相反,您想要看起来像表格的东西。为了在不丢失格式的情况下保留表格中的数据,可以将表格转换为普通数据范围。 Aspose.Cells确实支持Microsoft Excel的表格和列表对象的此功能。
使用Microsoft Excel
使用转换为范围功能快速将表格转换为常规数据范围,而不丢失格式。在Microsoft Excel 2007/2010中:
- 单击表中的任意位置,确保活动单元格位于表列中。
- 在设计选项卡的工具组中,单击转换为范围。
使用Aspose.Cells
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open an existing file that contains a table/list object in it | |
Workbook wb = new Workbook(dataDir + "book1.xlsx"); | |
// Convert the first table/list object (from the first worksheet) to normal range | |
wb.Worksheets[0].ListObjects[0].ConvertToRange(); | |
// Save the file | |
wb.Save(dataDir + "output.xlsx"); |
使用选项将表格转换为范围
Aspose.Cells在将表格转换为范围时提供附加选项,通过TableToRangeOptions类提供TableToRangeOptions类,并提供LastRow属性,允许您设置表格行的最后索引。表格格式将保留到指定的行索引,其余格式将被移除。
以下给出的示例代码演示了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. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open an existing file that contains a table/list object in it | |
Workbook workbook = new Workbook(dataDir + "book1.xlsx"); | |
TableToRangeOptions options = new TableToRangeOptions(); | |
options.LastRow = 5; | |
// Convert the first table/list object (from the first worksheet) to normal range | |
workbook.Worksheets[0].ListObjects[0].ConvertToRange(options); | |
// Save the file | |
workbook.Save(dataDir + "output.xlsx"); |