1.直接輸出table格式
<%@ page language="java" pageEncoding="GBK"%><%response.setHeader("Content-disposition","attachment; filename=exdata.xls");%>
<table id="PrintA" border=1>
<tr class="line-odd">
<td align="right">資助項目名稱:</td><td align="left">資助項目名稱
</td>
</tr>
<tr class="line-even" >
<td align="right">流水號:</td><td align="left">流水號
</td>
</tr>
<tr class="line-odd" >
<td align="right">資助編號:</td><td align="left">
資助編號
</td>
</tr>
</table>
注意:亂碼問題,頁面儲存為utf-8格式
編碼格式轉換
String s="";
s=new String(s.getBytes( "gb2312 "), "iso-8859-1 ")
2. 利用jxl產生excel檔案
<%@ page language="java" pageEncoding="GBK"%>
<%@page import="javax.servlet.ServletOutputStream"%>
<%@page import="jxl.*"%>
<%@page import="jxl.write.*"%>
<%
response.setContentType("application/download");
response.setHeader("Content-Disposition","attachment;filename=totalExcel.xls");
ServletOutputStream sos = response.getOutputStream();
WritableWorkbook wwb=null;
WritableCellFormat contentFromart = new WritableCellFormat(NumberFormats.TEXT);
wwb = Workbook.createWorkbook(sos);
WritableSheet ws = wwb.createSheet("sheet1", 0);
WritableCellFormat cellFormat=new WritableCellFormat(); //換行功能
cellFormat.setAlignment(jxl.format.Alignment.LEFT);
cellFormat.setWrap(true);
Label lab = new Label(0, 0, "序號");
ws.addCell(lab);
lab = new Label(1, 0, "流水號",contentFromart);//將 數字字串放入lable
ws.addCell(lab);
lab = new Label(2, 0, "項目編號");
ws.addCell(lab);
lab = new Label(3, 0, "資助項目名");
ws.addCell(lab);
lab = new Label(4, 0, "項目開始日期");
ws.addCell(lab);
lab = new Label(5, 0, "金額");
ws.addCell(lab);
lab = new Label(6, 0, "負責人");
ws.addCell(lab);
lab = new Label(7, 0, "填表人");
ws.addCell(lab);
lab = new Label(8, 0, "資助單位");
ws.addCell(lab);
wwb.write();
wwb.close();
sos.flush();
sos.close();
out.clear();
out = pageContext.pushBody();
%>
3.js 匯出到excel(要求伺服器裝office)。因為用到了Excel.Application 控制項,還得將電腦IE的安全層級設定降低。 否則報“Automation 伺服器不能建立對象”錯誤。
方法一:
function AllAreaExcel(tableid) {//整個表格拷貝到EXCEL中
var PrintA= document.getElementById(tableid);
var oXL = new ActiveXObject("Excel.Application");
//建立AX對象excel
var oWB = oXL.Workbooks.Add();
//擷取workbook對象
var oSheet = oWB.ActiveSheet;
//啟用當前sheet
var sel = document.body.createTextRange();
sel.moveToElementText(PrintA);
//把表格中的內容移到TextRange中
sel.select();
//全選TextRange中內容
sel.execCommand("Copy");
//複製TextRange中內容
oSheet.Paste();
//粘貼到活動的EXCEL中
oXL.Visible = true;
//設定excel可見屬性
}
//指定頁面地區“儲存格”內容匯入Excel
function CellAreaExcel(tableid)
{
var PrintA= document.getElementById(tableid);
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;
}
4.jsp匯出到word
<%@ page language="java" pageEncoding="GBK"%><%@pagecontentType="application/vnd.ms-word;charset=gbk"%><%response.setHeader("Content-disposition","attachment; filename=exdata.doc");%>
<table id="PrintA" border=1>
<tr class="line-odd">
<td align="right">資助項目名稱:</td><td align="left">資助項目名稱
</td>
</tr>
<tr class="line-even" >
<td align="right">流水號:</td><td align="left">流水號
</td>
</tr>
<tr class="line-odd" >
<td align="right">資助編號:</td><td align="left">
資助編號
</td>
</tr>
</table>
5.js匯出到word
//指定頁面地區內容匯入Word
function AllAreaWord(tableid)
{
var PrintA= document.getElementById(tableid);
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;
}