1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Java如何将html转换成word java poi html 转换word

Java如何将html转换成word java poi html 转换word

时间:2024-06-12 15:00:05

相关推荐

Java如何将html转换成word java poi html 转换word

推荐可以试用Free Spire.Doc for Java免费控件哦,它支持将html string和html file转word,代码操作简单,转换效果也很好。

Html file转wordimport com.spire.doc.*;

import com.spire.doc.documents.XHTMLValidationType;

public class htmlFileToWord {

public static void main(String[] args) {

String inputFile="data/InputHtmlFile.html";

String outputFile="output/htmlFileToWord.docx";

//open an html file.

Document document = new Document();

document.loadFromFile(inputFile, FileFormat.Html, XHTMLValidationType.None);

//save to a Word document.

document.saveToFile(outputFile, FileFormat.Docx);

}

}

Html string 转Wordimport com.spire.doc.*;

import java.io.*;

public class htmlStringToWord {

public static void main(String[] args) throws IOException {

String inputHtml = "data/InputHtml.txt";

String outputFile="output/htmlStringToWord.docx";

Document document = new Document();

//add a section.

Section sec = document.addSection();

String htmlText = readTextFromFile(inputHtml);

//add a paragraph and append html string.

sec.addParagraph().appendHTML(htmlText);

//save to a Word file.

document.saveToFile(outputFile, FileFormat.Docx);

}

public static String readTextFromFile(String fileName) throws IOException{

StringBuffer sb = new StringBuffer();

BufferedReader br = new BufferedReader(new FileReader(fileName));

String content = null;

while ((content = br.readLine()) != null) {

sb.append(content);

}

return sb.toString();

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。