国产Excel开发组件Spire.XLS教程:在VB.NET中将多个Excel 文件合并为一个

很多时候我们必须同时打开多个 Excel 文件时,合并相同类型或类别的 Excel 文件可以帮助我们避免麻烦,节省我们很多时间,本文将演示如何使用Spire.XLS for .NET库在 VB.NET 中将 Excel 文件合并为一个。

当我们必须同时打开多个 Excel 文件时,合并相同类型或类别的 Excel 文件可以帮助我们避免麻烦并节省我们很多时间,本文将演示如何使用

首先,您需要添加 Spire.XLS for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。 

PM> Install-Package Spire.XLS

以下是将多个 Excel 工作簿合并为一个的步骤:

  • 从 Excel 文件路径创建一个字符串数组。
  • 初始化一个Workbook对象以创建一个新的 Excel 工作簿,并使用Workbook.Worksheets.Clear()方法清除工作簿中的默认工作表。
  • 初始化另一个临时Workbook对象。
  • 循环遍历字符串数组,使用Workbook.LoadFromFile()方法将当前工作簿加载到临时 Workbook 对象中。
  • 循环浏览当前工作簿中的工作表,然后使用Workbook.Worksheets.AddCopy()方法将当前工作簿中的每个工作表复制到新工作簿中。
  • 使用Workbook.SaveToFile()方法将新工作簿保存到文件。  
using Spire.Xls;namespace MergeExcelFiles{    class Program    {        static void Main(string[] args)        {            //Create a string array from Excel file paths            string[] inputFiles = new string[] { "April.xlsx", "May.xlsx", "June.xlsx" };            //Initialize a new Workbook object            Workbook newWorkbook = new Workbook();            //Clear the default worksheets            newWorkbook.Worksheets.Clear();            //Initialize another temporary Workbook object            Workbook tempWorkbook = new Workbook();            //Loop through the string array            foreach (string file in inputFiles)            {                //Load the current workbook                tempWorkbook.LoadFromFile(file);                //Loop through the worksheets in the current workbook                foreach (Worksheet sheet in tempWorkbook.Worksheets)                {                    //Copy each worksheet from the current workbook to the new workbook                    newWorkbook.Worksheets.AddCopy(sheet, WorksheetCopyType.CopyAll);                }            }            //Save the new workbook to file            newWorkbook.SaveToFile("MergeWorkbooks.xlsx", ExcelVersion.Version2013);        }    }}

输入 Excel 工作簿:

VB.NET:将 Excel 文件合并为一个

合并的 Excel 工作簿:

VB.NET:将 Excel 文件合并为一个   

我们可以将相同或不同工作簿中的多个工作表合并为一个。以下步骤显示如何将同一工作簿中的两个 Excel 工作表合并为一个工作表:

  • 使用Workbook.LoadFromFile()方法初始化Workbook对象并加载 Excel 文件。
  • 使用Workbook.Worksheets[sheetIndex]属性获取需要合并的两个工作表,请注意,工作表索引是从零开始的。
  • 使用Worksheet.AllocatedRange属性获取第二个工作表的使用范围。
  • 使用Worksheet.Range[rowIndex, columnIndex]属性在第一个工作表中指定目标范围,请注意,行和列索引是从 1 开始的。
  • 使用CellRange.Copy(destRange)方法将第二个工作表的使用范围复制到第一个工作表中的目标范围。
  • 使用XlsWorksheet.Remove()方法删除第二个工作表。
  • 使用Workbook.SaveToFile()方法保存结果文件 
using Spire.Xls; namespace MergeExcelWorksheets{    class Program    {        static void Main(string[] args)        {            //Create a Workbook object            Workbook workbook = new Workbook();            //Load an Excel file            workbook.LoadFromFile("Sample.xlsx");            //Get the first worksheet            Worksheet sheet1 = workbook.Worksheets[0];            //Get the second worksheet            Worksheet sheet2 = workbook.Worksheets[1];            //Get the used range in the second worksheet            CellRange sourceRange = sheet2.AllocatedRange;            //Specify the destination range in the first worksheet            CellRange destRange = sheet1.Range[sheet1.LastRow + 1, 1];             //Copy the used range of the second worksheet to the destination range in the first worksheet            sourceRange.Copy(destRange);            //Remove the second worksheet            sheet2.Remove();            //Save the result file            workbook.SaveToFile("MergeWorksheets.xlsx", ExcelVersion.Version2013);        }    }}

输入 Excel 工作表:

VB.NET:将 Excel 文件合并为一个

合并的 Excel 工作表:

VB.NET:将 Excel 文件合并为一个 更多Spire产品使用

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年1月18日
下一篇 2022年1月18日

相关推荐

发表回复

登录后才能评论