简体   繁体   English

VSTO C# Excel 工作簿所有工作表

[英]VSTO C# Excel workbook all worksheets

I am using C# VSTO to make a text replacement in all Excel files in a directory.我正在使用 C# VSTO 在目录中的所有 Excel 文件中进行文本替换。 Currently, my code works, but only to the active worksheet (first worksheet) of each Excel file.目前,我的代码有效,但仅适用于每个 Excel 文件的活动工作表(第一个工作表)。 How do I perform this for all the existing worksheets in an Excel file:如何对 Excel 文件中的所有现有工作表执行此操作:

public void runFiles(string _path)
        {
            string path = _path;
            object m = Type.Missing;

            var xlApp = new Microsoft.Office.Interop.Excel.Application();

            DirectoryInfo d = new DirectoryInfo(path);

            FileInfo[] listOfFiles_1 = d.GetFiles("*.xlsx*").ToArray();
            FileInfo[] listOfFiles_2 = d.GetFiles("*.xls*").ToArray();

            FileInfo[] listOfFiles = listOfFiles_1.Concat(listOfFiles_2).ToArray();

            xlApp.DisplayAlerts = false;
            foreach (FileInfo file in listOfFiles)
            {

                var xlWorkBook = xlApp.Workbooks.Open(file.FullName);
                Excel.Worksheet xlWorkSheet = xlWorkBook.Worksheets;

                // get the used range. 
                Excel.Range r = (Excel.Range)xlWorkSheet.UsedRange;

                // call the replace method to replace instances. 
                bool success = (bool)r.Replace(
                    "Engineer",
                    "Designer",
                    Excel.XlLookAt.xlWhole,
                    Excel.XlSearchOrder.xlByRows,
                    true, m, m, m);

                xlWorkBook.Save();
                xlWorkBook.Close();
            }

            xlApp.Quit();

            Marshal.ReleaseComObject(xlApp);
        }

xlWorkBook.ActiveSheet only grabs the only first sheet. xlWorkBook.ActiveSheet只抓取唯一的第一张纸。 I tried xlWorkBook.Worksheets , but I am getting an error of Cannot implicitly convert type 'Microsoft.Office.Interop.Excel.Sheets' to 'Microsoft.Office.Interop.Excel.Worksheet'. An explicit conversion exists (are you missing a cast?)我尝试了xlWorkBook.Worksheets ,但我收到了Cannot implicitly convert type 'Microsoft.Office.Interop.Excel.Sheets' to 'Microsoft.Office.Interop.Excel.Worksheet'. An explicit conversion exists (are you missing a cast?) Cannot implicitly convert type 'Microsoft.Office.Interop.Excel.Sheets' to 'Microsoft.Office.Interop.Excel.Worksheet'. An explicit conversion exists (are you missing a cast?)

Worksheets property is a collection of worksheet. Worksheets属性是工作表的集合。 So you just need iterate through it.所以你只需要遍历它。

foreach (Excel.Worksheet xlWorkSheet in xlWorkBook.Worksheets)
{
    // get the used range. 
    Excel.Range r = (Excel.Range)xlWorkSheet.UsedRange;

    // call the replace method to replace instances. 
    bool success = (bool)r.Replace(
        "Engineer",
        "Designer",
        Excel.XlLookAt.xlWhole,
        Excel.XlSearchOrder.xlByRows,
        true, m, m, m);
}

The Worksheets method returns an array of worksheets. Worksheets方法返回一个工作表数组。 I have to edit each element in the array by doing this我必须通过这样做来编辑数组中的每个元素

                foreach (Excel.Worksheet xlWorkSheet in xlWorkBook.Worksheets)
                {
                    // get the used range. 
                    Excel.Range r = (Excel.Range)xlWorkSheet.UsedRange;

                    // call the replace method to replace instances. 
                    bool success = (bool)r.Replace(
                        "Engineer",
                        "Designer",
                        Excel.XlLookAt.xlWhole,
                        Excel.XlSearchOrder.xlByRows,
                        true, m, m, m);
                }
                xlWorkBook.Save();
                xlWorkBook.Close();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM