| 導購 | 订阅 | 在线投稿
分享
 
 
 

C#合並多個結構一樣的Excel

2008-08-20 08:08:40  編輯來源:互聯網  简体版  手機版  移動版  評論  字體: ||
 

合並代碼如下:

using System;

using System.Collections.Generic;

using System.Text;

using System.Reflection;

using Excel = Microsoft.Office.Interop.Excel;

namespace ConsoleApplication20

{

//添加引用-COM-MicroSoft Excel 11.0 Object Libery

class Program

{

static void Main(string[] args)

{

//M爲表格寬度標志(Excel中的第M列爲最後一列),3爲表頭高度

MergeExcel.DoMerge(new string[]

{

@"E:\excel\類型A\公司A.xls",

@"E:\excel\類型A\公司B.xls"

},

@"E:\excel\類型A\合並測試.xls", "M", 3);

MergeExcel.DoMerge(new string[]

{

@"E:\excel\類型B\統計表A.xls",

@"E:\excel\類型B\統計表B.xls"

},

@"E:\excel\類型B\合並測試.xls", "I", 4);

}

}

public class MergeExcel

{

Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();

//保存目標的對象

Excel.Workbook bookDest = null;

Excel.Worksheet sheetDest = null;

//讀取數據的對象

Excel.Workbook bookSource = null;

Excel.Worksheet sheetSource = null;

string[] _sourceFiles = null;

string _destFile = string.Empty;

string _columnEnd = string.Empty;

int _headerRowCount = 1;

int _currentRowCount = 0;

public MergeExcel(string[] sourceFiles,string destFile,string columnEnd,int headerRowCount)

{

bookDest = (Excel.WorkbookClass)app.Workbooks.Add(Missing.Value);

sheetDest = bookDest.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value) as Excel.Worksheet;

sheetDest.Name = "Data";

_sourceFiles = sourceFiles;

_destFile = destFile;

_columnEnd = columnEnd;

_headerRowCount = headerRowCount;

}

/// <summary>

/// 打開工作表

/// </summary>

/// <param name="fileName"></param>

void OpenBook(string fileName)

{

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);

sheetSource = bookSource.Worksheets[1] as Excel.Worksheet;

}

/// <summary>

/// 關閉工作表

/// </summary>

void CloseBook()

{

bookSource.Close(false, Missing.Value, Missing.Value);

}

/// <summary>

/// 複制表頭

/// </summary>

void CopyHeader()

{

Excel.Range range = sheetSource.get_Range("A1", _columnEnd + _headerRowCount.ToString());

range.Copy(sheetDest.get_Range("A1",Missing.Value));

_currentRowCount += _headerRowCount;

}

/// <summary>

/// 複制數據

/// </summary>

void CopyData()

{

int sheetRowCount = sheetSource.UsedRange.Rows.Count;

Excel.Range range = sheetSource.get_Range(string.Format("A{0}", _headerRowCount + 1), _columnEnd + sheetRowCount.ToString());

range.Copy(sheetDest.get_Range(string.Format("A{0}", _currentRowCount + 1), Missing.Value));

_currentRowCount += range.Rows.Count;

}

/// <summary>

/// 保存結果

/// </summary>

void Save()

{

bookDest.Saved = true;

bookDest.SaveCopyAs(_destFile);

}

/// <summary>

/// 退出進程

/// </summary>

void Quit()

{

app.Quit();

}

/// <summary>

/// 合並

/// </summary>

void DoMerge()

{

bool b = false;

foreach (string strFile in _sourceFiles)

{

OpenBook(strFile);

if (b == false)

{

CopyHeader();

b = true;

}

CopyData();

CloseBook();

}

Save();

Quit();

}

/// <summary>

/// 合並表格

/// </summary>

/// <param name="sourceFiles">源文件</param>

/// <param name="destFile">目標文件</param>

/// <param name="columnEnd">最後一列標志</param>

/// <param name="headerRowCount">表頭行數</param>

public static void DoMerge(string[] sourceFiles, string destFile, string columnEnd, int headerRowCount)

{

new MergeExcel(sourceFiles, destFile, columnEnd, headerRowCount).DoMerge();

}

}

}

 
合並代碼如下: using System; using System.Collections.Generic; using System.Text; using System.Reflection; using Excel = Microsoft.Office.Interop.Excel; namespace ConsoleApplication20 { //添加引用-COM-MicroSoft Excel 11.0 Object Libery class Program { static void Main(string[] args) { //M爲表格寬度標志(Excel中的第M列爲最後一列),3爲表頭高度 MergeExcel.DoMerge(new string[] { @"E:\excel\類型A\公司A.xls", @"E:\excel\類型A\公司B.xls" }, @"E:\excel\類型A\合並測試.xls", "M", 3); MergeExcel.DoMerge(new string[] { @"E:\excel\類型B\統計表A.xls", @"E:\excel\類型B\統計表B.xls" }, @"E:\excel\類型B\合並測試.xls", "I", 4); } } public class MergeExcel { Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass(); //保存目標的對象 Excel.Workbook bookDest = null; Excel.Worksheet sheetDest = null; //讀取數據的對象 Excel.Workbook bookSource = null; Excel.Worksheet sheetSource = null; string[] _sourceFiles = null; string _destFile = string.Empty; string _columnEnd = string.Empty; int _headerRowCount = 1; int _currentRowCount = 0; public MergeExcel(string[] sourceFiles,string destFile,string columnEnd,int headerRowCount) { bookDest = (Excel.WorkbookClass)app.Workbooks.Add(Missing.Value); sheetDest = bookDest.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value) as Excel.Worksheet; sheetDest.Name = "Data"; _sourceFiles = sourceFiles; _destFile = destFile; _columnEnd = columnEnd; _headerRowCount = headerRowCount; } /// <summary> /// 打開工作表 /// </summary> /// <param name="fileName"></param> void OpenBook(string fileName) { 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); sheetSource = bookSource.Worksheets[1] as Excel.Worksheet; } /// <summary> /// 關閉工作表 /// </summary> void CloseBook() { bookSource.Close(false, Missing.Value, Missing.Value); } /// <summary> /// 複制表頭 /// </summary> void CopyHeader() { Excel.Range range = sheetSource.get_Range("A1", _columnEnd + _headerRowCount.ToString()); range.Copy(sheetDest.get_Range("A1",Missing.Value)); _currentRowCount += _headerRowCount; } /// <summary> /// 複制數據 /// </summary> void CopyData() { int sheetRowCount = sheetSource.UsedRange.Rows.Count; Excel.Range range = sheetSource.get_Range(string.Format("A{0}", _headerRowCount + 1), _columnEnd + sheetRowCount.ToString()); range.Copy(sheetDest.get_Range(string.Format("A{0}", _currentRowCount + 1), Missing.Value)); _currentRowCount += range.Rows.Count; } /// <summary> /// 保存結果 /// </summary> void Save() { bookDest.Saved = true; bookDest.SaveCopyAs(_destFile); } /// <summary> /// 退出進程 /// </summary> void Quit() { app.Quit(); } /// <summary> /// 合並 /// </summary> void DoMerge() { bool b = false; foreach (string strFile in _sourceFiles) { OpenBook(strFile); if (b == false) { CopyHeader(); b = true; } CopyData(); CloseBook(); } Save(); Quit(); } /// <summary> /// 合並表格 /// </summary> /// <param name="sourceFiles">源文件</param> /// <param name="destFile">目標文件</param> /// <param name="columnEnd">最後一列標志</param> /// <param name="headerRowCount">表頭行數</param> public static void DoMerge(string[] sourceFiles, string destFile, string columnEnd, int headerRowCount) { new MergeExcel(sourceFiles, destFile, columnEnd, headerRowCount).DoMerge(); } } }
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
王朝網路微信公眾號
微信掃碼關註本站公眾號 wangchaonetcn
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有