HI:我很高兴你们都在那里,因为应该这么容易看起来不适合我。
背景:我正在尝试使用C#打开Excel工作簿(使用OpenFileDialog来选择它 - 称为" Gradebook")。然后我遍历多个Excel工作簿(在我用FolderBrowserDialog选择的文件夹中 - 将文件名保存在字符串[]中)。我希望逐个打开每个工作簿,从每个工作簿中提取一系列数据并将其粘贴到&34; Gradebook"中。到目前为止,捕获数组中的文件名是可行的,因此能够选择单个工作簿并打开它。
问题:我对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;
}
}
}
答案 0 :(得分:0)
foreach (string stdFile in fileNames);
此行末尾的分号会立即终止语句,因此{I}中的stdFile
不可用。删除分号。
这里有一个直接的Excel Interop简介at dotnetperls。