簡體   English   中英

循環瀏覽Excel工作簿,從每個工作簿復制一定范圍的單元格並粘貼到主工作簿

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

嗨:我很高興你們都在那里,因為應該如此簡單的事情似乎並不適合我。

背景:我正在嘗試使用C#打開Excel工作簿(使用OpenFileDialog選擇它-稱為“ Gradebook”)。 然后,我遍歷多個Excel工作簿(在我用FolderBrowserDialog選擇的文件夾中-將文件名保存在字符串[]中)。 我希望一個一個地打開每個工作簿,從每個工作簿中提取一系列數據並將其粘貼到“ 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;
        }
    }
}
foreach (string stdFile in fileNames);

該行末尾的分號立即終止了該語句,因此在下面的代碼中stdFile不可用。 刪除分號。

在dotnetperls上有Excel Interop的簡單介紹。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM