思路1:利用读取HTMl源码,然后生成wod文档,这样做对客户机器要求比较严格,实现工作中并不可取
思路2:将HTML源码传回到服务器,服务器负责生成word文档,客户机器再下载
这样做可行的
1.第一步:利用JS获取html源码
2.第二步:post源码到后台
3.第三步:利用poi生成word
4.配置文件下载
通过以上方式我们完成了html转换为word并下载的过程,相关技术可以网上搜搜即可
不足之处:html源码转换为word,我发现对于样式不解析,style=""这样的是可以的,总之,对样式的控制比较困难
如果技术达标,可以采用word编辑格式,转换为xml,再转换为freemakre模块大概这么样一个思路
想法变革
POI实际上是用来生成word的,实现上我们根本就不需要在后台生成word,而仅仅需要一个输入流即可
也就是把html源码转换为一个输入流即可下载了,而且下载后置于是word还是excel,关键在于对下载后
文件的命名,你写成XXX.word那下载后就是word文档,你写成XXX.xls,那下载后就是xls
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* 文件下载类
*
*/
public class DownLoadAction extends ActionSupport{
/**
* 下载文档,下载问价的访问入口方法
*
* @return resultName
*/
public String execute() {
// 返回 @Result 注解中的 name 值
return "success";
}
/**
* 获取下载读入流 必须要有方法
* 在 @Result 注解的参数 params 中, key 为 "inputName" 所对应的 value "inputStream";
* value "inputStream" ,决定该方法名为getInputStream;
* 如果 value 的值为 hello ,则该方法名为 getHello;
* @return InputStream
*/
public InputStream getInputStream() {
byte b[] = htmlSource.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
String exportfileName = "wqw"+(System.currentTimeMillis())+".doc";
try {
setFileName(new String(exportfileName.getBytes(), "iso8859-1") );
} catch (UnsupportedEncodingException e) {
LOG.error("文件下载后名称编码转换异常");
LOG.error("", e);
}
return bais;
}
/**
* 下载文件的名称,用于动态获取下载文件的名称(可选,即下载的文件名可以写死在@Result的params参数中,但是没有人会这么傻。)
* 在 @Result 注解的参数 params 中通过 ${fileName} 来获取该属性 fileName 的值;
* 和EL表达式的取值方式类似,呵呵;
*/
private String fileName;
public String getHtmlSource() {
return htmlSource;
}
public void setHtmlSource(String htmlSource) {
this.htmlSource = htmlSource;
}
private String htmlSource;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public static boolean writeWordFile() {
boolean w = false;
String path = "d:/";
try {
if (!"".equals(path)) {
// 检查目录是否存在
File fileDir = new File(path);
if (fileDir.exists()) {
// 生成临时文件名称
String fileName = "a.doc";
String content = "<html><div style=\"text-align: center\"><span style=\"font-size: 28px\"><span style=\"font-family: 黑体\">" +
"制度发布通知<br /> <br /> </span></span></div></html>";
byte b[] = content.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
FileOutputStream ostream = new FileOutputStream(path+ fileName);
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return w;
}
public static boolean writeWordFile2(String htmlSource) {
boolean w = false;
String path = "d:/";
try {
if (!"".equals(path)) {
// 检查目录是否存在
File fileDir = new File(path);
if (fileDir.exists()) {
// 生成临时文件名称
String fileName = "a.doc";
byte b[] = htmlSource.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
FileOutputStream ostream = new FileOutputStream(path+ fileName);
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return w;
}
public static void main(String[] args) {
if(writeWordFile()){
System.out.println("执行成功");
}else{
System.out.println("执行失败");
}
}
}