How to accept input from custom excel file using javascript

hi guys .. I am making a page in which I have to take the excel file from the user and read this file .. but all I know is to read the specific file by specifying its address .. for this I use javascript and html. code to read excel file

<input type="button" id="btnSubmit" onclick="readdata(1, 2)" value="Submit" /> <script> function readdata(x,y) { try { var excel = new ActiveXObject("Excel.Application"); excel.Visible = false; var excel_file = excel.Workbooks.Open("D:\\Test.xls");// alert(excel_file.worksheets.count); var excel_sheet = excel_file.Worksheets("Sheet1"); var data = excel_sheet.Cells(x, y).Value; //alert(data); drawWithexcelValue(data); } catch (ex) { alert(ex); } // return data; } </script> 

This is my code for reading an excel file, defining the path to a specific excel file ... now I need to do to extract the excel file from the user ..guys, please help ..

And any help through the code would be really appreciated.

someone please help me

+4
source share
1 answer

As mentioned in @Alex K, this will only work in IE. Here a solution is possible:

Put all your Excel files in the same folder. For example: C:\sheets\ (Edit: I think this is the easiest solution, since I do not know how to get the full and real file path through javascript)

Now change the code to something in these lines:

 <input type="file" id='excelFile' /> <button id='loadExcel'>Load Excel File</button> <script type="text/javascript"> var EXCELDIR = "C:\\sheets\\"; var loadExcel = document.getElementById('loadExcel'); loadExcel.onclick = function() { var filepath = EXCELDIR + document.getElementById('excelFile').value.split('\\')[2]; readdata(1,2, filepath); } function readdata(x,y,filepath) { try { var excel = new ActiveXObject("Excel.Application"); excel.Visible = false; var excel_file = excel.Workbooks.Open(filepath); alert(excel_file.worksheets.count); var excel_sheet = excel_file.Worksheets("Sheet1"); var data = excel_sheet.Cells(x, y).Value; //alert(data); drawWithexcelValue(data); } catch (ex) { alert(ex); } // return data; } </script> 

Disclaimer: I have not tested the code since I am on a mac, hope this works.

0
source

Source: https://habr.com/ru/post/1488105/


All Articles