現(xiàn)有的需求是前端導(dǎo)入word文件,然后需要在瀏覽器上展示出來,實(shí)現(xiàn)方案是將前端導(dǎo)入的word轉(zhuǎn)成html的形式,再輸出給前端,廢話不多說,直接上代碼.
需要用到的依賴
<!-- WordToHtml .doc .odcx poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>4.1.2</version> </dependency> <!-- 操作excel的庫 注意版本保持一致 poi poi-ooxml poi-scratchpad --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.poi.xwpf.converter.xhtml</artifactId> <version>2.0.2</version> </dependency>
?代碼實(shí)現(xiàn)
public static String word2007ToHtml(MultipartFile file) throws IOException {
String html = null;
if (file.isEmpty() || file.getSize() <= 0) {
log.error("Sorry File does not Exists!");
return null;
} else {
/* 判斷是否為docx文件 */
if (Objects.requireNonNull(file.getOriginalFilename()).endsWith(".docx") || file.getOriginalFilename().endsWith(".DOCX")) {
// 1)加載word文檔生成XWPFDocument對象
@Cleanup FileInputStream in = (FileInputStream) file.getInputStream();
XWPFDocument document = new XWPFDocument(in);
// 2)解析XHTML配置(這里設(shè)置IURIResolver來設(shè)置圖片存放的目錄)
XHTMLOptions options = XHTMLOptions.create();
//3.將word中圖片保存到oss中
options.setImageManager(SpringUtil.getBean(ImageManagerImpl.class));
options.setIgnoreStylesIfUnused(false);
options.setFragment(true);
// 3)將XWPFDocument轉(zhuǎn)換成XHTML
@Cleanup ByteArrayOutputStream baos = new ByteArrayOutputStream();
XHTMLConverter.getInstance().convert(document, baos, options);
html = new String(baos.toByteArray(), StandardCharsets.UTF_8);
System.out.println(html);
} else {
System.out.println("Enter only as MS Office 2007+ files");
throw new BizException("word文檔必須以.docx結(jié)尾");
}
}
return html;
}
注意事項(xiàng)
1.這個(gè)方法只支持docx結(jié)尾的文檔,doc文檔大同小異,如果有需要可以嘗試自己寫一下
2.和圖片上傳有關(guān)的這個(gè)類ImageManagerImpl,我就不寫出來了,涉及公司的一些東西,如果你要用,只需要寫個(gè)類繼承一下 ImageManager 這個(gè)類,然后重寫一下它的方法,也就是寫一下你圖片上傳邏輯之類的就行了,如:
private byte[] picture;
private String suffix;
private String fileName;
public ImageManagerImpl() {
super(new File(""), "");
}
@Override
public void extract(String imagePath, byte[] imageData){
this.fileName = imagePath.split("\\.")[0];
this.suffix = "." + imagePath.split("\\.")[1];
this.picture = imageData;
}
@SneakyThrows
@Override
public String resolve(String uri) {
@Cleanup InputStream inputStream = new ByteArrayInputStream(picture);
MultipartFile fileResult = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
return uploadFile(fileResult,UUID.randomUUID().toString().replaceAll("-", "")+suffix);
}
3.生成后的html這個(gè)時(shí)候不能直接給前端,因?yàn)橹苯咏o前端的話會在傳輸過程中格式發(fā)生一些詭異的變化,我這里處理的方法是將html給base64加密,然后讓前端去解析,這樣就沒問題了.
4. 其實(shí)這里生成的html都是一行行的div標(biāo)簽,并沒有頭和體的標(biāo)簽,雖然也能展示,但有些特殊用法的時(shí)候就不太行了,如html轉(zhuǎn)PDF的時(shí)候,這個(gè)時(shí)候要么在生成html的時(shí)候加上去,要么在轉(zhuǎn)PDF的時(shí)候加上去
htmlStr = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" + "<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">\n" + "\t<head></head><body style=\"font-family:SimSun;\">"+htmlDecode+"\t</body>\n" + "</html>";
注意這里是最后將html里面的字體全部統(tǒng)一成一種了,為了后期轉(zhuǎn)PDF方便文章來源:http://www.zghlxwxcb.cn/news/detail-610364.html
好了,暫時(shí)就這么多,后續(xù)有時(shí)間再補(bǔ)充文章來源地址http://www.zghlxwxcb.cn/news/detail-610364.html
到了這里,關(guān)于java實(shí)現(xiàn)word轉(zhuǎn)html的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!