Exemple #1
0
        static void Main(string[] args)
        {
            Excel.Application excelApp      = MyExcelUtils.OpenExcelApplication();
            string            excelFilePath = MyExcelUtils.PromptUser("Type in excel file path: ");

            if (!excelFilePath.Equals("-q") || !excelFilePath.Equals("-Q"))
            {
                Excel.Workbook workbook = MyExcelUtils.OpenWorkbook(excelApp, excelFilePath);
                if (workbook != null)
                {
                    Excel.Sheets    sheets    = workbook.Sheets;
                    Excel.Worksheet worksheet = sheets.Item[1];

                    string startCell = MyExcelUtils.PromptUser("Enter start cell: ");
                    string endCell   = MyExcelUtils.PromptUser("Enter end cell: ");

                    if ((!startCell.Equals("-q") && !endCell.Equals("-q")) ||
                        (!startCell.Equals("-Q") && !endCell.Equals("-Q")))
                    {
                        Excel.Range selectedRange = MyExcelUtils.SelectRangeInSheet(worksheet, startCell, endCell);
                        int.TryParse(MyExcelUtils.PromptUser("Enter row index: "), out int rowIndex);
                        int.TryParse(MyExcelUtils.PromptUser("Enter column index: "), out int columnIndex);

                        string cellValue = MyExcelUtils.GetValueOfCellInRange(selectedRange, rowIndex, columnIndex);

                        Console.WriteLine($"The value of cell({rowIndex}, {columnIndex}) is \"{cellValue}\"");
                    }
                    MyExcelUtils.CloseWorkbook(workbook, true);
                }
            }
            MyExcelUtils.CloseExcelApplication(excelApp);
        }