可以使用 Qt 输出为 .doc 格式吗?

发布于 2024-08-20 01:08:06 字数 171 浏览 3 评论 0原文

我正在使用 Qt 编写程序。我希望它输出到 .doc 以保留格式,但 Qt 支持的所有格式都是纯文本、ODF 和 HTML 格式。 ODF 和 HTML 将保留格式,但随后我必须将其复制并粘贴到 .doc 文件中。

我希望能够保存 .doc 文件,而不必担心这样做。我已经用谷歌搜索了很多次,但没有找到解决方案。

I am writing a program using Qt. I want it to output to .doc to preserve formatting, but all that is supported by Qt are plain text, ODF, and HTML formats. ODF and HTML will preserve the formatting, but then I would have to copy and paste this to a .doc file.

I want to be able to save a .doc file and not have to worry about doing this. I have Googled this many times, but I haven't found a solution.

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

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

发布评论

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

评论(4

望喜 2024-08-27 01:08:07

创建有效的 .doc 文件的唯一方法是使用 Microsoft Office。它可以通过 OLE 自动化从您的程序中完成(更多关于该问题的信息,请参见此处 - 为什么 Microsoft Office 文件格式如此复杂?(以及一些解决方法))。

Qt 提供了用于处理 COM 对象的 ActiveQt 框架。因此,您可以加载 Word 并使用其 COM 接口将 .html 转换为 .doc。当然,必须安装MS Office。

The only way to create a valid .doc file is to use Microsoft Office. It can be done from your program with OLE Automation (more on that problem here - Why are the Microsoft Office file formats so complicated? (And some workarounds)).

Qt provides the ActiveQt framework for working with COM objects. So you can load Word and convert .html to .doc using its COM interface. Of course, MS Office must be installed.

夢归不見 2024-08-27 01:08:07

当您说“.doc”格式时,我假设您指的是 Microsoft Word 使用的“Word 97-2003 文档”格式。 Microsoft Office 可以很好地打开 .html 文件,因此您可以走这条路。如果 Word 安装在运行该程序的计算机上,则可以将数据复制到剪贴板并使用自动化功能让 Word 粘贴数据并将其保存为 .doc 格式。

您具体使用什么格式?

When you say ".doc" format I will assume you mean the "Word 97-2003 Document" format used by Microsoft Word. Microsoft Office can open .html files just fine, so you could go that route. If Word is installed on the some computer that you're running the program on, you could copy the data to the clipboard and use automation to have Word paste it and save it in .doc format.

What formatting are you using specifically?

泪痕残 2024-08-27 01:08:07

正如您所说,QTextDocumentWriter 仅支持纯文本、ODF 和 HTML 格式。

如果您只需要在 Windows 上阅读文档,则可以生成 PDF,而不是尝试生成 .doc:

 QPrinter printer;
 QTextDocument document;

 //TODO: Put stuff to print in your 'document'

 printer.setOutputFormat(QPrinter::PdfFormat);
 printer.setOutputFileName("/foobar/nonwritable.pdf");

 document.print( printer );

另一种可能的解决方案,如果您必须在 Word 中修改文档,请尝试 Word 2003 的 ODF 插件 用于打开 ODF 文件。

As you stated, QTextDocumentWriter support only plain text, ODF and HTML formats.

If you need to read only your document on Windows, you can produce a PDF instead of trying to produce a .doc:

 QPrinter printer;
 QTextDocument document;

 //TODO: Put stuff to print in your 'document'

 printer.setOutputFormat(QPrinter::PdfFormat);
 printer.setOutputFileName("/foobar/nonwritable.pdf");

 document.print( printer );

Another possible solution, if you have absolutely to modify your document in Word, try an ODF add-on for Word 2003 to open your ODF file.

攒一口袋星星 2024-08-27 01:08:07

还有另一种选择......实际上你可以称之为黑客攻击,但这是一种简单的方法,因为我自己找到了它。

  1. 创建一个看起来像您的文档的 HTML 模板
  2. 使用 Microsoft Word 打开 HTML 模板
  3. 调整模板(有些内容可能会被误调整)
  4. 在您想要写入一些数据/文本的地方,放置
    某种标记,例如#offaddress#
  5. 保存文档
  6. 将文档重命名回 HTML
  7. 打开文档。该文档将包含大量 Microsoft Word 文档代码和内容;不用担心。
  8. 复制整个代码并将其粘贴到 Qt
  9. 将输出设置为 Qt 作为 .doc 文件
  10. 将关键字 (#offaddress#) 替换为您的内容
  11. 将内容保存到扩展名为 .doc 的文件

Voilà;您有 .doc 文件。

There is another alternative to this... Practically you can call it hacking, but it is kind of a simple way as I find it for myself.

  1. Create a HTML template that looks like your document
  2. Open the HTML template using Microsoft Word
  3. Adjust the template (some things might be misadjusted)
  4. In the places where you would like to write some data/text, put
    some kind of marker, e.g. #offaddress#
  5. Save the document
  6. Rename the document back to HTML
  7. Open the document. The document will contain a lot of Microsoft Word document codes and stuff; don't worry.
  8. Copy the entire code and paste it in your Qt
  9. Set output to your Qt as .doc file
  10. Replace your keywords (#offaddress#) with your content
  11. Save the content to file with extension .doc

Voilà; you have your .doc file.

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