I would like to query an excel document using sql. I want to use c# and ado to perform the query. I don't want to install office on the server. Is there a way to do this?
-
Use a csv instead of the excel file. Mysql and sqlserver both have ways to use a csv file and read the data.Nived– Nived2016-05-13 13:51:24 +00:00Commented May 13, 2016 at 13:51
-
@Nived I have edited the questionLuke101– Luke1012016-05-13 13:54:07 +00:00Commented May 13, 2016 at 13:54
-
Could you create a dataTable from the byte array and query that?SierraOscar– SierraOscar2016-05-13 14:31:15 +00:00Commented May 13, 2016 at 14:31
Add a comment
|
1 Answer
You could use an OleDB connection to access your Excel spreadsheet, here is an example using DataTables
string connString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}; Extended Properties=Excel 12.0;", "myDocument.xlsx");
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM MyTable", connString);
DataSet ds = new DataSet();
adapter.Fill(ds, "TheData");
DataTable theTable = ds.Tables["TheData"];
Once you have done this you can access values like
theTable.Rows[indexOfRow].ItemArray[indexOfItem] //The items are stored as objects
This specific example is for .xlsx files
5 Comments
Luke101
Hi thanks for your answer. Is there a way to do this without saving the file to disk? Perhaps, query excel that is loaded in memory.
Alfie Goodacre
@Luke101 care to expand, maybe give an example? :)
Luke101
In the connection it has "myDocument.xlsx". This means I have to save the file to disk first. I have a byte array of the excel file. Is there a way to query the byte array instead? Without saving the excel file to disk.
Alfie Goodacre
@Luke101 Sorry, I'm not sure how to do that in all honesty, this link stackoverflow.com/questions/1490526/… has someone saying you can't and another saying that software they made can do it, but that's all I can help with there
SierraOscar
I always thought Extended Properties had to be
Excel 12.0 Xml for the openXML format?