小弟之前写了一篇多个word合并的功能
最近想到word可以合并,excel应该也可以合并
小弟之前写了一篇多个word合并的功能
最近想到word可以合并,excel应该也可以合并
首先准备好两个测试的excel档(1.xls,2.xls)放在c槽里,执行完程序就产生3.xls的合并档了
WinForm(c#)
ExcelCombine.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;//这个要先加入Microsoft.Office.Interop.Excel参考
using System.Reflection;//这个记得参考
namespace WindowsApplication1
{
public partial class ExcelCombine : Form
{
public ExcelCombine()
{
InitializeComponent();
}
private void ExcelCombine_Load(object sender, EventArgs e)
{
object missing = Missing.Value;
string oFirstXls = @"c:1.xls";//excel档1
string oSecondXls = @"c:2.xls";//excel档2
string oOutputXls = @"c:3.xls";//合并档,excel档3
Excel.Application excelApp = new Excel.ApplicationClass();
Excel.Workbook wbook1 = excelApp.Workbooks.Open(oFirstXls, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);//载入excel档1
Excel.Workbook wbook2 = excelApp.Workbooks.Open(oSecondXls, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);//载入excel档2
wbook1.Worksheets.Copy(missing, wbook2.Sheets["Sheet1"]);//在excel档2的Sheet1之后插入所有excel档1的Sheet
//wbook1.Worksheets.Copy(wbook2.Sheets["Sheet1"], missing);//在excel档2的Sheet1之前插入所有excel档1的Sheet
wbook2.SaveAs(oOutputXls, missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);//另存excel档3
wbook1.Close(missing, missing, missing);
wbook2.Close(missing, missing, missing);
excelApp.Quit();
}
}
}
执行结果:
excel档1
excel档2
合并档,excel档3
参考网址:
http://msdn.microsoft.com/zh-tw/library/microsoft.office.tools.excel.worksheet.copy(VS.80).aspx
http://topic.csdn.net/t/20061110/17/5149046.html
原文:大专栏 [C#]WinForm利用Microsoft.Office.Interop.Excel的Worksheets.Copy来合并多个Excel档
原文地址:https://www.cnblogs.com/chinatrump/p/11516545.html