简体   繁体   English

如何从Java JSP JavaScript打开Excel?

[英]How can I open an excel from a java jsp javascript?

I have a JSP, with a link: Open EXCEL file . 我有一个带有链接的JSP: Open EXCEL file If you click on it, it should open the something.xls with microsoft excel . 如果单击它,它将使用Microsoft excel打开something.xls

I tried it with the <a> tag, but it doesn't work: 我用<a>标记尝试过,但是不起作用:

<a href="./something.xls" target="_blank">
    Open EXCEL file
</a>

Could anyone give me some useful code, and help, how to open an xls by excel from jsp? 谁能给我一些有用的代码,以及帮助,如何从jsp中打开excel的xls? Thank you! 谢谢!

NO simply,you cannot open like that,Since the files can be open/downloaded by browser,and HTML/Javascript cannot have access to those system level programms. ,您不能那样打开,因为可以通过浏览器打开/下载文件,并且HTML / Javascript无法访问这些系统级程序。

You might need some Applet/Flash codes,which have access to communicate with system level. 您可能需要一些Applet / Flash代码,这些代码可以与系统级别进行通信。

It sounds like you are trying to get Excel to open on your client machine. 听起来您正在尝试使Excel在客户端计算机上打开。 As long as that is really what you are trying to do - then you would need to send the appropropriate mime type in your response header to let the browser know they type of they file is 'xls'. 只要这确实是您要尝试做的-那么您就需要在响应标头中发送适当的mime类型,以使浏览器知道它们的文件类型是“ xls”。

See this question: Setting mime type for excel document 看到这个问题: 为Excel文档设置mime类型

That also means that you would have to have a servlet manage the download rather than simply using an HTML anchor tag directly pointing to the file on the server. 这也意味着您将必须让servlet管理下载,而不是简单地使用直接指向服务器上文件的HTML锚标记。 Or, you would need to configure your server to send the appropriate mime type whenever a spreadsheet file is being sent to the browser. 或者,您需要将服务器配置为在将电子表格文件发送到浏览器时发送适当的mime类型。

Try putting this in your web.xml file: 尝试将其放在您的web.xml文件中:

<!-- Set Excel mime-mapping so spreadsheets open properly instead of being sent as an octet/stream -->
<!-- If this is not done, the user may be prompted to save the file, or it may open as garbage text in the browser --> 
<mime-mapping> 
    <extension>xls</extension> 
    <mime-type>application/vnd.ms-excel</mime-type> 
</mime-mapping>

(credit: http://wiki.metawerx.net/wiki/Web.xml.MimeMapping ) (信用: http : //wiki.metawerx.net/wiki/Web.xml.MimeMapping

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM