Message 1 of 4
Excel to datagrid in Revit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I have a code (see below)that can load an excel file xls and xlsx in visual studio. This code i'd like to use in Revit. It is working with xls files but when i want to load xlsx i get the following error message. Is this because it isn't possible to read xlsx? i that case i will stay with xls. But maybe it's a simple solution that i couldn't find
private void button3_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); //create openfileDialog Object openFileDialog1.Filter = "XML Files (*.xml; *.xls; *.xlsx; *.xlsm; *.xlsb) |*.xml; *.xls; *.xlsx; *. xlsm; *.xlsb";//open file format define Excel Files(.xls)|*.xls| Excel Files(.xlsx)|*.xlsx| openFileDialog1.FilterIndex = 3; openFileDialog1.Multiselect = false; //not allow multiline selection at the file selection level openFileDialog1.Title = "Open Excel File"; //define the name of openfileDialog openFileDialog1.InitialDirectory = @"Desktop"; //define the initial directory if (openFileDialog1.ShowDialog() == DialogResult.OK) //executing when file open { string path = openFileDialog1.FileName; textBox1.Text = openFileDialog1.FileName; OleDbConnection connection = new OleDbConnection(String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + path + ";Extended Properties=\"Excel 12.0;HDR=YES;\"")); connection.Open(); OleDbCommand sqlCommand = new OleDbCommand("SELECT * FROM [Blad1$]", connection); OleDbDataAdapter adaObj = new OleDbDataAdapter(); adaObj.SelectCommand = sqlCommand; DataSet setObj = new DataSet(); adaObj.Fill(setObj); connection.Close(); dataGridView1.DataSource = setObj.Tables[0]; }