For Each 循环的 Excel Interop 错误消息

问题描述 投票:0回答:1

我正在尝试重新创建一个我在 c# 中使用的 excel 宏,但遇到了一个我无法解决的错误。基本上,该程序从第一个工作表中获取原始数据,应该根据 AA 列对其进行过滤,然后复制该范围并将其粘贴到它自己的新工作表中。我不断收到此错误:System.Runtime.InteropServices.COMException:'远程过程调用失败。 (来自 HRESULT 的异常:0x800706BE)'在每个语句之后的行上,有什么想法吗?

            foreach (var facility in machine)
            {
                firstWS.Range["A1:I" + lastRow2].AutoFilter(3, machine, XlAutoFilterOperator.xlFilterValues, Type.Missing, Type.Missing);
                Excel.Range visibleCells = firstWS.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeVisible, Type.Missing);
                firstWS.AutoFilter.Range.Copy();
                Excel.Worksheet newWS = xlApp2.Worksheets.Add(After: xlWorkbook2.Sheets[xlWorkbook2.Sheets.Count]);
                newWS.AutoFilterMode = false;
                newWS.Paste();

            }

如果有帮助,下面是完整的代码,抱歉有点乱,我是 C# 的新手:

        private void button3_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp2;
            Excel.Workbook xlWorkbook2;
            Excel.Worksheet ws2;
            Excel.Worksheet newSheetName;
            Excel.Range rng3;
            Excel.Range rng4;

            long lastRow2;
            long fullRow;
            xlApp2 = new Excel.Application();

            //open workbook
            xlWorkbook2 = xlApp2.Workbooks.Open(fileExcel, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            //system hides the workbook from the user while the code runs
            xlApp2.Visible = true;
           
            //declare the first worksheet "Report" as firstWS
            Worksheet firstWS = xlWorkbook2.Worksheets["Report"];
            //Sort the worksheet by column C **work in progress**
            fullRow = firstWS.Rows.Count;
            lastRow2 = firstWS.Cells[fullRow, "I"].End(Excel.XlDirection.xlUp).Row;
            rng3 = firstWS.Range["A1:I" + lastRow2];
            rng3.Sort(rng3.Columns[3], XlSortOrder.xlAscending);
            
            //firstWS.Columns["C"].Sort("C", XlSortOrder.xlAscending);
            //Delete columns K:N, F:I and then update the column names of F1:I1
            firstWS.Range["K:N"].Delete(XlDeleteShiftDirection.xlShiftToLeft);
            firstWS.Range["F:I"].Delete(XlDeleteShiftDirection.xlShiftToLeft);
            firstWS.Range["F1"].Value = "Current Qty";
            firstWS.Range["G1"].Value = "NDC";
            firstWS.Range["H1"].Value = "Pkg Sz of Comm. Containers";
            firstWS.Range["I1"].Value = "Number of Full Comm. Containers";
            //Sets the filter for Column C to use to name the different worksheets
            firstWS.Columns[3].AdvancedFilter(XlFilterAction.xlFilterCopy, Type.Missing, CopyToRange: firstWS.Range["AA1"],Unique: true);
            firstWS.Range["A1:I" + lastRow2].Copy();
            Worksheet newerWSCopy = xlWorkbook2.Worksheets.Add();
            newerWSCopy.Name = "Reporting";
            rng4 = newerWSCopy.Range["A1"];
            rng4.PasteSpecial(XlPasteType.xlPasteValues);
            //machine = firstWS.Range["AA2", firstWS.Cells[fullRow, "AA"].End(Excel.XlDirection.xlUp)];

            Excel.Range machine = firstWS.Range["AA2", firstWS.Cells[firstWS.Rows.Count, "AA"].End(Excel.XlDirection.xlUp)];
            

            

            foreach (var facility in machine)
            {
                firstWS.Range["A1:I" + lastRow2].AutoFilter(3, machine, XlAutoFilterOperator.xlFilterValues, Type.Missing, Type.Missing);
                Excel.Range visibleCells = firstWS.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeVisible, Type.Missing);
                firstWS.AutoFilter.Range.Copy();
                Excel.Worksheet newWS = xlApp2.Worksheets.Add(After: xlWorkbook2.Sheets[xlWorkbook2.Sheets.Count]);
                newWS.AutoFilterMode = false;
                newWS.Paste();

            }```



I've tried reworking this line but keep getting this same error. 
c# excel excel-interop
1个回答
0
投票

我能够弄清楚,我需要更改 foreach 变量和自动筛选条件。

© www.soinside.com 2019 - 2024. All rights reserved.