Пример #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);
        }
Пример #2
0
        static void Main(string[] args)
        {
            string excelFilePath = string.Concat(excelFolderPath, "abc.xlsx");

            // Open excel appication
            Excel.Application excelApp = MyExcelUtils.OpenExcelApplication();

            // Open excel workbook
            Excel.Workbook workbook = MyExcelUtils.OpenWorkbook(excelApp, excelFilePath);
            if (workbook != null)
            {
                Excel.Sheets    sheets    = workbook.Sheets;
                Excel.Worksheet worksheet = sheets.Item["Test 1"];

                Stopwatch sw = new Stopwatch();
                Console.WriteLine("Worksheet name: Test 1");
                Console.WriteLine($"Number of rows in worksheet: {worksheet.UsedRange.Rows.Count}");
                Console.WriteLine($"Number of columns in worksheet: {worksheet.UsedRange.Columns.Count}");
                Console.WriteLine();

                ///
                Console.WriteLine($"Search All Values in Column - RESULT");
                sw.Start();
                List <Excel.Range> searchResults1 = SearchAllValuesInColumn(worksheet, 2, "12");
                sw.Stop();

                long   timeInMiliSeconds1 = sw.ElapsedMilliseconds;
                double timeInSeconds1     = (double)timeInMiliSeconds1 / 1000;

                Console.WriteLine($"Found {searchResults1.Count} result(s)\n" +
                                  $"\tin {timeInSeconds1} second(s)\n" +
                                  $"\tin {timeInMiliSeconds1} milisecond(s)");
                Console.WriteLine();

                ///
                Console.WriteLine($"Search All Values in Row - RESULT");
                sw.Start();
                List <Excel.Range> searchResults2 = SearchAllValuesInRow(worksheet, 5, "12");
                sw.Stop();

                long   timeInMiliSeconds2 = sw.ElapsedMilliseconds;
                double timeInSeconds2     = (double)timeInMiliSeconds2 / 1000;

                Console.WriteLine($"Found {searchResults2.Count} result(s)\n" +
                                  $"\tin {timeInSeconds2} second(s)\n" +
                                  $"\tin {timeInMiliSeconds2} milisecond(s)");
            }


            // Save and Close workbook
            //workbook.Save();
            MyExcelUtils.CloseWorkbook(workbook, false);

            // Close Excel application
            MyExcelUtils.CloseExcelApplication(excelApp);
        }