Help me with my code, because i don’t now how to do this simple task. How to select 10 random rows from excel file?
string filepath = @"C:\1.xlsx";
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + @";Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text""";
OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet1$]", conn);
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(ds);
showdata.DataSource = ds.Tables[0];
If you create a DataGridView on your Windows Form called
RandomExcelRows, create a button calledbutton1and put the following code inside the Click event handler forbutton1:And then create a method underneath called RandomRows containing the following:
The program will take a random number of rows (this amount is specified by you) from the Spreadsheet you specify and place these rows into your DataGridView.
It’s pretty crude, and needs refactoring but it is the basis of what you need. You could also get the string containing the file location from a textbox instead of hardcoding this, the same goes for the number of rows you want, and the other parameters such as minimum and maximum rows.
As well, you could use an OpenFileDialog to allow your user to browse to your excel file, etc etc.