下面我總結一下導出word檔案和excel檔案的2種辦法。
一. 方法一
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;
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");

%>




<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>



<%

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.要簡單的内容頁(省略)
二.方法二


<HTML>

<HEAD>

<title>WEB頁面導出為Excel文檔的方法

</title>

</HEAD>


<BR>

<table id = "PrintA" width="100%" border="1" cellspacing="0" cellpadding="0" bgcolor = "#61FF13">

<TR style="text-align : center;">

<TD>單元格A</TD>




</TR>

<TR>

<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">單元格合并行A</FONT></TD>


</TABLE>


<table id = "PrintB" width="100%" border="1" cellspacing="0" cellpadding="0">


<TD>單元格B</TD>






<TD colSpan=4 style="text-align : center;">單元格合并行B</TD>



<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 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();
oRange.Paste();
oWD.Application.Visible = true;
//window.close();

</SCRIPT>

</body></html>

本文轉自yunlielai51CTO部落格,原文連結:http://blog.51cto.com/4925054/1148822,如需轉載請自行聯系原作者