System.Runtime.InteropServices.COMException:'Range类的AutoFilter方法失败

时间:2018-11-19 18:23:47

标签: c# asp.net excel office-interop

在构建代码时,我经常遇到相同的错误。我尝试在工具的调试选项中禁用“仅我的代码”。还是在更早的时候,我遇到了一些Symbol错误,但是无论如何都对它进行了排序,现在“在mscorlib.dll中发生了'System.Runtime.InteropServices.COMException类型的未处理异常”

using Microsoft.Office.Interop.Excel;
using System;
using System.Reflection;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

namespace WindowsFormsApp6
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private string add = "";
    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            var user = Environment.UserName;
            openFileDialog1.InitialDirectory = @"C:\Users\" + user + @"\Downloads";
            openFileDialog1.Title = "Select File to Upload";
            openFileDialog1.FileName = string.Empty;
            openFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm;*.xlt;*.xltm;*.xltm";
            DialogResult _result = openFileDialog1.ShowDialog();

            if (_result == DialogResult.OK)
            {
                add = textBox1.Text = openFileDialog1.FileName;
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        //if (dateTimePicker1.Value >= dateTimePicker2.Value)
        //{
        //   MessageBox.Show("Date of Join cannot be less than or equal to Date of Birth");
        //}
        // dateTimePicker1_ValueChanged(sender,e);
        dateTimePicker1.Value = dateTimePicker1.Value.AddDays(2);          

        Excel.Application myApp = new Excel.Application();
        Excel.Workbook myWorkBook;
        Excel.Worksheet myWorkSheet;
        Excel.Range range;
        Excel.Range xlrange;
        myWorkBook = myApp.Workbooks.Open(add, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);           
        myWorkSheet = (Worksheet)myWorkBook.Sheets[2];
        range = (Range)myWorkSheet.Application.Rows[1, Type.Missing];
        range.Select();
        range.Delete(XlDirection.xlUp);
        xlrange = myWorkSheet.UsedRange;
        string xx = textBox2.Text.Trim();
        String[] FilterList =  { xx };
        xlrange.AutoFilter(3, FilterList, Excel.XlAutoFilterOperator.xlFilterValues, Type.Missing, true);           
        //myWorkBook.Save();
        myWorkBook.Close(true);
        myApp.Quit();
     }

 [The image is for exception unhandled error I'm getting][1]  }
}

输出窗口也有一些错误“ WindowsFormsApp6.exe”(CLR v4.0.30319:WindowsFormsApp6.exe):已加载“ C:\ WINDOWS \ assembly \ GAC_MSIL \ Microsoft.Office.Interop.Excel \ 15.0.0.0__71e9bce111e9429c \ Microsoft.Office.Interop.Excel.dll'。该模块不带符号。”

0 个答案:

没有答案