Excel工作表更改事件未在C#中触发

时间:2019-03-04 07:06:40

标签: c# excel worksheet-function worksheet

我正在努力从列表中的excel中填充数据,并将该数据发送到外部源。我既可以执行此步骤,也可以在用户更改任何单元格值时获取值。我尝试了以下链接:Excel worksheet change event is not firing when two workbooks are open Excel worksheet change event is not firing 我正在使用相同的代码,但无法获取单元格的编辑值。 请找到我的源代码。预先感谢。

private static Application excelApp = null;
private static Workbook excelBook = null;
private static Worksheet excelSheet = null;
private static Range excelRange = null;
private static Hashtable myHashtable;
private static string jsonData = string.Empty;
private static DocEvents_ChangeEventHandler EventDel_CellsChange;


 public static void ReadFile()
    {
        Console.Clear();
        if (_enableconsoleLog.ToLower() == "no")
        {
            Console.WriteLine("Successfully connected to channel IP : " + 
SendData._hostName + " | Port : " + SendData._port);
        }
        try
        {
            excelApp = new Application();
            if (excelApp == null)
            {
                Console.WriteLine("Excel is not installed!!");
                logger.Error("Excel is not installed!!");
                return;
            }
            var extenstion = Path.GetExtension(_excelPath);
            if (extenstion == ".xlsx" || extenstion == ".xlsm")
            {
                excelBook = excelApp.Workbooks.Open(_excelPath);
                excelSheet = excelBook.Sheets[_sheetName];
                excelSheet.Activate();
                excelApp.EnableEvents = true;
                EventDel_CellsChange = new 
  DocEvents_ChangeEventHandler(CellsChange);
                excelSheet.Change += EventDel_CellsChange;
                excelRange = excelSheet.UsedRange;
                if (_enableconsoleLog.ToLower() == "yes")
                {
                    Console.WriteLine("Successfully connected to channel IP : " + SendData._hostName + " | Port : " + SendData._port);
                    Console.Write("\nData of the file ( " + _excelPath + " ) is  : \n");
                }
                int rows = excelRange.Rows.Count;
                int cols = excelRange.Columns.Count;
                for (int i = 1; i <= rows; i++)
                {
                    var list = new List<string>();
                    if (_enableconsoleLog.ToLower() == "yes")
                        Console.Write("\n");
                    for (int j = 1; j <= cols; j++)
                    {
                        str = String.IsNullOrEmpty(Convert.ToString((excelRange.Cells[i, j] as Range).Value)) ? String.Empty : Convert.ToString((excelRange.Cells[i, j] as Range).Value);
                        list.Add(str);
                        if (_enableconsoleLog.ToLower() == "yes")
                            Console.Write(str + "\t");
                    }
                    PopulateData(list);
                }
            }
            else
            {
                if (_enableconsoleLog.ToLower() == "yes")
                {
                    Console.WriteLine("File is not in correct format.\nThe entered file is : " + _excelPath);
                }
                logger.Error("File is not in correct format");
            }
        }
        catch (Exception ex)
        {
            if (_enableconsoleLog.ToLower() == "yes")
                Console.WriteLine("Error in Reading Excel file : " + _excelPath + ".\nPlease Check the file.");
            logger.Error("Error in Reading Excel file. Please Check the file." + ex);
        }

public static void CellsChange(Range Target)
    {
        Debug.WriteLine("Delegate: You Changed Cells " + Target.get_Address(Missing.Value, Missing.Value, XlReferenceStyle.xlA1, Missing.Value, Missing.Value) + " on " + Target.Worksheet.Name);
    }

0 个答案:

没有答案