【问题标题】:Looping through Excel workbooks, copying a range of cells from each and pasting to a master workbook循环遍历 Excel 工作簿,从每个工作簿复制一系列单元格并粘贴到主工作簿
【发布时间】:2013-08-17 22:40:07
【问题描述】:

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

背景:我正在尝试使用 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;
        }
    }
}

【问题讨论】:

  • 谢谢安迪 - 让我更近了一步。我现在在底部添加了 3 行附加代码(见下文),现在得到一条类似的消息“当前上下文中不存在名称成绩簿”。它在 foreach 语句上方和我添加的类的顶部定义:“Excel.Workbook GradeBook;”范围?原因。
  • Excel.Worksheet xlGradesheet = Gradebook.Sheets[1]; Excel.Range xlRangeTemp = xlGradesheet.Range["A6:E6"]; xlRangeTemp = xlRange.Value;

标签: c# excel loops interop


【解决方案1】:
foreach (string stdFile in fileNames);

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

这里有一个关于 Excel 互操作的简单介绍 at dotnetperls

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    相关资源
    最近更新 更多