将Excel表转换为数据范围
有时您在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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ConvertTableToRange.class) + "tables/"; | |
// 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.getWorksheets().get(0).getListObjects().get(0).convertToRange(); | |
// Save the file | |
wb.save(dataDir + "ConvertTableToRange_out.xlsx"); |
使用选项将表格转换为范围
Aspose.Cells在通过TableToRangeOptions类转换表格为数据范围时提供了额外选项。TableToRangeOptions类提供了LastRow属性,允许您设置表格行的最后索引。表格的格式将保留到指定的行索引,其余的格式将被移除。
以下给出的示例代码演示了TableToRangeOptions类的使用。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ConvertTableToRangeWithOptions.class) + "Tables/"; | |
// Open an existing file that contains a table/list object in it | |
Workbook workbook = new Workbook(dataDir + "book1.xlsx"); | |
TableToRangeOptions options = new TableToRangeOptions(); | |
options.setLastRow(5); | |
// Convert the first table/list object (from the first worksheet) to normal range | |
workbook.getWorksheets().get(0).getListObjects().get(0).convertToRange(options); | |
// Save the file | |
workbook.save(dataDir + "ConvertTableToRangeWithOptions_out.xlsx"); |