简体   繁体   English

循环浏览Excel工作簿,从每个工作簿复制一定范围的单元格并粘贴到主工作簿

[英]Looping through Excel workbooks, copying a range of cells from each and pasting to a master workbook

HI: I am so glad you all are out there because what should be so easy appears not to be for me. 嗨:我很高兴你们都在那里,因为应该如此简单的事情似乎并不适合我。

Background: I am trying to use C# to open an Excel Workbook (using OpenFileDialog to select it - called "Gradebook"). 背景:我正在尝试使用C#打开Excel工作簿(使用OpenFileDialog选择它-称为“ Gradebook”)。 Then I loop through multiple Excel workbooks (in a folder I select with FolderBrowserDialog - which saves the filenames in a string[]). 然后,我遍历多个Excel工作簿(在我用FolderBrowserDialog选择的文件夹中-将文件名保存在字符串[]中)。 I wish to open each workbook one by one, extract a range of data from each workbook and paste it in the "Gradebook". 我希望一个一个地打开每个工作簿,从每个工作簿中提取一系列数据并将其粘贴到“ Gradebook”中。 So far capturing the filenames in the array is working and so is the ability to select single workbook and open it. 到目前为止,捕获数组中的文件名是可行的,选择单个工作簿并打开它的功能也是如此。

The Question: I am having problems with the foreach statement. 问题:我在使用foreach语句时遇到问题。 As I loop through the workbooks, the line: Excel.Workbook stdWorkbook = excelApp.Workbooks.Open(stdFile) has an error: "the name stdFile does not exist in the context". 当我遍历工作簿时,以下行: Excel.Workbook stdWorkbook = excelApp.Workbooks.Open(stdFile)出现错误:“名称stdFile在上下文中不存在”。

Also, I am rather new to programming and seem to be having problems finding anything that explains interop with Excel and C# in simple enough terms for me to understand. 另外,我对编程还很陌生,似乎在查找任何足以解释Excel和C#互操作的问题时遇到了问题。 I would greatly appreciate any direction to a good 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 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;
        }
    }
}
foreach (string stdFile in fileNames);

The semi-colon at the end of this line terminates the statement immediately, so stdFile is not available in the code further down. 该行末尾的分号立即终止了该语句,因此在下面的代码中stdFile不可用。 Remove the semi-colon. 删除分号。

There is a straight-forward introduction to Excel Interop here at dotnetperls . 在dotnetperls上有Excel Interop的简单介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Nth个Excel工作簿中的Xth工作表复制到一个新的工作簿中,使用哪种语言? - Copying Xth sheet from N Excel workbooks into a new workbook-which language to use? 将特定范围的 Excel 单元格从一个工作表复制到另一个工作表 - copying of specific range of excel cells from one worksheet to another worksheet 使用 VSTO 将一系列单元格从 excel 复制到 powerpoint - Copying a range of cells from excel into powerpoint using VSTO 从特定关键字下方复制特定范围的excel单元格 - Copying specific range of excel cells from below a specific keyword 如何使用C#将一个Excel工作簿中特定范围的单元格复制到另一工作簿中 - how to copy cells from a specific range from one excel workbook to another workbook using c# 使用跳过的列从Excel复制数据并粘贴到DataGridView中 - Copying Data with Skipped Columns From Excel and Pasting into DataGridView 在Excel中从范围中删除单元格 - Remove cells from range in Excel 将单元格从Excel复制到Word时如何保持单元格的格式 - How to maintain formatting of cells when copying cells from Excel to Word C#Excel:从其他工作簿复制工作表时出现问题 - C# Excel: Issue in copying worksheet from different workbook Powershell:将数据从一个Excel工作簿复制到另一个 - Powershell: Copying data from one Excel workbook to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM