1

Please, is it possible to read documents like excel sheets, notepad etc into JavaScript and use the data in the document for computations. Please, if it is possible, I would love to know how to do it. Thank you

4
  • You can use AJAX requests to load files from the same domain/port as your website, and parse them according to their file schemes. There are libraries or APIs for most interesting file formats, for instance the JavaScript API for office is discussed here: msdn.microsoft.com/en-us/magazine/jj891051.aspx Commented May 21, 2016 at 19:12
  • on a web page? for example using an upload button/ Commented May 21, 2016 at 19:13
  • You can use excel sheets as data source files as long as you save them in csv format. Without any problems. If your aim is to build a desktop html application - targeting Explorer, you can make use of FileSystemObject to. Search with: google.com/#q=msdn+filesystemobject+excel+JScript Commented May 21, 2016 at 19:30
  • @chika please consider marking the answer if it helped you, otherwise leave a comment if you need further info. Commented Jun 8, 2016 at 9:40

1 Answer 1

1

You can access the selected file without uploading it, but you'll need the HTML5 file API:

  function readSingleFile(evt) {
    //Retrieve the first (and only!) File from the FileList object
    var f = evt.target.files[0]; 

    if (f) {
      var r = new FileReader();
      r.onload = function(e) { 
          var contents = e.target.result;
          alert(contents);
      }
      r.readAsText(f);
    } else { 
      alert("Failed to load file");
    }
  }

  document.getElementById('fileinput').addEventListener('change', readSingleFile, false);

Check this fiddle out:

Just select a txt file https://jsfiddle.net/n454do1d/1/

Sign up to request clarification or add additional context in comments.

1 Comment

It really helped me...thank you...sorry for replying this late

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.