HI:我很高興你們都在那裏,因爲應該如此容易的東西看起來不適合我。我試圖用C#打開Excel工作簿(使用OpenFileDialog選擇它 - 稱爲「成績簿」)。然後我循環瀏覽多個Excel工作簿(在FolderBrowserDialog中選擇的文件夾中 - 將文件名保存爲字符串[])。我希望逐一打開每個工作簿,從每個工作簿中提取一系列數據並將其粘貼到「成績簿」中。到目前爲止,捕獲數組中的文件名是可行的,選擇單個工作簿並將其打開的能力也是如此。通過Excel工作簿循環,複製各個單元格的範圍並粘貼到主工作簿中
問題:我對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;
}
}
}
謝謝安迪 - 讓我更靠近一步。我現在在底部添加了3行附加代碼(見下面),現在得到類似的消息,「在當前上下文中不存在名稱成績簿」。它在foreach語句上面定義,並在類的頂部添加:「Excel.Workbook GradeBook;」適用範圍?原因。 – user2016566
Excel.Worksheet xlGradesheet = Gradebook.Sheets [1]; Excel.Range xlRangeTemp = xlGradesheet.Range [「A6:E6」]; xlRangeTemp = xlRange.Value; – user2016566