【问题标题】:VSTO C# Excel workbook all worksheetsVSTO C# Excel 工作簿所有工作表
【发布时间】:2019-12-14 02:37:49
【问题描述】:

我正在使用 C# VSTO 在目录中的所有 Excel 文件中进行文本替换。目前,我的代码有效,但仅适用于每个 Excel 文件的活动工作表(第一个工作表)。如何对 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 只抓取唯一的第一张纸。我试过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?)的错误

【问题讨论】:

    标签: c# excel vsto


    【解决方案1】:

    Worksheets 属性是工作表的集合。所以你只需要遍历它。

    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);
    }
    

    【讨论】:

      【解决方案2】:

      Worksheets 方法返回一个工作表数组。我必须通过这样做来编辑数组中的每个元素

                      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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-09
        • 1970-01-01
        • 2016-07-04
        • 2019-10-25
        • 1970-01-01
        • 1970-01-01
        • 2011-01-23
        相关资源
        最近更新 更多