各位大哥:帮我看看一个jsp中的表格导入到excel中的问题,,万分感谢
我试了一下在ie浏览的情况下下,用下面代码可以将html中的表格导入到excel表中,为什么在tomcat服务器下就不行了呢?怎么改这段代码啊?
function PrintTableToExcel(objTab)
{
try
{
var xls = new ActiveXObject( "Excel.Application" );
}
catch(e)
{
alert( "要打印该表,您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”,您的浏览器须允许执行控件。 请点击【帮助】了解浏览器设置方法!");
return false;
}
xls.visible = true;
var xlBook = xls.Workbooks.Add;
var xlsheet = xlBook.Worksheets(1);
var x = 1;
var y = 1;
for (var i = 0; i < objTab.rows.length; i++)
{
y = 1;
for (var j = 0; j < objTab.rows[i].cells.length; j++)
{
xlsheet.Cells(x, y).Value = objTab.rows[i].cells[j].innerHTML;
xlsheet.Cells(x, y).Borders.LineStyle = 1;
y++;
}
x++;
}
xlsheet.Columns.AutoFit; //自动适应大小
return;
}
问题点数:20、回复次数:3Top
1 楼dawangzi16(大望子)回复于 2005-10-06 13:24:42 得分 0
转自别处,你比较一下:
直接写字板连后ie 打开。
<%@ page contentType="text/html; charset=GBK"%>
<input type="hidden" name="out_excel" onclick="AutomateExcel();" value="µ¼³öµ½excel" class="notPrint">
<title>ä¯ÀÀÆ÷±í¸ñµ¼³öµ½Excel</title>
<input type="button" name="out_word1" onclick="javascript:AutomateExcel() " value="µ¼³öµ½excel" class="notPrint">
<table id="data" width="200" border="1">
<tr>
<td>ÎÒÊÇÀî´ºÀ×</td>
<td>11</td>
</tr>
<tr>
<td>22</td>
<td>22</td>
</tr>
<tr>
<td>33</td>
<td>33</td>
</tr>
<tr>
<td>44¡¡</td>
<td>44</td>
</tr>
</table>
ÎÒÊÇÀî´ºÀ×
<SCRIPT LANGUAGE="JavaScript">
<!--
function AutomateExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var table = document.all.data;
var hang = table.rows.length;
var lie = table.rows(0).cells.length;
// Add table headers going cell by cell.
for (i=0;i<hang;i++)
{
for (j=0;j<lie;j++)
{
oSheet.Cells(i+1,j+1).Value = table.rows(i).cells(j).innerText;
}
}
oXL.Visible = true;
oXL.UserControl = true;
}
//-->
</SCRIPT>Top
2 楼dawangzi16(大望子)回复于 2005-10-06 13:27:04 得分 0
上边的有乱码:
<%@ page contentType="text/html; charset=GBK"%>
<input type="hidden" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="notPrint">
<title>浏览器表格导出到Excel</title>
<input type="button" name="out_word1" onclick="javascript:AutomateExcel() " value="导出到excel" class="notPrint">
<table id="data" width="200" border="1">
<tr>
<td>我是李春雷</td>
<td>11</td>
</tr>
<tr>
<td>22</td>
<td>22</td>
</tr>
<tr>
<td>33</td>
<td>33</td>
</tr>
<tr>
<td>44 </td>
<td>44</td>
</tr>
</table>
我是李春雷
<SCRIPT LANGUAGE="JavaScript">
<!--
function AutomateExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var table = document.all.data;
var hang = table.rows.length;
var lie = table.rows(0).cells.length;
// Add table headers going cell by cell.
for (i=0;i<hang;i++)
{
for (j=0;j<lie;j++)
{
oSheet.Cells(i+1,j+1).Value = table.rows(i).cells(j).innerText;
}
}
oXL.Visible = true;
oXL.UserControl = true;
}
//-->
</SCRIPT>
Top
3 楼tzxyc()回复于 2005-10-06 20:05:44 得分 0
给你一段源程序,你看一下就能明白了,我自己用的很好,现在我开发的系统就是用的是这个方法
<%@page contentType="text/html; charset=gb2312" language="java"%>
<%@page import="java.io.*" %>
<%@page import="jxl.*" %>
<%@page import="jxl.write.*" %>
<%@page import="jxl.format.*" %>
<html>
<head>
<title>Excel</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
</head>
<body>
<%
try
{
InputStream is = new FileInputStream("c:\\hihigo.xls");
jxl.Workbook rwb = Workbook.getWorkbook(is);
int mysheets = rwb.getNumberOfSheets();
jxl.Sheet rs = rwb.getSheet(0);
String myname = ""+rs.getName();
int rsColumns = rs.getColumns();
int rsRows = rs.getRows();
Cell c00 = rs.getCell(1,1);
String strc00 = c00.getContents();
String mytype0 = ""+c00.getType();
Cell c01 = rs.getCell(2,2);
String strc01 = c01.getContents();
String mytype1 = ""+c01.getType();
%>
No of sheets : <%=mysheets%>
<br>
sheet name: <%=myname%>
<br>
No of Columns : <%=rsColumns%>
<br>
No of Rows : <%=rsRows%>
<br>
vlaue1: <%=strc00%>
<br>
type1 : <%=mytype0%>
<br>
vlaue2: <%=strc01%>
<br>
type2 : <%=mytype1%>
<%
rwb.close();
}
catch (Exception e)
{
e.printStackTrace();
}
%>
<%
try
{
//Method 1:创建可写入的Excel工作薄
//jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(targetfile,rw));
//Method 2:将WritableWorkbook直接写入到输出流
OutputStream os = new FileOutputStream("c:\\test.xls");
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
jxl.write.WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);
//1.添加Label对象
jxl.write.Label labelC = new jxl.write.Label(0, 0, "This is a Label cell");
ws.addCell(labelC);
//添加带有字型Formatting的对象
jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD, true);
jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf);
jxl.write.Label labelCF = new jxl.write.Label(1, 0, "This is a Label Cell", wcfF);
ws.addCell(labelCF);
//2.添加Number对象
jxl.write.Number labelN = new jxl.write.Number(0, 1, 3.1415926);
ws.addCell(labelN);
//添加带有formatting的Number对象
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
jxl.write.Number labelNF = new jxl.write.Number(1, 1, 3.1415926, wcfN);
ws.addCell(labelNF);
//3.添加Boolean对象
jxl.write.Boolean labelB = new jxl.write.Boolean(0, 2, false);
ws.addCell(labelB);
//4.添加DateTime对象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0, 3, new java.util.Date());
ws.addCell(labelDT);
//添加带有formatting的DateFormat对象
jxl.write.DateFormat df = new jxl.write.DateFormat("dd MM yyyy hh:mm:ss");
jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
jxl.write.DateTime labelDTF = new jxl.write.DateTime(1, 3, new java.util.Date(), wcfDF);
ws.addCell(labelDTF);
//写入Exel工作表
wwb.write();
//关闭Excel工作薄对象
wwb.close();
out.println("ok");
}
catch (Exception e)
{
out.println("not ok");
e.printStackTrace();
}
%>
</body>
</html>Top




