C# 循环浏览Excel工作簿,从每个工作簿复制一系列单元格并粘贴到主工作簿

C# 循环浏览Excel工作簿,从每个工作簿复制一系列单元格并粘贴到主工作簿,c#,excel,loops,interop,C#,Excel,Loops,Interop,嗨:我很高兴你们都在那里,因为本该如此简单的事情似乎不适合我 背景:我正试图用C打开一个Excel工作簿,使用OpenFileDialog来选择它-称为成绩册。然后,我使用FolderBrowserDialog在我选择的文件夹中循环浏览多个Excel工作簿,该对话框将文件名保存在字符串[]中。我希望逐个打开每个工作簿,从每个工作簿中提取一系列数据,并将其粘贴到成绩册中。到目前为止,捕获数组中的文件名是可行的,选择单个工作簿并打开它的能力也是可行的 问题是:我对foreach语句有问题。当我在工作

嗨:我很高兴你们都在那里,因为本该如此简单的事情似乎不适合我

背景:我正试图用C打开一个Excel工作簿,使用OpenFileDialog来选择它-称为成绩册。然后,我使用FolderBrowserDialog在我选择的文件夹中循环浏览多个Excel工作簿,该对话框将文件名保存在字符串[]中。我希望逐个打开每个工作簿,从每个工作簿中提取一系列数据,并将其粘贴到成绩册中。到目前为止,捕获数组中的文件名是可行的,选择单个工作簿并打开它的能力也是可行的

问题是:我对foreach语句有问题。当我在工作簿中循环时,行:Excel.Workbook stdWorkbook=excelApp.workbooks.OpenstdFile有一个错误:上下文中不存在stdFile名称

另外,我对编程相当陌生,似乎很难找到任何能够用简单易懂的术语解释Excel和C互操作的东西。如果能找到好的消息来源,我将不胜感激

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;  //Use Project add reference to add the       Microsoft Excel Object library, then add this statement
using System.Reflection;

namespace ExcelLoadStdDataToGradingSheet
{
    public partial class Form1 : Form
    {
        public string[] fileNames;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // select the folder with the student assignment 
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "Browse to find Folder with student assignments to open";
            fbd.SelectedPath = "C:\\Users\\Robert\\Google Drive\\Programming";
            DialogResult result = fbd.ShowDialog();
            if (result == DialogResult.OK)
            {
                string[] fileNames = Directory.GetFiles(fbd.SelectedPath);
                System.Windows.Forms.MessageBox.Show("Files found: " +   fileNames.Length.ToString(), "Message");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Select the Excel file used for grading and open it
            OpenFileDialog dialog = new OpenFileDialog(); // Open dialog box to select   desired Excel file.  Next 3 line adjust that browser dialog
            dialog.Title = "Browse to find Grade Sheet to open";
            dialog.InitialDirectory = @"c:\\Users\\Robert\\Google Drive\\Programming";
            dialog.ShowDialog();
            string GradeExcel = dialog.FileName;
            Excel.Application excelApp = new Excel.Application();   //This creates the Excel application instance "excelApp"
            excelApp.Visible = true;
            Excel.Workbook GradeBook = excelApp.Workbooks.Add(GradeExcel);

            // Loop to open every student file, extract filename, name, Red ID, and creation date
            foreach (string stdFile in fileNames);
            {
                // Open student Workbook and copy data from "Hidden" Worksheet
                //Excel.Application newApp = new Excel.Application();  //This creates the Excel application instance "newApp"
                Excel.Workbook stdWorkbook = excelApp.Workbooks.Open(stdFile);  //Open student Workbooks
                Excel.Worksheet xlWorksheet = stdWorkbook.Sheets["Hidden"];
                Excel.Range xlRange = xlWorksheet.Range["A2:E2"];



                // Paste student information to the Grade Sheet
                //  Workbooks(GradeBook).Sheets("GradingSheet").Range("SetActiveCell").Select.Paste;  //' start  pasting in "A5";
                // SetActiveCell = ActiveCell.Offset(1, 0).Select;
                // workbooks("StdWorkbook").close savechanges:=false;
            }
            // workbooks("Gradingbook").close savechanges:=true;
        }
    }
}
此行末尾的分号立即终止语句,因此stdFile在下面的代码中不可用。去掉分号


这里直接介绍了Excel Interop。

谢谢Andy-让我更进一步。我现在添加了3行额外的代码,见下图底部,现在得到了一个类似的消息,即当前上下文中不存在该名称成绩册。它定义在foreach语句上方,位于我添加的类的顶部:Excel.Workbook成绩册;范围?reasons.Excel.Worksheet xlGradesheet=成绩册.Sheets[1];Excel.Range xlRangeTemp=xlGradesheet.Range[A6:E6];xlRangeTemp=xlRange.Value;
foreach (string stdFile in fileNames);