ueditor如何实现word文档的导入和下载功能?
答案:4 悬赏:0 手机版
解决时间 2021-11-28 07:49
- 提问者网友:流星是天使的眼泪
- 2021-11-27 13:08
ueditor如何实现word文档的导入和下载功能?
最佳答案
- 五星知识达人网友:枭雄戏美人
- 2021-11-27 13:49
ueditor实现word文档的导入和下载功能的方法:
1、UEditor没有提供word的导入功能,只能说是粘贴复制。
2、方案:用poi来提供word导入,思路是将word转换为html输出,再用UEditor提供的setContent()方法将html的内容添加到编辑器中。
方案缺点,一是poi对word文本的格式获取必须按setContent()可接受的方式进行;二是我暂时没发现poi可以提供获取段落格式(对齐方式、缩进量)的方法。
代码如下:package demo;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.w3c.dom.Document;
public class Word2Html {
public static void main(String argv[]) {
String htmlContent = new String();
try {
htmlContent = convert2Html("D://test//1.doc","D://test//1.html");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(htmlContent);
}
public static void writeFile(String content, String path) {
FileOutputStream fos = null;
BufferedWriter bw = null;
try {
File file = new File(path);
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
bw.write(content);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fos != null)
fos.close();
} catch (IOException ie) {
}
}
}
public static String convert2Html(String fileName, String outPutFile)
throws TransformerException, IOException, ParserConfigurationException {
HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName)); //WordToHtmlUtils.loadDoc(new FileInputStream(inputFile));
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
wordToHtmlConverter.setPicturesManager(new PicturesManager()
{
public String savePicture( byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches )
{
return "D:/test/"+suggestedName;
}
} );
wordToHtmlConverter.processDocument(wordDocument);
//save pictures
List pics = wordDocument.getPicturesTable().getAllPictures();
if( pics!=null ){
for(int i=0; i Picture pic = (Picture)pics.get(i);
System.out.println();
try {
pic.writeImageContent(new FileOutputStream("D:/test/" + pic.suggestFullFileName()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
out.close();
writeFile(new String(out.toByteArray()), outPutFile);
return new String(out.toByteArray());
}
}
1、UEditor没有提供word的导入功能,只能说是粘贴复制。
2、方案:用poi来提供word导入,思路是将word转换为html输出,再用UEditor提供的setContent()方法将html的内容添加到编辑器中。
方案缺点,一是poi对word文本的格式获取必须按setContent()可接受的方式进行;二是我暂时没发现poi可以提供获取段落格式(对齐方式、缩进量)的方法。
代码如下:package demo;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.w3c.dom.Document;
public class Word2Html {
public static void main(String argv[]) {
String htmlContent = new String();
try {
htmlContent = convert2Html("D://test//1.doc","D://test//1.html");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(htmlContent);
}
public static void writeFile(String content, String path) {
FileOutputStream fos = null;
BufferedWriter bw = null;
try {
File file = new File(path);
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
bw.write(content);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fos != null)
fos.close();
} catch (IOException ie) {
}
}
}
public static String convert2Html(String fileName, String outPutFile)
throws TransformerException, IOException, ParserConfigurationException {
HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName)); //WordToHtmlUtils.loadDoc(new FileInputStream(inputFile));
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
wordToHtmlConverter.setPicturesManager(new PicturesManager()
{
public String savePicture( byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches )
{
return "D:/test/"+suggestedName;
}
} );
wordToHtmlConverter.processDocument(wordDocument);
//save pictures
List pics = wordDocument.getPicturesTable().getAllPictures();
if( pics!=null ){
for(int i=0; i
System.out.println();
try {
pic.writeImageContent(new FileOutputStream("D:/test/" + pic.suggestFullFileName()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
out.close();
writeFile(new String(out.toByteArray()), outPutFile);
return new String(out.toByteArray());
}
}
全部回答
- 1楼网友:一把行者刀
- 2021-11-27 16:40
1、选中表格;
2、选择表格工具布局选项卡,在对齐方式处,根据需要选择一种即可,如图所示。
- 2楼网友:舍身薄凉客
- 2021-11-27 15:55
这个编辑器有几个函数,可以在编辑器加载完后给编辑器赋值。具体你搜一下帮助文档。
ueditor_1_2_0-gbk完整包里面有个_examples文件夹,看哈帮助吧,里面有获得内容、写入内容、判断是否有内容等等函数都有,而且还有示例。
editor.getContent()
editor.setContent()追问这只是对编辑器进行的get和set,并不能实现Word文档的导入呢。
ueditor_1_2_0-gbk完整包里面有个_examples文件夹,看哈帮助吧,里面有获得内容、写入内容、判断是否有内容等等函数都有,而且还有示例。
editor.getContent()
editor.setContent()追问这只是对编辑器进行的get和set,并不能实现Word文档的导入呢。
- 3楼网友:酒安江南
- 2021-11-27 15:09
UEditor没有提供word的导入功能,只能说是粘贴复制。
另外的方案:用poi来提供word导入,思路是将word转换为html输出,再用UEditor提供的setContent()方法将html的内容添加到编辑器中。这个方案有两个缺点,一是poi对word文本的格式获取必须按setContent()可接受的方式进行;二是我暂时没发现poi可以提供获取段落格式(对齐方式、缩进量)的方法。
可以参考以下代码,这是从网上找的(不记得网页地址了,谢谢代码原主提供)
package demo;import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.w3c.dom.Document;
public class Word2Html {
public static void main(String argv[]) {
String htmlContent = new String();
try {
htmlContent = convert2Html("D://test//1.doc","D://test//1.html");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(htmlContent);
}
public static void writeFile(String content, String path) {
FileOutputStream fos = null;
BufferedWriter bw = null;
try {
File file = new File(path);
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
bw.write(content);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fos != null)
fos.close();
} catch (IOException ie) {
}
}
}
public static String convert2Html(String fileName, String outPutFile)
throws TransformerException, IOException, ParserConfigurationException {
HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName)); //WordToHtmlUtils.loadDoc(new FileInputStream(inputFile));
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
wordToHtmlConverter.setPicturesManager(new PicturesManager()
{
public String savePicture( byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches )
{
return "D:/test/"+suggestedName;
}
} );
wordToHtmlConverter.processDocument(wordDocument);
//save pictures
List pics = wordDocument.getPicturesTable().getAllPictures();
if( pics!=null ){
for(int i=0; i
System.out.println();
try {
pic.writeImageContent(new FileOutputStream("D:/test/" + pic.suggestFullFileName()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
out.close();
writeFile(new String(out.toByteArray()), outPutFile);
return new String(out.toByteArray());
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯