0

HI:我很高兴你们都在那里,因为应该如此容易的事情似乎不适合我。

背景:我正在尝试使用 C# 打开 Excel 工作簿(使用 OpenFileDialog 选择它 - 称为“成绩簿”)。然后我循环浏览多个 Excel 工作簿(在我使用 FolderBrowserDialog 选择的文件夹中 - 它将文件名保存在字符串 [] 中)。我希望一个一个地打开每个工作簿,从每个工作簿中提取一系列数据并将其粘贴到“成绩簿”中。到目前为止,捕获数组中的文件名是有效的,选择单个工作簿并打开它的能力也是如此。

问题:我在使用 foreach 语句时遇到问题。当我遍历工作簿时,行:Excel.Workbook stdWorkbook = excelApp.Workbooks.Open(stdFile)有一个错误:“名称 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;
        }
    }
}
4

1 回答 1

0
foreach (string stdFile in fileNames);

此行末尾的分号立即终止语句,因此stdFile在后面的代码中不可用。删除分号。

在 dotnetperls有一个关于 Excel 互操作的简单介绍。

于 2013-08-17T23:59:39.677 回答