如何在C#中从Excel搜索然后选择特定行?

时间:2019-03-31 06:38:09

标签: c# .net excel-interop

这可能是简单重复的问题,但是我是Interop.Excel的新手。我正在使用Excel作为数据库。我有此功能可以在现有文件中插入新行

public static string filePath = @"new.xlsx";
private static void AddNewRowsToExcelFile()
        {
            IList<Employee> empList = new List<Employee>() {
        new Employee(){Card_ID=1003, Counter_ID=2, Scanner_ID=3, Ip="192.168.2.76"}
    };

            Excel.Application xlApp = new Excel.Application();

            Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath, 0, false, 5, "", "", false,
                Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            Excel.Range xlRange = xlWorkSheet.UsedRange;
            int rowNumber = xlRange.Rows.Count + 1;

            foreach (Employee emp in empList)
            {
                xlWorkSheet.Cells[rowNumber, 1] = emp.Counter_ID;
                xlWorkSheet.Cells[rowNumber, 2] = emp.Card_ID;
                xlWorkSheet.Cells[rowNumber, 3] = emp.Scanner_ID;
                xlWorkSheet.Cells[rowNumber, 4] = emp.Ip;
                rowNumber++;
            }

            // Disable file override confirmaton message  
            xlApp.DisplayAlerts = false;
            xlWorkBook.SaveAs(filePath, Excel.XlFileFormat.xlOpenXMLWorkbook,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange,
                Excel.XlSaveConflictResolution.xlLocalSessionChanges, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value);
            xlWorkBook.Close();
            xlApp.Quit();

            Marshal.ReleaseComObject(xlWorkSheet);
            Marshal.ReleaseComObject(xlWorkBook);
            Marshal.ReleaseComObject(xlApp);

            Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("\nRecords Added successfully...");
            Console.BackgroundColor = ConsoleColor.Black;
        }
        public class Employee
        {
            public int Card_ID { get; set; }
            public int Counter_ID { get; set; }
            public int Scanner_ID { get; set; }
            public string Ip { get; set; }
        }

这是读取Excel文件的功能

private static void ReadExcelFile()
        {
            Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("\nReading the Excel File...");
            Console.BackgroundColor = ConsoleColor.Black;

            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath);
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            Excel.Range xlRange = xlWorkSheet.UsedRange;
            int totalRows = xlRange.Rows.Count;
            int totalColumns = xlRange.Columns.Count;

            string firstValue, secondValue, ThirdValue, FourthValue;

            for (int rowCount = 1; rowCount <= totalRows; rowCount++)
            {

                firstValue = Convert.ToString((xlRange.Cells[rowCount, 1] as Excel.Range).Text);
                secondValue = Convert.ToString((xlRange.Cells[rowCount, 2] as Excel.Range).Text);
                ThirdValue = Convert.ToString((xlRange.Cells[rowCount, 3] as Excel.Range).Text);
                FourthValue = Convert.ToString((xlRange.Cells[rowCount, 4] as Excel.Range).Text);


                Console.WriteLine(firstValue + "\t" + secondValue + "\t" + ThirdValue + "\t" + FourthValue);

            }

            xlWorkBook.Close();
            xlApp.Quit();

            Marshal.ReleaseComObject(xlWorkSheet);
            Marshal.ReleaseComObject(xlWorkBook);
            Marshal.ReleaseComObject(xlApp);

            Console.WriteLine("End of the file...");

        }

这些是我处理硬代码数据(用于学习目的)的简单功能

现在,我想知道如何搜索特定的列单元格,然后从excel中获取该行? 谢谢

1 个答案:

答案 0 :(得分:0)

如果您只想读取没有索引的内容,则可以传递列名,例如:

xlRange.Cells[rowCount, "B"] as Excel.Range

但是如果您想使用sql语法,请看一下此link