下面我总结一下导出word文件和excel文件的2种办法。
一. 方法一
1.开始操作的页面
2.处理输出的页面
3.要简单的内容页(省略)
二.方法二
一. 方法一
1.开始操作的页面
<%@ page contentType="text/html;charset=GBK" %>
<html>
<head>
<title>导出测试</title>
<script language="javascript">
function unload_word(url)
{
var url = url;
alert(url);
location.href='/unload/unload_word.jsp?url='+url;
}
function unload_excel(url)
{
var url = url;
alert(url);
location.href='/unload/unload_excel.jsp?url='+url;
}
</script>
</head>
<body>
<input type="button" value="导出为Word文档" name="download" onclick="unload_word('http://172.18.3.88:9001/unload/data_show.jsp')" >
<br>
<input type="button" value="导出为Excel文档" name="download" onclick="unload_excel('http://172.18.3.88:9001/unload/data_show.jsp')">
</body>
</html>
说明:
http://172.18.3.88:9001/unload/data_show.jsp
这个是要导出页面的地址,你可以自己修改,但记住不能少了http这个前缀,地址要完整。
<html>
<head>
<title>导出测试</title>
<script language="javascript">
function unload_word(url)
{
var url = url;
alert(url);
location.href='/unload/unload_word.jsp?url='+url;
}
function unload_excel(url)
{
var url = url;
alert(url);
location.href='/unload/unload_excel.jsp?url='+url;
}
</script>
</head>
<body>
<input type="button" value="导出为Word文档" name="download" onclick="unload_word('http://172.18.3.88:9001/unload/data_show.jsp')" >
<br>
<input type="button" value="导出为Excel文档" name="download" onclick="unload_excel('http://172.18.3.88:9001/unload/data_show.jsp')">
</body>
</html>
2.处理输出的页面
<%@ page contentType="application/vnd.ms-word; charset=GBK"%>
<%@ page import="java.net.URL"%>
<%
response.setHeader("Content-disposition","attachment; filename=print_tmp.doc");
%>
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title></title>
</head>
<%
String url_target = new String(request.getParameter("url"));
out.print(url_target);
String filename = new String();
URL url = new URL(url_target);
// filename = url.getFile(); // 获得此 URL 的文件名
filename = url.getPath();
%>
<jsp:include flush="true" page="<%=filename%>"></jsp:include>
<%@ page import="java.net.URL"%>
<%
response.setHeader("Content-disposition","attachment; filename=print_tmp.doc");
%>
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title></title>
</head>
<%
String url_target = new String(request.getParameter("url"));
out.print(url_target);
String filename = new String();
URL url = new URL(url_target);
// filename = url.getFile(); // 获得此 URL 的文件名
filename = url.getPath();
%>
<jsp:include flush="true" page="<%=filename%>"></jsp:include>
3.要简单的内容页(省略)
二.方法二
<%@ page contentType="text/html;charset=GBK" %>
<HTML>
<HEAD>
<title>WEB页面导出为Excel文档的方法
</title>
</HEAD>
<body>
<BR>
<table id = "PrintA" width="100%" border="1" cellspacing="0" cellpadding="0" bgcolor = "#61FF13">
<TR style="text-align : center;">
<TD>单元格A</TD>
<TD>单元格A</TD>
<TD>单元格A</TD>
<TD>单元格A</TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
</TABLE>
<BR>
<table id = "PrintB" width="100%" border="1" cellspacing="0" cellpadding="0">
<TR style="text-align : center;">
<TD>单元格B</TD>
<TD>单元格B</TD>
<TD>单元格B</TD>
<TD>单元格B</TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;">单元格合并行B</TD>
</TR>
</TABLE>
<br/><br/><br/>
<input type="button" onclick="AllAreaWord();" value="导出页面指定区域内容到Word">
<input type="button" onclick="AllAreaExcel();" value="导出页面指定区域内容到Excel">
<input type="button" onclick="CellAreaExcel();" value="导出表单单元格内容到Excel">
<SCRIPT LANGUAGE="javascript">
// 指定页面区域内容导入Excel
function AllAreaExcel()
{
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var sel=document.body.createTextRange();
sel.moveToElementText(PrintA);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true;
}
// 指定页面区域“单元格”内容导入Excel
function CellAreaExcel()
{
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var Lenr = PrintA.rows.length;
for (i=0;i<Lenr;i++)
{
var Lenc = PrintA.rows(i).cells.length;
for (j=0;j<Lenc;j++)
{
oSheet.Cells(i+1,j+1).value = PrintA.rows(i).cells(j).innerText;
}
}
oXL.Visible = true;
}
// 指定页面区域内容导入Word
function AllAreaWord()
{
var oWD = new ActiveXObject("Word.Application");
var oDC = oWD.Documents.Add("",0,1);
var oRange =oDC.Range(0,1);
var sel = document.body.createTextRange();
sel.moveToElementText(PrintA);
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true;
//window.close();
}
</SCRIPT>
</body></html>
<HTML>
<HEAD>
<title>WEB页面导出为Excel文档的方法
</title>
</HEAD>
<body>
<BR>
<table id = "PrintA" width="100%" border="1" cellspacing="0" cellpadding="0" bgcolor = "#61FF13">
<TR style="text-align : center;">
<TD>单元格A</TD>
<TD>单元格A</TD>
<TD>单元格A</TD>
<TD>单元格A</TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
</TABLE>
<BR>
<table id = "PrintB" width="100%" border="1" cellspacing="0" cellpadding="0">
<TR style="text-align : center;">
<TD>单元格B</TD>
<TD>单元格B</TD>
<TD>单元格B</TD>
<TD>单元格B</TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;">单元格合并行B</TD>
</TR>
</TABLE>
<br/><br/><br/>
<input type="button" onclick="AllAreaWord();" value="导出页面指定区域内容到Word">
<input type="button" onclick="AllAreaExcel();" value="导出页面指定区域内容到Excel">
<input type="button" onclick="CellAreaExcel();" value="导出表单单元格内容到Excel">
<SCRIPT LANGUAGE="javascript">
// 指定页面区域内容导入Excel
function AllAreaExcel()
{
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var sel=document.body.createTextRange();
sel.moveToElementText(PrintA);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true;
}
// 指定页面区域“单元格”内容导入Excel
function CellAreaExcel()
{
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var Lenr = PrintA.rows.length;
for (i=0;i<Lenr;i++)
{
var Lenc = PrintA.rows(i).cells.length;
for (j=0;j<Lenc;j++)
{
oSheet.Cells(i+1,j+1).value = PrintA.rows(i).cells(j).innerText;
}
}
oXL.Visible = true;
}
// 指定页面区域内容导入Word
function AllAreaWord()
{
var oWD = new ActiveXObject("Word.Application");
var oDC = oWD.Documents.Add("",0,1);
var oRange =oDC.Range(0,1);
var sel = document.body.createTextRange();
sel.moveToElementText(PrintA);
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true;
//window.close();
}
</SCRIPT>
</body></html>
本文转自yunlielai51CTO博客,原文链接:http://blog.51cto.com/4925054/1148822,如需转载请自行联系原作者