I often end up running a big SPARQL query (usually on a server), exporting the results a TSV, and post processing the results with some combination of vi, perl, awk, sort etc., then loading the processed data into a copy of Excel to get stats out of it, or produce a chart or whatever.
The other day I was wondering if you could pull results directly from a SPARQL endpoint into Excel. Well, it turns out that you can, via something called an Internet Query File.
I’ll give a quick example of how to make it work with a local copy of 4store, then show one on a remote SPARQL endpoint.
Local 4store database
First, create a local 4store database, and import some DOAP files:
$ 4s-backend-setup doap
$ 4s-backend doap
$ 4s-import doap http://4store.org/doap.rdf
$ 4s-import doap http://librdf.org/rasqal/rasqal.rdf
$ 4s-httpd doap
Next, you need to create your .iqy file, it should have the following lines, but needs to use CRLF (\r\n) line endings, you can download this example:
WEB
1
http://localhost:["port", "Port number"]/sparql/?output=text&query=["query","SPARQL query."]
PreFormattedTextToColumns=True
ConsecutiveDelimitersAsOne=False
SingleBlockTextImport=False
Save this file as 4store.iqy.
Now, open Excel, and fill out column A with the following lines (one line per cell)
Query
PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX doap: <http://usefulinc.com/ns/doap#>
SELECT ?name ?homepage (GROUP_CONCAT(STR(?l); separator=", ") AS ?licenses)
WHERE { ?proj doap:name ?name ; doap:homepage ?homepage ; doap:license ?l }
GROUP BY ?proj
Next, select a cell under the query, open the Data menu, and pick Get External Data, Run Saved Query…

Now pick your .iqy file using the file browser, and open it with Get Data. Now, you will have a dialog called Returning Data to Microsoft Excel, or something similar, just take the default options.
Now you will be asked for a port number, if it’s a default 4store install you should enter 8080 in the textbox, and select Use this value/reference for future refreshes.

Now you need to tell it where to get the query text from, enter =A2:A5 into the text box, and select Use this value/reference for future refreshes.

When you click OK after a short delay you should see a table filled out with the results:

Tips & Tricks
You can rerun the SPARQL query by right clicking on the cell you did the Data binding from, and selecting Refresh Data.
The output=text CGI parameter in the URI tells 4store to output TSV results, instead of SPARQL XML. According to the Microsoft documentation web queries only process HTML, but not only does TSV work, it seems to be a lot faster. The correct way would be to set the accept header, but I don’t know of any way to do that in the .iqy syntax.
You might be wondering why the query is broken up over lots of cells, well, two reasons:
Editing long sections of text in Excel is painful, so it’s easier to edit if you can just to significant portions of the query easily.
If text cells in Excel go over 255 characters long, lots of Bad Things happen internally, and you wont be able to run the query. For some reason ranges of cells work fine, the strings get concatenated.
If you’re using 5store, instead of parametrising the port number, you can pick a KB name:
http://localhost:8080/["kb", "KB name"]/sparql?output=text&query=["query","SPARQL query."]
Querying Remote Endpoints
Create the following .iqy file:
WEB
1
http://dbpedia.org/sparql?format=text/html&query=["query","SPARQL query."]
Selection=EntirePage
PreFormattedTextToColumns=True
Or you can download a premade one. Fill out a spreadsheet like:
Query
PREFIX yago: <http://dbpedia.org/class/yago/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?mfr (STR(?n) AS ?name) (STR(?found) AS ?founded)
WHERE { ?mfr a yago:SportsCarManufacturers ; rdfs:label ?n OPTIONAL { ?mfr <http://dbpedia.org/property/foundation> ?found } FILTER(LANG(?n) = "en") }
ORDER BY ?name
And follow the Excel instructions as per a local 4store, but with the dbpedia.iqy file.

For some reason processing HTML results is much slower than TSV. The dbpedia endpoint returns pretty quickly in curl, but Excel takes ages to ingest the data.
You can also download a premade XLSX file with the .iqy file baked in.