jsp简单导出word,不使用poi

1..jsp页面

<body>
<div id="print">
<div align="center"><span class="STYLE1" id="doc_title">组织处理决定表</span></div>
<input id="state" type="hidden" value="3"/>
<input id="treatingTableid" type="hidden" value="${structure_TreatingTable.id}"/>
<p class="time">
<span id="username" style="float:left;">填写人:${structure_TreatingTable.username}</span>
<span id="time" style="float:right;">填写时间:${structure_TreatingTable.INPUT_TIME}</span>
</p>
<table width="1000" border="1" cellspacing="0" align="center">
    <tr height="500px">
   <td><span id="content">${structure_TreatingTable.content2}</span></td>
</tr>
</table>
</div>
<div align="center">
<button id="dcbtn" οnclick="LOOKTABLE.exportdcbg()" class="btn btn-info" type="button">导出</button>
</div>
</body>

2.js传值

(function($){
$.EXPORT = function(){
this.EXPORT_HTML = function(descode,fileName){
var test1=$("#print").html();
alert(test1);
$.ajax({
type:'post',
url: path+'/resultReportedController/exportword',
data:{"html": test1,"id":descode,"fileName":fileName},
dataType:'json',
success: function(data){
if(data = 1){
layer.msg("导出成功");
}
}
});
};
}
})(jQuery);
var wordexport = new jQuery.EXPORT();

3.Controller

@RequestMapping("/exportword")
@ResponseBody
public Integer exportdisposetable(String id,String html,String fileName) {
try {
ExportDocTest edt = new ExportDocTest();
edt.createOutWord(id, html,fileName);
return 1;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}


public void createOutWord(String descode,String html,String fileName) throws IOException {
File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
String desktopPath = desktopDir.getAbsolutePath(); //得到桌面路径
       try {  
           if (!"".equals(desktopPath)) {  
               // 检查目录是否存在  
               File fileDir = new File(desktopPath);  
               if (fileDir.exists()) {  
                   // 生成临时文件名称   
                   String content = "<html><body><div style=\"text-align: center\">"+html+"</div></body></html>"; //一个完整的html
                   byte b[] = content.getBytes();  
                   ByteArrayInputStream bais = new ByteArrayInputStream(b);  
                   POIFSFileSystem poifs = new POIFSFileSystem();  
                   DirectoryEntry directory = poifs.getRoot();  
                   DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); //把html的内容,转换为word格式
                   FileOutputStream ostream = new FileOutputStream(desktopPath + "\\" + fileName+ "_"+descode+".doc");  
                   poifs.writeFilesystem(ostream);  
                   bais.close();  
                   ostream.close();  
               }  
           }  
       } catch (IOException e) {  
           e.printStackTrace();  
     }  
}

 

以上步奏简单的完成word的导出,流程是这样的:

前台把页面内容的HTML代码以字符串的形式传给后台,我们在后台把html以字节流方式读出,读出格式为word格式,此word内容便和你页面的样式一样,不需要再为格式烦恼

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值