String content=“你好啊”;
byte b[] = content.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
/* * 关键地方
- 生成word格式*/
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument(“WordDocument”, bais);
//输出文件
String fileName=“文件名”;
response.reset();
request.setCharacterEncoding(“utf-8”);
response.setContentType(“application/ms-word;charset=UTF-8”);//导出word格式
response.addHeader(“Content-Disposition”, “attachment;filename=” +
new String( (fileName + “.doc”).getBytes(),
“iso-8859-1”));
OutputStream ostream = response.getOutputStream();
poifs.writeFilesystem(ostream);
ostream.flush();
ostream.close();
bais.close();
直接下载会有转码问题
将String content=“你好啊”;变为String content=“你好啊”;直接就可以导出 不需要转码 content如果是 富文本 直接 在前面后面加html标签就可以