C#合并Excel

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[1as 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[2as 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();
        }

    }
}

posted on 2009-07-08 11:12  lwl0606  阅读(392)  评论(0编辑  收藏  举报

导航

我要啦免费统计