且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在客户端读取excel文件内容?

更新时间:2022-10-15 13:47:17

通常不可能通过浏览器中的JavaScript读取/写入任何文件。所以没有任何额外的插件,你将无法从浏览器读/写Excel文件。 Excel的ActiveX对象允许您执行此操作,但只有IE支持ActiveX对象。



可能还有其他浏览器的其他插件,但我并不知道。 p>

首先,为什么要这样做?你可以给一个用例吗?或许有更好的选择可用于您正在尝试的。



更新



您将必须将excel文件传递到服务器,并在服务器端读取excel(例如在servlet中)。



您将不得不在多部分内的JSP中使用< input type ='file'> 表单

< form name =myFormaction =myServletenctype =multipart / form-datamethod =post>



在服务器端,您可能需要使用 Apache共享文件上传



一旦您拥有该文件(或其上的流),您可以使用 Apache POI HSSF / XSSF ,然后将数据更新到数据库或将其传回给JSP


From the JSP page, I need to browse excel file and after selecting file on system, I need to read that excel file contents and fill my form.

Currently I have tried with below code but its only working in IE with some changes in IE internet options for ActiveXObject. Its not working in rest of the browsers.

<script>
    function mytest2() {
        var Excel;
        Excel = new ActiveXObject("Excel.Application"); 
        Excel.Visible = false;
        form1.my_textarea2.value = Excel.Workbooks.Open("C:/Documents and Settings/isadmin/Desktop/test.xlsx").ActiveSheet.Cells(1,1).Value;
        Excel.Quit();
    }
</script>

Please suggest some solution so that it works in all browsers.

It is generally not possible to read/write any file via JavaScript in a browser. So without any additional plug-ins you will not be able to read/write Excel files from the browser. The ActiveX objects of Excel let you do this, but only IE supports ActiveX objects.

There may be other plugins for other browsers, but i am aware of none.

In the first place, why do you want to do that? Can you give a use case? Perhaps there are better options available than what you are trying.

UPDATE

You will have to pass the excel file to the server and do the reading of the excel in the server side (in a servlet for instance).

You will have to use a <input type='file'> in the JSP within a multipart form
<form name="myForm" action="myServlet" enctype="multipart/form-data" method="post">

On the server side, you may want to use Apache Commons File Upload.

Once you have the file (or a stream on it) you can parse the file using, say, Apache POI HSSF/XSSF and then update the data to a database or pass it back to a JSP