C # Copy (formatted) Excel worksheet

Source: Internet
Author: User

This article describes how to copy worksheet data in C #. Replication of worksheets takes two things into account

    1. Copy between different worksheets in the same workbook
    2. Copying between worksheets in different workbooks
      ( the Copy here contains all the contents of the original data table, such as copying data, borders, shading, formulas, etc. )
      In addition, for replication of Excel tabular data, you can copy only the data for a row or column in a worksheet, in addition to copying the entire worksheet. The following is a detailed description of the code operation process, methods for reference.
      Using tools: Free Spire.xls for. NET (Community Edition)
      Instructions for use: after installation, refer directly to Spire.XLS.dll in the project program (DLL files can be obtained in the Bin folder under the installation path)
      Test Document:
One, copy in the same workbook
using Spire.Xls;namespace Copy2{    class Program    {        static void Main(string[] args)        {            //创建一个Workbook类对象,并加载Excel文件            Workbook workbook = new Workbook();            workbook.LoadFromFile("Sample.xlsx");            //获取第一个工作表sheet1            Worksheet sheet1 = workbook.Worksheets[0];            //添加一个新的工作表sheet2,并命名该工作表            Worksheet sheet2 = workbook.Worksheets.Add("Copy");            //将第一个工作表复制到新添加的工作表            sheet2.CopyFrom(sheet1);            //保存并打开文件            workbook.SaveToFile("工作表复制.xlsx",FileFormat.Version2010);            System.Diagnostics.Process.Start("工作表复制.xlsx");        }    }}

Test results:

Second, cross-workbook replication
using Spire.Xls;namespace CopyWorksheet_XLS{    class Program    {        static void Main(string[] args)        {            //创建工作簿1,并加载第一个Excel文件            Workbook workbook1 = new Workbook();            workbook1.LoadFromFile("Sample.xlsx");            //获取第一个工作表            Worksheet sheet1 = workbook1.Worksheets[0];            //创建工作簿2,并加载第二个Excel文件            Workbook workbook2 = new Workbook();            workbook2.LoadFromFile(@"C:\Users\Administrator\Desktop\sample2.xlsx");            //调用方法AddCopy()将第一个Excel文件的sheet1复制到第二个Excel文件,并命名复制后的文件            Worksheet sheet2 = workbook2.Worksheets.AddCopy(sheet1);            sheet2.Name = "Copy";            //保存并打开文件            workbook2.SaveToFile("Report.xlsx");            System.Diagnostics.Process.Start("Report.xlsx");        }    }}

Test results:

Third, copy rows or columns
using Spire.Xls;namespace InsertRow_s__XLS{    class Program    {        static void Main(string[] args)        {            //初始化Workbook类实例,并加载Excel 测试文档            Workbook workbook = new Workbook();            workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.xlsx");            //获取第一个工作表            Worksheet worksheet = workbook.Worksheets[0];            //新插入一行作为第3行            worksheet.InsertRow(3);            //新插入一列作为第8列           // worksheet.InsertColumn(8);            //将第一行数据复制到第三行            worksheet.Copy(worksheet.Range["A1:H1"], worksheet.Range["A3:H3"], true);            //将第二列数据复制到第八列           // worksheet.Copy(worksheet.Range["B1:B27"],worksheet.Range["H1:H27"],true);                      //保存并打开文档            workbook.SaveToFile("复制行.xlsx", ExcelVersion.Version2010);            System.Diagnostics.Process.Start("复制行.xlsx");        }    }}

Test results:

1. Copying rows

2. Copying columns

The above content for the "C # copy Excel Worksheet" of the entire content, welcome reprint (Reproduced please specify the source)

C # Copy (formatted) Excel worksheet

Related Article

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.