C#合并Excel

來源:互聯網
上載者:User
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel=Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace ExcelOp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] files={"c:\\芙蓉區.xls","c:\\天心區.xls"};
            Mege m = new Mege(files, "c:\\aaa.xls", "C", 1);
            Console.WriteLine("Start Merge");
            m.DoMerge();
            Console.WriteLine("End Merge");
            Console.ReadLine();
        }
    }
    class Mege
    {
        Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
        //destination book
        Excel.Workbook bookDest = null;
        Excel.Worksheet sheetDest = null;
        //source book
        Excel.Workbook bookSource = null;
        Excel.Worksheet sheetSource = null;
        //source files path
        string[] sourceFiles = null;
        //destination file
        string destFile = string.Empty;
        //end column eg:A-C c is the end column
        string columnEnd = string.Empty;
        //the header rows'count
        int headerRowCount = 1;
        //which row the pointer pointed
        int currentRowCount = 0;

        public Mege(string[] sFiles,string dFile,string cEnd,int hCount)
        {
            //create a new excel file, sheet1 sheet2 and sheet3 work sheet will be created 
            bookDest = (Excel.WorkbookClass)app.Workbooks.Add(Missing.Value);
            //create a new work sheet
            sheetDest = bookDest.Worksheets[1] as Excel.Worksheet;
            //or we can create a new work sheet like :
            /*sheetDest = bookDest.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value) as Excel.Worksheet;
            sheetDest.Name = "Sheet4";*/
            sourceFiles = sFiles;

            destFile = dFile;

            columnEnd = cEnd;

            headerRowCount = hCount;
        }

        protected void OpenBook(string filename)
        {
            //open the source excel file
            bookSource = app.Workbooks._Open(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            //open the sheet in the source workbook
            sheetSource = bookSource.Worksheets[2] as Excel.Worksheet;
        }
        protected void CloseBook()
        {
            //close the excel file
            bookSource.Close(false, Missing.Value, Missing.Value);
        }
        protected void CopyHeader()
        {
            //get the range eg:from A1 to C5
            Excel.Range range = sheetSource.get_Range("A1",columnEnd+headerRowCount.ToString());
            //copy the sheet header from source excel file 
            range.Copy(sheetDest.get_Range("A1", Missing.Value));
            //move the record pointer
            currentRowCount += headerRowCount;
        }
        protected void CopyData()
        {
            //compute the Row Count Of the Sheet
            int sheetRowCount = sheetSource.UsedRange.Rows.Count;
            //get the Rows that has Record
            Excel.Range range = sheetSource.get_Range(String.Format("A{0}", headerRowCount + 1), columnEnd + sheetRowCount);
            //copy the record to destination excel sheet
            range.Copy(sheetDest.get_Range(String.Format("A{0}",currentRowCount+1),Missing.Value));
            //move the record pointer
            currentRowCount += sheetRowCount;
        }
        protected void Save()
        {
            //sace the destination excel file
            bookDest.Saved = true;
            bookDest.SaveCopyAs(destFile);
        }
        /// <summary>
        /// 退出進程
        /// </summary>
        protected void Quit()
        {
            //current application quit
            app.Quit();
        }

        public void DoMerge()
        {
            //add sheet header only once
            bool b = false;
            //Iteration the source files
            foreach (string strFile in sourceFiles)
            {
                //open the excel
                OpenBook(strFile);
                if (b == false)
                {
                    CopyHeader();
                    b = true;
                }
                //copy the data
                CopyData();
                //close the excel
                CloseBook();
            }
            Save();
            Quit();
        }

    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.