「Java開発」Word文書をHTMLに変換するプログラム
サンプルコード
@Test
public void docxToHtml()
throws Exception
{
String sourceFileName = “D:\\test\\cft.docx";
String targetFileName = “E:\\changfa\\test.html";
String imagePathStr = “E:\\test\\img\\";
OutputStreamWriter outputStreamWriter = null;
try
{
XWPFDocument document = new XWPFDocument(new FileInputStream(sourceFileName));
XHTMLOptions options = XHTMLOptions.create();
// ディレクトリに画像を保存
options.setExtractor(new FileImageExtractor(new File(imagePathStr)));
// htmlの画像のパス
options.URIResolver(new BasicURIResolver(“img"));
outputStreamWriter = new OutputStreamWriter(new FileOutputStream(targetFileName),
“utf-8");
XHTMLConverter xhtmlConverter = (XHTMLConverter)XHTMLConverter.getInstance();
xhtmlConverter.convert(document, outputStreamWriter, options);
}
finally
{
if (outputStreamWriter != null)
{
outputStreamWriter.close();
}
}
}