如何在JSP页面中打印Word文档而不打开它?

发布于 2024-07-07 10:24:42 字数 136 浏览 7 评论 0原文

我有一个基于 J2EE 的 Web 应用程序,在其中单击按钮时,我需要从 Java 创建一个 Word 文件。 我希望能够将打印命令发送到该文件,以便打印该文件,而无需用户打开文档并手动执行。

谁能告诉我这是否可能,如果可以,如何进行?

I have a J2EE based web application, in which on clicking a button I need to create a word file from Java. I want to be able to sent the printing command to this file, so that the file is being printed without the user having to open the document and do it manually.

Could anyone please tell me if this is possible and if so how to proceed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

水染的天色ゝ 2024-07-14 10:24:42

生成文件很容易。 查看 Apache POI,这是一个用于访问 Microsoft 格式文件的 Java API。 该网站有很多例子。

至于打印它,我真的不知道这是否可能。 我认为你总是需要在客户端打开它才能打印它。

It is easy to generate the file. Take a look at Apache POI, a Java API To Access Microsoft Format Files. The site is plenty of examples.

About printing it, I don't really know if that's possible. I think you always need to open it in the client side in order to print it.

寂寞陪衬 2024-07-14 10:24:42

您必须创建一个 ActiveX 来加载文档内容并触发打印命令。 没有其他方法可以在客户端计算机中打开文件并触发命令。

编辑:
如果您可以使用 HTML,则只需在弹出窗口上执行

<script>
function load() {
window.print();
window.close();
}
</script>

<body onLoad="load()" ...>

即可打开文档并打印。 然后关闭弹出窗口。

You have to create an ActiveX to load the doc content and fire the print command. There is no other way to open a file in the client computer and fire comands.

EDIT:
If you can use HTML, you can just do

<script>
function load() {
window.print();
window.close();
}
</script>

and

<body onLoad="load()" ...>

on a popup window to open the document and print it. And then close the popup.

↙厌世 2024-07-14 10:24:42

您可以使用仅 2 行的简单 AutoIt 代码。 并在程序中执行该代码文件来完成工作。

http://www.autoitscript.com/site/autoit/

喜欢这段代码:

$sRTFfilename = "C:\t\t.rtf" ;Change this path to one of your own 
ShellExecute('"' & $sRTFfilename & '"', "", @ScriptDir, "print", @SW_HIDE)

;-)

You can use simple AutoIt code of just 2 lines. And get your work done execute that code file in your program.

http://www.autoitscript.com/site/autoit/

Like this code:

$sRTFfilename = "C:\t\t.rtf" ;Change this path to one of your own 
ShellExecute('"' & $sRTFfilename & '"', "", @ScriptDir, "print", @SW_HIDE)

;-)

我要还你自由 2024-07-14 10:24:42

当您特别提到 Java 作为您的环境时,您可能需要考虑 OpenOffice 或 StarOffice - 它们的 API 比 ActiveX 或远程控制 Word 更容易通过 Java 进行通信。

它并不比 Word 更重量级。 参考资料和文档应该很容易找到。

我曾在一家公司工作过,我们经常使用远程控制 Word,最后改用 StarOffice。 这不是用于打印而是用于文档转换(例如从Word 到HTML),但应该足够相似。

当然,我说的是“旧版本的 Word”,但我们通常会遇到 Word 锁定的问题,并通过某些任意对话框请求确认 Word 发现的任何内容 - 显然我们的服务器(在后台运行,没有桌面接触)的操作没能做到。 改用 StarSuite 后,情况好多了。

As you are specifically mentioning Java as your environment you might want to take OpenOffice or StarOffice into account - they have an API that's a lot easier to talk to from Java than ActiveX or remote-controlling Word.

It's no more heavyweight than Word is. References and docs should be easy to find.

I've worked in a company where we used Remote-controlled-Word a lot and finally switched to StarOffice. This wasn't for printing but for document conversion (e.g. from Word to HTML), but should be sufficiently similar.

Of course I'm talking "old versions of Word" but we usually had the problem of Word locking up with some arbitrary dialog requesting to confirm whatever Word has found - an operation that our server (running in the background with no Desktop contact) obviously was not able to. This went a lot better after switching to StarOffice.

霊感 2024-07-14 10:24:42

是否可以改为以 HTML 格式呈现文档? 如果您能做到这一点,那么您就可以相当简单地允许用户通过浏览器进行打印。 您也可能会更轻松地使用 PDF,至少可以更轻松地跨不同平台访问它。

如果您被迫使用 MS Word,那么您能做的事情将会非常有限。 正如莱昂内尔提到过,我认为 ActiveX 将是您唯一的选择,即使如此,也必须打开文档,您也许能够自动从浏览器启动 Word。 您甚至可以通过 ActiveX 将 Word 实例嵌入到 IE 中,但我对此并不能 100% 确定。

Would it be possible to render the document in HTML instead? If you could do that then you could allow the users to print via the browser fairly simply. You might also have an easier time with PDF's, at the very least it would be more accessible across different platforms.

If you're forced to use MS Word then you're going to be very limited in what you can do. As Leonel mentioned, I think ActiveX is going to be your only choice and even then the document would have to be opened, you just might be able to launch Word from the browser automatically. You might even be able to embed an instance of Word into IE via ActiveX, but I'm not 100% sure about that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文