gpt4 book ai didi

java - 有没有办法显示换行符,使用 HWPFDocument 的订单列表

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:26:32 25 4
gpt4 key购买 nike

我正在尝试使用 HWPFDocument 创建 word 文档。我能够创建具有某些功能的文档,但无法获得一些东西。我的问题很简单,但我无法弄清楚一些事情。我想将这个简单的 HTML 转换成创建的 worddoc:

<div xmlns="http://www.w3.org/1999/xhtml" class="formatted_content">
<strong>cloudHQ.tester.4</strong> –
this is the bold text
<br/>
this is italic text
<br/>
<ul>
<li>bullets 1</li>
<li>bullets 2</li>
<li>bullets 3</li>
</ul>
<br/>
<ol>
<li>Number1</li>
<li>Number2</li>
<li>Number3</li>
</ol>
<br/>
<pre>this is simple quote</pre>
<br>
this is simple quote
</div>

在这里我可以转换粗体和斜体文本。但无法弄清楚如何创建

1) <ul><li>....
2) <ol><li>...
3) break <br>
4) <pre>

标记到 WordDoc 中。

有没有这样的例子,请告诉我非常感谢您所做的努力,提前致谢。

已编辑:

包含的库:

  include_class "org.apache.poi.poifs.filesystem.POIFSFileSystem"
include_class "org.apache.poi.hwpf.usermodel.ParagraphProperties"
include_class "org.apache.poi.hwpf.usermodel.CharacterRun"
include_class "org.apache.poi.hwpf.usermodel.CharacterProperties"

这是将 html 转换为 doc 的主要代码:

 def convert_from_html_to_doc(html_file_name, comment_files)

puts("Script start.....")
puts("Parsing document comments start.....NEW")

default_file = "misc/poi_experiment/empty.doc"
fs = JavaPoi::POIFSFileSystem.new(JavaPoi::FileInputStream.new(default_file))

# Horrible word Document Format
hwpfDocument = JavaPoi::HWPFDocument.new(fs)

# range is used for getting the range of the document except header and footer
range = hwpfDocument.getRange()

par1 = range.insertAfter(JavaPoi::ParagraphProperties.new(), 0)
par1.setSpacingAfter(200);

puts("Adding given html content to doc.")
main_html = Nokogiri::HTML(File.read(html_file_name))
characterRun = par1.insertAfter(main_html.text)
# setting the font size
characterRun.setFontSize(2 * 12)

puts("Start procees on comment..... total : #{comment_files.size}")
comment_files.each do |cf|

file_path = "misc/poi_experiment/#{cf}"
puts("The comment file path : #{file_path}")

html = Nokogiri::HTML(File.read(file_path)).css('html')
puts( html )

par = characterRun.insertAfter(JavaPoi::ParagraphProperties.new(), 0)
par.setSpacingAfter(200);

#text = "<b><u>this is bold and underlined text</u></b>"
text = html.to_s.scan(/\D\d*/)
index = 0
currentCharacterRun , currentCharacterStyleList = [], []
character_arr = text.to_s.scan(/\D\d*/)

character_or_tag, index = get_next_character_or_tag(character_arr, index)

while !character_or_tag.nil?
if character_or_tag.is_char?
currentCharacterRun << character_or_tag.get_char
end
if character_or_tag.is_start_tag?
currentCharacterRunText = currentCharacterRun.join
if currentCharacterRunText != ""
characterproperties = JavaPoi::CharacterProperties.new
characterproperties = emit_to_document_and_apply_style(characterproperties, currentCharacterStyleList)
characterRun = par.insertAfter(currentCharacterRunText,characterproperties)
currentCharacterRun = []
end
currentCharacterStyleList << character_or_tag.get_tag
end
if character_or_tag.is_end_tag?
currentCharacterRunText = currentCharacterRun.join
if currentCharacterRunText != ""
characterproperties = JavaPoi::CharacterProperties.new
characterproperties = emit_to_document_and_apply_style(characterproperties, currentCharacterStyleList)
characterRun = par.insertAfter(currentCharacterRunText,characterproperties)
currentCharacterRun = []
end
currentCharacterStyleList.reject! { |x| x == character_or_tag.get_tag.gsub("/","") }
end

character_or_tag, index = get_next_character_or_tag(character_arr, index)
end
end

hwpfDocument.write(JavaPoi::FileOutputStream.new("#{html_file_name}.doc", true))
end

希望这有助于理解您。

最佳答案

经过大量尝试,我想继续使用 jod 转换器

关于java - 有没有办法显示换行符,使用 HWPFDocument 的订单列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10779528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com