用 Java 将网站放入新 Word 文件的编程方式

发布于 2024-07-20 13:01:09 字数 397 浏览 3 评论 0原文

是否可以通过编程方式将网页内容放入 Word 文件中?

为了使这个问题更加复杂,我想在 Java 中执行这些步骤(如果必须的话,使用 JNI)。

以下是我想要以编程方式执行的步骤,然后是我今天手动执行此操作的方法:

  1. 提供带有 URL 的方法(手动:在 Firefox 中打开页面)
  2. 复制该 URL 的内容 >(手动:Ctrl-A 选择全部)
  3. 创建一个新的 Word 文档(手动:打开 Microsoft Word)
  4. 将 URL 内容粘贴到 Word 中(手动:Ctrl- V 粘贴)
  5. 保存Word 文件(手动:保存Word 文件)

Is it possible to programmatically place the contents of a web page into a Word file?

To further complicate this, I'd like to do these steps in Java (using JNI if I must).

Here are the steps I want to do programmatically, followed by ways that I would do this manually today:

  1. Provide a method with a URL (Manually: Open page in Firefox)
  2. Copy the contents of that URL (Manually: Ctrl-A to select all)
  3. Create a new Word document (Manually: Open Microsoft Word)
  4. Paste the contents of the URL into Word (Manually: Ctrl-V to paste)
  5. Save the Word file (Manually: Save the Word file)

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

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

发布评论

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

评论(3

后来的我们 2024-07-27 13:01:09

恕我直言,您可以更好地使用 HTTP 下载文件,然后使用 Apache POI 并复制Word文件中的HTTP流

you could do better imho downloading the file using HTTP then create a new word file using Apache POI and copying the HTTP stream inside the word file

浮生面具三千个 2024-07-27 13:01:09

HTMLUnit 可用于以编程方式打开页面(如有必要,可伪装成 Firefox),并且 Apache POI 可用于创建 Microsoft Word 文件(Word 97 格式)。

HTMLUnit can be used to programmatically open the page (posing as Firefox if necessary), and Apache POI can be used to create a Microsoft Word file (in Word 97 format).

沉鱼一梦 2024-07-27 13:01:09

本文介绍了一种在 Java 中操作 MS-Word 文档文件的方法,只需使用字符串替换或 XSLT。

至于抓取 URL 的内容,这是任务中比较简单的部分,您可以通过非常简单的事情来完成。

import java.net.URL;
import java.net.URLConnection;
import java.io.InputStreamReader;
import java.io.BufferedReader;


public class util
{

  public String HttpGet(String urlString)
  {
    String resultData= null;
    try
    {
      URL url = new URL(urlString);
      URLConnection conn = url.openConnection();
      conn.connect();

      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String line = null;
      java.lang.StringBuffer sb1= new java.lang.StringBuffer();
      while ( (line = br.readLine()) != null)
        sb1.append(line);

      resultData= sb.toString();
      mStatus= "gotprice";

    } 
    catch (java.lang.Throwable e)
    {
      e.printStackTrace();
    }
    return resultData;
  }


}

This article describes a way to manipulate MS-Word doc files from within Java, just using string replace, or XSLT.

As for grabbing the content of a URL, that is the simpler part of the task, which you can accomplish with something pretty simple.

import java.net.URL;
import java.net.URLConnection;
import java.io.InputStreamReader;
import java.io.BufferedReader;


public class util
{

  public String HttpGet(String urlString)
  {
    String resultData= null;
    try
    {
      URL url = new URL(urlString);
      URLConnection conn = url.openConnection();
      conn.connect();

      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String line = null;
      java.lang.StringBuffer sb1= new java.lang.StringBuffer();
      while ( (line = br.readLine()) != null)
        sb1.append(line);

      resultData= sb.toString();
      mStatus= "gotprice";

    } 
    catch (java.lang.Throwable e)
    {
      e.printStackTrace();
    }
    return resultData;
  }


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