String strArray = ConvertToStringArray(values) Range range = worksheet.get_Range("A" + i.ToString(), "J" + i.ToString()) how to get the count of rows in excel sheet using c#įor (int i = 2 i <= i++) loop through total rows of the spreadsheet and place each row in the list view Worksheet worksheet = (.Worksheet)sheets.get_Item(1) get the first and only worksheet from the collection of worksheets get the collection of sheets in the workbook Using (OpenFileDialog ofd = new OpenFileDialog() ) Private void btnOpen_Click(object sender, EventArgs e)
How to read data from excel sheet using c#Īdding a click event handler to the Open button allows you to open an excel file, then start excel application to read data from excel sheet and add data to the ListView control. String arrays = new string Īrrays = (string)values.GetValue(1, i).ToString() ĭeclaring an excel application variable. string ConvertToStringArray(Array values) Next, Set the GridLines property of ListView control to True.Ĭlicking Edit Column, then add the columns you want to read from excel file.Ĭreating a ConvertToStringArrary method allows you to convert array to string array.
You can find the .dll in locate C:\Windows\assembly\GAC_MSIL\Ĭlicking on your ListView control, then change View to Details type. We will use COM to read the excel file, so you need to add a reference to the .dll to your project. Right-clicking on your References, then select Add References. How to read excel file in c# windows application You can modify your layout as shown below. Creating a new Windows Forms Application, then drag ListView, Button controls from your Visual Studio into your form designer.