C#wpf如何传递变量Excel。工作表从按钮点击到另一个按钮点击

C#wpf如何传递变量Excel。工作表从按钮点击到另一个按钮点击,c#,excel,wpf,worksheet-function,C#,Excel,Wpf,Worksheet Function,我正在编写C#WPF easy程序,用于对excel中的特定列进行排序,在将变量excel.worksheet从单击按钮传递到另一个单击按钮时遇到问题。 代码如下: using Microsoft.Win32; using System.IO; using System; using System.Windows; using Excel = Microsoft.Office.Interop.Excel; namespace Sorting { public partial class

我正在编写C#WPF easy程序,用于对excel中的特定列进行排序,在将变量excel.worksheet从单击按钮传递到另一个单击按钮时遇到问题。 代码如下:

using Microsoft.Win32;
using System.IO;
using System;
using System.Windows;
using Excel = Microsoft.Office.Interop.Excel;

namespace Sorting
{
    public partial class MainWindow : Window
    {

        int column, CounterRow;
        string filepath;
        Excel.Worksheet x = new Excel.Worksheet();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Choose_Files_Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.Filter = "All files (*.*)|*.*";
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            if (ofd.ShowDialog() == true)
            {
                ListBox_Files.Items.Clear();
                filepath = ofd.FileName;
                ListBox_Files.Items.Add(Path.GetFileName(filepath));

            }
            if (ListBox_Files.Items.Count != 0) ListBox_Files.IsEnabled = Otworz.IsEnabled = true;
        }

        private void Button_Click_0(object sender, RoutedEventArgs e)
        {
            Excel.Application excel = new Excel.Application();
            excel.Visible = true;
            excel.DisplayAlerts = false;

            Excel.Workbook sheet = excel.Workbooks.Open(filepath);
            Excel.Worksheet x = excel.ActiveSheet as Excel.Worksheet;

            MessageBox.Show("select column");

            Sort(x,1);

            MessageBox.Show("end");

            excel.Quit();
        }

        void Button_Click(object sender, RoutedEventArgs e)
        {
            Excel.Application excel = new Excel.Application();
            excel.Visible = true;
            excel.DisplayAlerts = false;

            Excel.Workbook sheet = excel.Workbooks.Open(filepath);
            Excel.Worksheet x = excel.ActiveSheet as Excel.Worksheet;

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Sort(x, 1);
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
           Sort(x, 2);
        }

        private void Sort(Excel.Worksheet excel, int info)
        {
            Excel.Range rng = excel.Application.ActiveCell;
            column = rng.Column;            

            Excel.Range userRange = excel.UsedRange;
            CounterRow = userRange.Rows.Count;

            Excel.Range range = excel.Range[excel.Cells[7, 1], excel.Cells[CounterRow - 1, 8]];

            if (info == 1) range.Sort(range.Columns[column], Excel.XlSortOrder.xlAscending);
            else if (info == 2) range.Sort(range.Columns[column], Excel.XlSortOrder.xlDescending);
        }
    }
}

谁能解释一下,为什么单击按钮0起作用,而我通过单击按钮打开文件,然后通过单击按钮1对列进行排序,单击按钮2不起作用?VisualStudio说:“System.InvalidCastException”

当您正在将
x
转换为
Excel时,它不起作用。工作表
类型,但实际上
x
不能转换为上述类型。在进行调试时,您可以检查
x
的类型,并验证“x”是否可以转换为
Excel。工作表
typeOk,谢谢您的回答:)但是您知道如何解决此问题吗?我应该如何声明此变量才能使其工作?没关系;)我刚刚解决了这个问题:)我必须在主类的开头声明:Excel.Worksheet x;Excel.Application Excel=新建Excel.Application();我还必须添加:Excel.Worksheet x=Excel.ActiveSheet作为Excel.Worksheet;在“每个单击按钮”对话框中,该按钮无效。当我更仔细地阅读时,我把它读了出来