package com.eluotuo.common.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* 强html转换为doc
* @author wanglei
* @version [版本号, 2016年6月1日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class HtmlToDoc
{
public static void main(String[] args)
throws Exception
{
// TODO Auto-generated method stub
byte[] bytes =
writeWordFile("
hello
FileUtils.writeByteArrayToFile(new File("d:/1.doc"), bytes);
}
/**
* 读取html文件到word
*
* @param html html文件的路径
* @return word的二进制
* @throws Exception
*/
public static byte[] writeWordFile(String html)
throws Exception
{
ByteArrayInputStream bais = null;
FileOutputStream fos = null;
ByteArrayOutputStream bos = null;
try
{
byte b[] = html.getBytes("gbk");
bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
bos = new ByteArrayOutputStream();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
poifs.writeFilesystem(bos);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (fos != null)
{
fos.close();
}
if (bais != null)
{
bais.close();
}
if (bos != null)
{
bos.close();
}
}
return bos.toByteArray();
}
}PS 上面的解决方案只能在微软word中打开 在 wps中打开会出问题