您的位置:首页 > 编程语言 > C#

C#合并Excel

2009-07-08 11:12 309 查看
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();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: