Java 是否有任何方法可以使用HWPFDocument显示换行符、订单列表
我正在尝试使用HWPFDocument创建word文档。我能够创建具有某些功能的文档,但不能得到一些东西。我的问题很简单,但我不能想出几件事。 我想将这个简单的HTML转换为创建的worddoc:Java 是否有任何方法可以使用HWPFDocument显示换行符、订单列表,java,apache-poi,Java,Apache Poi,我正在尝试使用HWPFDocument创建word文档。我能够创建具有某些功能的文档,但不能得到一些东西。我的问题很简单,但我不能想出几件事。 我想将这个简单的HTML转换为创建的worddoc: <div xmlns="http://www.w3.org/1999/xhtml" class="formatted_content"> <strong>cloudHQ.tester.4</strong> – this is the bo
<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>
这是将html转换为文档的主要代码,如:
def将_从_html_转换为_doc(html_文件名、注释文件)
放置(“脚本开始…”)
puts(“解析文档注释开始…..新建”)
默认文件=“misc/poi\u实验/empty.doc”
fs=JavaPoi::POIFSFileSystem.new(JavaPoi::FileInputStream.new(默认_文件))
#可怕的word文档格式
hwpfDocument=JavaPoi::hwpfDocument.new(fs)
#范围用于获取除页眉和页脚之外的文档范围
range=hwpfDocument.getRange()
par1=range.insertAfter(JavaPoi::ParagraphProperties.new(),0)
par1.在(200)之后设置包装;
puts(“将给定html内容添加到文档中”)
main_html=Nokogiri::html(File.read(html_文件名))
characterRun=par1.insertAfter(main_html.text)
#设置字体大小
characterRun.setFontSize(2*12)
puts(“在注释上启动进程…..总计:#{comment_files.size}”)
注释_files.each do | cf|
file_path=“misc/poi_experience/#{cf}”
puts(“注释文件路径:#{file_path}”)
html=Nokogiri::html(File.read(File_path)).css('html'))
puts(html)
PAR= ListRun.Inter TaFter(JavaPoi::SimultField.New()),0)
设定间隔(200);
#text=“这是粗体加下划线的文本”
text=html.to_.s.scan(/\D\D*/)
索引=0
currentCharacterRun,currentCharacterStyleList=[],[]
character\u arr=text.to\u.s.scan(/\D\D*/)
字符或标记,索引=获取下一个字符或标记(字符arr,索引)
虽然字符\u或\u tag.nil?
如果字符或标签是字符?
currentCharacterRun本节内容可能对您有用。例如,要创建列表,我想您需要使用
此类用于在Word文档中创建列表。它与HWPF文档中的registerList一起使用。在Word中,列表不是范围内的实体,这意味着您实际上无法将一个实体添加到文档中。列表仅用作列表项的属性。注册列表后,可以将列表项添加到作为列表一部分的文档中。我看到的唯一好处是,您可以在文档中的任何位置添加一个列表条目,并从上一个列表继续编号
在java中,您可以这样做:
我不是ruby专家,所以我将把JRuby的翻译留给你
这给了你列表中的#1和#2,以及#3和#4是我认为的段落的样式化版本。经过多次尝试后,我想继续jod converter你能展示一下如何将html转换为文档吗?或者您正在尝试编写这样的转换器?我正在尝试使用ApachePOI和jRuby进行转换。请查看我的编辑部分。尼基塔:你有什么解决方案吗?Alfresco使用该工具生成缩略图或页面预览,这要归功于OpenOffice服务(服务器在后台运行)。仅用于转换目的的良好选择。POI首先以文档构造或修改为目标。
1) <ul><li>....
2) <ol><li>...
3) break <br>
4) <pre>
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"
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
new HWPFList(boolean numbered, StyleSheet styleSheet)