En boucle dans les classurs Excel, en copiant une gamme de cellules de chacune et en collant à un classur principal

HI: Je suis tellement content que vous soyez tous à l'écart parce que ce qui devrait être si facile semble ne pas être pour moi.

Context: J'essaie d'utiliser C # pour ouvrir un classur Excel (en utilisant OpenFileDialog pour le sélectionner – appelé «Gradebook»). Ensuite, je parcourue plusieurs classurs Excel (dans un dossier que je sélectionne avec FolderBrowserDialog – qui enregistre les noms de files dans une string []). Je souhaite ouvrir chaque classur un par un, extraire une gamme de données de chaque classur et le coller dans le «Gradebook». Jusqu'à présent, la capture des noms de files dans le tableau fonctionne et la possibilité de sélectionner un classur unique et de l'ouvrir.

La question: J'ai des problèmes avec la déclaration foreach. Comme je passe dans les classurs, la ligne: Excel.Workbook stdWorkbook = excelApp.Workbooks.Open(stdFile) a une erreur: "le nom stdFile n'existe pas dans le context".

En outre, je suis assez nouveau pour la programmation et je semble avoir des problèmes pour find tout ce qui explique l'interop avec Excel et C # dans des termes assez simples pour que je comprenne. J'apprécierais grandement toute direction pour une bonne source.

 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 ssortingng[] 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) { ssortingng[] fileNames = Directory.GetFiles(fbd.SelectedPath); System.Windows.Forms.MessageBox.Show("Files found: " + fileNames.Length.ToSsortingng(), "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(); ssortingng 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 (ssortingng 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; } } } 

 foreach (ssortingng stdFile in fileNames); 

Le point-virgule à la fin de cette ligne termine l'instruction immédiatement, de sorte que stdFile n'est plus disponible dans le code. Supprimer le point-virgule.

Il y a une introduction directe à Excel Interop ici chez dotnetperls .