How C # merges an Excel worksheet

Source: Internet
Author: User

Merging documents can effectively simplify the management of complex documents. In the work, when we encounter the need to merge multiple Excel worksheets into a single worksheet, how to merge, this article will be further described.
using ToolsFree Spire.xls for. NET, Visual Studio
PS:Before you edit the code, you need to add a reference Spire.XLS.dll to the project program and add it to the namespace
Merge Excel tables in two, one is to merge worksheets from multiple worksheets to one worksheet, and the other is to merge multiple worksheets in one workbook into a single worksheet

One, merging from multiple workbooks
Using Spire.xls;namespace mergeworksheet_xls{class Program {static void Main (string[] args) {            Create a Workbook class object Workbook Newbook = new Workbook (); Newbook.            Version = excelversion.version2013; Delete a worksheet in a document (the newly created document contains 3 sheets by default) Newbook.            Worksheets.clear ();            Create a temporary Workbook to load the Excel document that needs to be merged Workbook tempbook = new Workbook (); Load the Excel document you want to merge into the array string[] EXCELFiles = new string[] {@ "C:\Users\Administrator\Desktop\test.xlsx" @ "C:\Us            Ers\administrator\desktop\sample.xlsx "}; Traverse array for (int i = 0; i < excelfiles.length; i++) {//Load Excel document TE Mpbook.                LoadFromFile (Excelfiles[i]); Call the AddCopy method to add all the worksheets in the document to the new workbook foreach (Worksheet sheet in Tempbook. Worksheets) {Newbook.                Worksheets.addcopy (sheet);    }}//Save document        Newbook.                    SaveToFile ("Mergefiles.xlsx", excelversion.version2013); }    }}

Before merging:

After merging:

Ii. merging from the same workbook
using Spire.Xls;namespace MergeWorksheetsToOne_XLS{    class Program    {        static void Main(string[] args)        {            //实例化一个Workbook类,加载Excel文档            Workbook workbook = new Workbook();            workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx");            //获取第1、2张工作表            Worksheet sheet1 = workbook.Worksheets[0];            Worksheet sheet2 = workbook.Worksheets[1];            //复制第2张工作表内容到第1张工作表的指定区域中            sheet2.AllocatedRange.Copy(sheet1.Range[sheet1.LastRow +3, 1]);            //删除第2张工作表           sheet2.Remove();            //重命名的工作表1           sheet1.Name = "外贸单证";            //保存并运行文档           workbook.SaveToFile("MergeSheets.xlsx", ExcelVersion.Version2013);           System.Diagnostics.Process.Start("MergeSheets.xlsx");        }    }}

Before merging:

After merging:

The above merge method for this time about the merged Excel document the entire content, if likes the Welcome reprint (Reprint please specify the source)

How C # merges an Excel worksheet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.