java poi html 转换word

superman369
  • 45

POIFSFileSystem poifs = new POIFSFileSystem();

DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); // 第一个参数是怎么来的??
回复
阅读 2.8k
1 个回答

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

Html file转word

import 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 转Word

import 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();
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏