天天看点

html转为word文档分页,html转word文档

packagecn.com.szhtkj.util;importjava.io.ByteArrayInputStream;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importorg.apache.poi.poifs.filesystem.DirectoryEntry;importorg.apache.poi.poifs.filesystem.DocumentEntry;importorg.apache.poi.poifs.filesystem.POIFSFileSystem;public classtestMain {public static booleanwriteWordFile() {boolean w = false;

String path= "d:/";try{if (!"".equals(path)) {//检查目录是否存在

File fileDir = newFile(path);if(fileDir.exists()) {//生成临时文件名称

String fileName = "a11.doc";//使用传html 转word

String contentConcat = "" + "" + "

信息" + ""

+ "

姓名

+ "

性别" + "" + ""

+ "" + "";byte b[] = contentConcat.getBytes("UTF-8");

ByteArrayInputStream bais= newByteArrayInputStream(b);

POIFSFileSystem poifs= newPOIFSFileSystem();

DirectoryEntry directory=poifs.getRoot();

DocumentEntry documentEntry= directory.createDocument("WordDocument", bais);

FileOutputStream ostream= new FileOutputStream(path +fileName);

poifs.writeFilesystem(ostream);

ostream.close();

poifs.close();

bais.close();}

}

}catch(IOException e) {

e.printStackTrace();

}returnw;

}public static voidmain(String[] args) {

writeWordFile();

}

}