我需要一些帮助才能将多个工作簿集成到一个主工作簿中。我想使用以下代码。如果有人能帮我解决以下问题,我将非常感激:
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using System.IO;
namespace IntegrationMacro
{
class Program
{
static void Main(string[] args)
{
Excel.Application app = new Excel.Application();
app.Visible = true;
app.Workbooks.Add("");
app.Workbooks.Add(@"C:\Users\ssehgal\Documents\Excel-Files-For-Macro\WorkBook1.xls");
app.Workbooks.Add(@"C:\Users\ssehgal\Documents\Excel-Files-For-Macro\WorkBook2.xls");
for (int i = 2; i <= app.Workbooks.Count; i++)
{
for (int j = 1; j <= app.Workbooks[i].Worksheets.Count; j++)
{
Excel.Worksheet ws = (Excel.Worksheet)app.Workbooks[i].Worksheets[j];
ws.Copy(app.Workbooks[1].Worksheets[1]);
}
}
}
}
}
答案 0 :(得分:0)
var app = new Application() { Visible = true };
var master = app.Workbooks.Add();
// repeat this for each workbook you need to combine...
app.Workbooks.Add(__PATH_TO_THE_WORKBOOK__)
.Worksheets.Cast<Worksheet>()
.ToList() // Move() will modify the source of enumeration, so we need to make a temp copy
.ForEach(x => x.Move(master.Sheets[1]));