How to determine whether Microsoft Excel or OpenOffice is installed on the system using jQuery
Asked Answered
M

2

1

I needed to export data of a table to Excel. I got a solution as below:

http://jsfiddle.net/jquerybyexample/xhYcD/

In the above example, it allows you to generate .xls file when Microsoft Excel is installed on the system but I have OpenOffice installed on my system, so when I add the line

 window.open('data:application/vnd.oasis.opendocument.spreadsheet,' + $('#dvData').html());

instead of

 window.open('data:application/vnd.ms-excel,' + $('#dvData').html());

then it generates the .ods file.

I need to determine whether Excel or Open Office is installed so that I can put the condition for the above two lines using jQuery or JavaScript.


EDIT

Can I put back to back the two conditions, is that a harm?

$("#btnExport").click(function(e) {
    window.open('data:application/vnd.oasis.opendocument.spreadsheet,' + $('#dvData').html());
    window.open('data:application/vnd.ms-excel,' + $('#dvData').html());
    e.preventDefault();
});

ANOTHER EDIT

Just tried this solution on IE8, it doesn't work. Any other alternatives for this solution or this can be fixed to work on IE?

Maighdiln answered 10/4, 2013 at 6:4 Comment(7)
You can't. Such information is not available to JavaScript.Anecdotage
your best bet is probably to ask the user which format he prefers, I also don't think there is a way for this that is cross browserSemicircle
I don't think it's possible with JavaScript. If you aren't sure about what the user is running, then include the option for different applications' file.Antagonist
Agreed! Please check the EDIT: update i put on my question; is that doeable or is it a harm?Maighdiln
Just make 2 buttons, one for each format. The User will choose which one he/she prefers.Hiphuggers
I really hope it is not possible.Gaona
@Imperative: I cannot provide two buttons to the user, we need to figure out which condition to invoke programatically...Maighdiln
M
0

As far as i know there exists a vague way , you can check the existing Mime types to ensure the presence

eg: application/x-msoffice for office

function GetMimeTypes () {
                var message = "";
                    // Internet Explorer supports the mimeTypes collection, but it is always empty
                if (navigator.mimeTypes && navigator.mimeTypes.length > 0) {
                    var mimes = navigator.mimeTypes;
                    for (var i=0; i < mimes.length; i++) {
                        message += "<b>" + mimes[i].type + "</b> : " + mimes[i].description + "<br />";
                    }
                }
                else {
                    message = "Your browser does not support this ";
                   //sorry!
                }

                return ( message);
            }
Marge answered 10/4, 2013 at 6:33 Comment(0)
D
0

i have posted question that me be your expected answer.hope this help you. Export HTML Table to EXCEL in Java script

function downloadsalesreport () {

            var cache = {};

            this.tmpl = function tmpl(str, data) {
                // Figure out if we're getting a template, or if we need to
                // load the template - and be sure to cache the result.
                var fn = !/\W/.test(str) ?
                  cache[str] = cache[str] ||
                    tmpl(document.getElementById(str).innerHTML) :

                  // Generate a reusable function that will serve as a template
                  // generator (and which will be cached).
                  new Function("obj",
                    "var p=[],print=function(){p.push.apply(p,arguments);};" +

                    // Introduce the data as local variables using with(){}
                    "with(obj){p.push('" +

                    // Convert the template into pure JavaScript
                    str.replace(/[\r\t\n]/g, " ")
                          .split("{{").join("\t")
                          .replace(/((^|}})[^\t]*)'/g, "$1\r")
                          .replace(/\t=(.*?)}}/g, "',$1,'")
                          .split("\t").join("');")
                          .split("}}").join("p.push('")
                          .split("\r").join("\\'")
                          + "');}return p.join('');");

                // Provide some basic currying to the user
                return data ? fn(data) : fn;
            };
 var tableToExcel = (function () {
 var uri = 'data:application/vnd.ms-excel;base64,',
                    template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{{=worksheet}}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>{{for(var i=0; i<tables.length;i++){ }}<table>{{=tables[i]}}</table>{{ } }}</body></html>',
                base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) },
                format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
                return function (tableList, name) {
                    if (!tableList.length > 0 && !tableList[0].nodeType) table = document.getElementById("#tablesalesentry")
                    var tables = [];
                    for (var i = 0; i < tableList.length; i++) { tables.push(tableList[i].innerHTML); }
                    var ctx = { worksheet: name || 'Worksheet', tables: tables };
                    window.location.href = uri + base64(tmpl(template, ctx))
                }
            })();

            tableToExcel(document.getElementsByTagName("table"), "one");

        }
Dirtcheap answered 8/11, 2013 at 12:27 Comment(1)
It would be great if you could summarize here, the part of your question that you link to, that actually answers this question. Otherwise at the moment this doesn't really answer the original question.Ovation

© 2022 - 2024 — McMap. All rights reserved.