java POI HTML turn Word

1, reference jar package

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>4.1.0</version>
</dependency>

2, the core code

String html = "<div> Test contents </ div";

POIFSFileSystem poifs = null;
FileOutputStream ostream = null;
ByteArrayInputStream bais = null;
String uuid = "测试.doc";
File file = null;

{the try
  // the HTML content must be <html> <body> </ body> </ html> Packaging
  fileParam.setcContent ( "<html> <body >" + html + "</ body> </ html>");
  byte [] B = fileParam.getcContent () the getBytes ();.
  Bais new new = A ByteArrayInputStream (B);
  POIFS new new POIFSFileSystem = ();
  the DirectoryEntry poifs.getRoot Directory = ();
  // can not modify the name WordDocument
  directory.createDocument ( "WordDocument", Bais);
  the ostream new new = a FileOutputStream (UUID);
  poifs.writeFilesystem (the ostream); // current directory generates a test of .doc
} the catch (Exception E) {
  logger.error ( "Exception iS } { ", E);
} {the finally
  IOUtils.closeQuietly (POIFS);
  IOUtils.closeQuietly (the ostream);
  IOUtils.closeQuietly (bais);
  try {
    FileUtils.forceDelete(file);
  } catch (Exception e2) {
  }
}

Guess you like

Origin www.cnblogs.com/jiehanshi/p/11121782.html