Excel处理开发工具Aspose.Cells功能演示:使用C#复制或移动Excel工作表

  • 在Excel工作簿中复制工作表
  • 将工作表从一个工作簿复制到另一个
  • 将工作表从一个位置移动到另一个位置

为了在Excel文件中复制工作表,我们将利用

在C#中的Excel工作簿中复制工作表

以下是使用Aspose.Cells for .NET在Excel工作簿中复制工作表的步骤。

  • 使用Workbook类加载Excel文件。
  • 从Workbook.Worksheets属性中检索WorksheetCollection对象中的工作表。
  • 使用WorksheetCollection.AddCopy(string)方法通过提供其名称来复制工作表。
  • 使用Workbook.Save(string)方法保存更新的工作簿。

下面的代码示例演示如何使用C#在Excel工作簿中复制工作表。

// Open an existing Excel fileWorkbook wb = new Workbook("workbook.xlsx");// Create a WorksheetCollection object with reference to the sheets of the WorkbookWorksheetCollection sheets = wb.Worksheets;// Copy data to a new sheet from an existing sheet within the Workbooksheets.AddCopy("Sheet1");// Save the Excel filewb.Save("CopyWithinWorkbook.xlsx");

将工作表从一个工作簿复制到另一个

现在,让我们看一下如何将工作表从一个Excel工作簿复制到另一个工作表。以下是执行此操作的步骤。

  • 创建Workbook类的对象并加载源Excel文件。
  • 创建Workbook类的另一个对象并加载目标Excel文件。
  • 使用destinationWorkbook.Worksheets [int] .Copy(sourceWorkbook.Worksheets [int])方法将工作表从源复制到目标工作簿。
  • 使用Workbook.Save(string)方法保存目标工作簿。

下面的代码示例演示如何使用C#将工作表从一个工作簿复制到另一个工作簿。

// Open source Excel fileWorkbook sourceWorkbook = new Workbook("source.xlsx");// Open destination Excel fileWorkbook destinationWorkbook = new Workbook("destination.xlsx");// Copy the first sheet of the source workbook into destination workbookdestinationWorkbook.Worksheets[0].Copy(sourceWorkbook.Worksheets[0]);// Save the Excel filedestinationWorkbook.Save("copy-worksheets.xlsx");

在C#中将工作表从一个位置移动到另一个位置

在某些情况下,可能需要重新排列工作簿中工作表的位置。可以通过指定索引将工作表移动到所需位置来完成此操作。以下是将工作表从一个位置移动到另一位置的步骤。

  • 使用Workbook类加载Excel文件。
  • 使用Workbook.Worksheets属性在WorksheetCollection对象中检索工作表。
  • 从工作表集合到工作表对象中访问所需的工作表。
  • 使用Worksheet.MoveTo(int)方法将工作表移动到指定的索引。
  • 使用Workbook.Save(string)方法保存更新的工作簿。

下面的代码示例演示如何使用C#将Excel工作表从一个位置移动到另一位置。

// Open an existing excel fileWorkbook wb = new Workbook("workbook.xlsx");// Create a WorksheetCollection object with reference to the sheets of the WorkbookWorksheetCollection sheets = wb.Worksheets;// Get the first worksheet.Worksheet worksheet = sheets[0];// Move the first sheet to the third position in the workbookworksheet.MoveTo(2);// Save the Excel filewb.Save("move-worksheet.xlsx");

如果你想试用Aspose的全部完整功能,可 联系在线客服获取30天临时授权体验。


还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月12日
下一篇 2021年1月12日

相关推荐

发表回复

登录后才能评论