1. jsp页面部分内容导出生成word文档
jsp页面导出为word文件需要利用apache的POI来完成。
核心代码如下:
<%@ page contentType="application/msword; charset=utf-8" %>
其实如果用框架做就方便多了,比如Struts2。在Action里直接写如下代码:
if(out!=null){
String fileName="";
fileName+="评价报告.doc";
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Content-disposition","attachment; filename="+new String(fileName.getBytes("GB2312"), "8859_1"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
out是jsp页面表单元素,一个button,用于提交表单到相应Action进行Word下载。Action设置jsp页面头文件。这样每次点击button就可以把相应jsp页面的内容保存到Word中并且支持下载,Word中内容并且是可编辑状态。
2. jsp导出到word
jsp输出word 在页面直接打开word。
在Action中写 response.reset(); response.setContentType("application/msword;charset=utf-8"); response.setHeader("Content-Disposition", "inline;filename=temp.doc");response.getOutputStream().write(document.getContent()); response.getOutputStream().flush(); response.getOutputStream().close();return null;在页面时下载word。 在Action中写 response.reset(); response.setContentType("application/x-download;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=temp.doc");response.getOutputStream().write(document.getContent()); response.getOutputStream().flush(); response.getOutputStream().close();return null;。
3. 怎么实现将jsp页面内容输出为word文档
JSP页面显示的内容导出到WORD文档:<%response.setContentType("application/msword;charset=utf-8"); response.setHeader("Content-Disposition","attachment;filename=test.doc"); //用word打开页面%>
8888 |
4. 怎样设置jsp生成word文档以页面视图来显示
1-apache的POI,此方法对Excel的导出做的很好,目前对Word的导出方面的功能尚未完全。
2-纯JavaScript脚本实现。主要通过客户端调用本机Office组件来实现。
3-在JSP页面引入头文件实现。 纯JavaScript脚本实现细节方面大体是创建一个word组件ActiveXObject('Word.Application'),用js通过表ID取得表内容然后保存到word,要注意的是js实现有很多不好的地方,例如Internet选项需要把ActiveX空间全部启用,安全级别设置为中。
这样的话岂不是每台机器都要配置一下。其次每次生成word文档以后弹出对话框(无法保存此文件,因为它已在别处打开(C:\。
\STARTUP\Powerword.dot)),出现此问题就需要把C:\Documents and Settings\当前用户名\Application Data\Microsoft\Word\STARTUP下的Powerword.dot文件删除,每次遇到此问题就需要删除文件来解决,十分不方便。 JSP页面引入来实现Word保存就方便多了,但是也有不足的地方,首先如果需要引入
在Action里直接写如下代码: if(out!=null){ String fileName=""; fileName+="评价报告.doc";try {HttpServletResponse response = ServletActionContext.getResponse(); response.setHeader("Content-disposition","attachment; filename="+new String(fileName.getBytes("GB2312"), "8859_1")); } catch (UnsupportedEncodingException e) { e.printStackTrace();}out是jsp页面表单元素,一个button,用于提交表单到相应Action进行Word下载。Action设置jsp页面头文件。
这样每次点击button就可以把相应jsp页面的内容保存到Word中并且支持下载,Word中内容并且是可编辑状态。 不足的地方在于由于表内容是动态生成,有的需要先查看在下载Word,就需要另外建立一个新JSP页面进行Word下载,当然首先要在struts.xml里配置好页面转向。
新建立的页面传值同查看页面要保持一样。
5. 如何把JSP中的内容转化为WORD文档形式
可以利用table把试卷输出,然后利用JavaScript保存此表格为Word文档。
//下面代码为引用论坛其他人的回复7a686964616fe59b9ee7ad9431333335343330,自己没测试过
App为表格ID,你调用一下SaveAs函数.
function PrintFile()
{
var strResult=window.confirm("确认用Word打印吗?");
if(strResult)
{
try
{
App.focus();
document.execCommand("SelectAll");
document.execCommand("Copy");
App.focus();
var WordApp=new ActiveXObject("Word.Application");
WordApp.Application.Visible=true;
var Doc=WordApp.Documents.Add();
Doc.Activate();
Doc.Content.Paste();
Doc.PrintPreview();
WordApp.DisplayAlerts=false;
Doc.Close();
WordApp.DisplayAlerts=true;
WordApp.Quit();
}
catch(e){}
}
else
{
var hwnd=window.open("");
hwnd.document.write(App.innerHTML);
}
return false;
}
6. 怎样将jsp(含图片)导出word文档,图片也要导出
<script type="text/javascript">
function copyTable(){
//tabel ID, 会将table内所有的内//容复制到word
var elTable = document.getElementById("tableid");
var oRangeRef = document.body.createTextRange();
oRangeRef.moveToElementText( elTable );
oRangeRef.execCommand( "Copy" );
var appWord = new ActiveXObject( "Word.Application" );
appWord.Application.Visible = true;
var mydoc=appWord.Documents.Add('',0,1);
myRange =mydoc.Range(0,1);
myRange.Paste();
appWord.ActiveWindow.ActivePane.View.Type=9;
appWord = null;
}
</script>
修改里面的 tableid 换成你要保存的table ID
7. jsp导出到word
jsp输出word
在页面直接打开word。
在Action中写
response.reset();
response.setContentType("application/msword;charset=utf-8");
response.setHeader("Content-Disposition", "inline;filename=temp.doc");
response.getOutputStream().write(document.getContent());
response.getOutputStream().flush();
response.getOutputStream().close();
return null;
在页面时下载word。
在Action中写
response.reset();
response.setContentType("application/x-download;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=temp.doc");
response.getOutputStream().write(document.getContent());
response.getOutputStream().flush();
response.getOutputStream().close();
return null;
转载请注明出处51数据库 » jsp导出word文档