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();
}
}
}
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();
}
}
}
相关文章推荐
- C#实现Excel跨文件多SHEET合并计算(原创)
- 学习(二)C#合并同一文件夹下相同格式的Excel数据
- C#导出Excel——多表头、表头跨行跨列合并
- Excel 2007单元格及内容的合并、拆分_C#教程
- DataSet导出到Excel,并生成文件(C#实现,可合并行和列)
- C#导出涉及行列合并的复杂的Excel数据
- C# 如何合并Excel工作表
- Excel 2007单元格及内容的合并、拆分_C#教程
- C# 两张Excel 合并到新建的一张Excel
- C#合并多个结构一样的Excel
- C# 实现Excel转Word、Word转PDF、合并PDF
- c# 导入Excel 存到DataTable并进行行转列操作及合并DataTable相同行的值
- C#合并多个结构一样的Excel
- [网上整理]C#合并Excel
- C#处理多个Excel合并,复制Sheet
- C#处理多个Excel合并,复制Sh…
- C#合并多个结构一样的Excel
- C#用winform实现excel数据合并
- C#操作Excel文件暨C#实现在Excel中将连续多列相同数据项合并
- C# 如何合并Excel工作表