论坛首页 Java企业应用论坛

使用ActiveX输出页面内容到Excel

浏览 6539 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-03-10  
刚从网上找到的代码,使用ActiveX把HTML里面的内容输出到Excel真的比较方便,而且不需要针对每个页面写脚本,不过会有安全问题。大家认为如何?
<title>&Ecirc;&amp;&Ntilde;é</title>
<table id = 'prtable1' border = 1 width = '400'>
<tr id = 'tr'>
<td id = 'tabletd'>00</td>
<td id = 'tabletd'>00</td>
<td id = 'tabletd'>00</td>
</tr>
<tr id = 'tr'>
<td id = 'tabletd'>c1r1</td>
<td id = 'tabletd' colspan = "2">c2r1</td>
</tr>
<tr id = 'tr'>
<td id = 'tabletd'>c1r2</td>
<td id = 'tabletd'>c2r2</td>
<td id = 'tabletd'>c3r2</td>
</tr>
<tr id = 'tr'>
<td id = 'tabletd' colspan = "2">c2r3</td>
<td id = 'tabletd'>c3r3</td>
</tr>
<tr id = 'tr'>
<td id = 'tabletd' colspan = "3">c2r3</td>
</tr>
<tr id = 'tr'>
<td id = 'tabletd'>c1r1</td>
<td id = 'tabletd' colspan = "2">c2r1</td>
</tr>
<tr id = 'tr'>
<td id = 'tabletd' colspan = "2">c1r2</td>
<td id = 'tabletd'>c2r2</td>
</tr>
<tr id = 'tr'>
<td id = 'tabletd'>c1r3</td>
<td id = 'tabletd' colspan = "2">c2r3</td>
</tr>
</table>
<br>
<input type = 'button' value = '&amp;ò&Oacute;&amp;' onclick = "tableinsert();">
<table id = 'thecpationtable' border = 0>
<tr>
<td align = 'cetner'>&Ograve;&amp;&amp;ò&Oacute;&amp;&amp;&Auml;±í&amp;&amp;</td>
</tr>
</table>
<span id = 'thespan'>
</span>
<script>

function tableinsert();
{
var rows = 0
var cols = 0
tablestr = "<table border = 1>"
var ExApp = new ActiveXObject("Excel.Application");
var ExWBk = ExApp.workbooks.add();
var ExWSh = ExWBk.worksheets.add();
ExApp.DisplayAlerts = false
ExApp.visible = true
for(var i = 0; i < tr.length; i ++);
{ cols = 0
var myrow = tr[i]
if(myrow.getAttribute("id"); != "");
{
rows ++
for(var j = 0; j < myrow.cells.length; j ++);
{
var mycell = myrow.cells[j]
if(mycell.getAttribute("id"); != "");
{
cols ++
var iColspan = getColspan(mycell);
if(iColspan > 0);
{
ExWSh.range(ExWSh.cells(rows,cols);, ExWSh.cells(rows,cols + iColspan););.MergeCells = true
}
ExWSh.cells(rows,cols);.value = mycell.innerText
cols = cols + iColspan
}
}
}
}
ExWSh.Range(ExWSh.cells(1,1);, ExWSh.cells(rows,cols););.borders.weight = 2
ExWSh.Range(ExWSh.cells(1,1);, ExWSh.cells(rows,cols););.borders.LineStyle = 1
ExWSh.PageSetup.CenterHorizontally = true 
}

function getColspan(obj);
{
var theColspan = obj.getAttribute("colspan");
if(theColspan != 1 && theColspan != null); return theColspan -1
else return 0
}
</script> 
   发表时间:2004-03-10  
不错
0 请登录后投票
   发表时间:2004-03-10  
用处不会很大,页面中的数据一般是分页显示的,而excel中需要所有的查询结果。

如果把数据全部放到页面上,然后用activeX倒入到excel,那就可以来欣赏office的速度是如何慢了。

所以还不如在后台用api直接生成excel文件。

孤魂一笑 写道
刚从网上找到的代码,使用ActiveX把HTML里面的内容输出到Excel真的比较方便,而且不需要针对每个页面写脚本,不过会有安全问题。大家认为如何?
<title>&Ecirc;&&Ntilde;é</title>
<table id = 'prtable1' border = 1 width = '400'>
<tr id = 'tr'>
<td id = 'tabletd'>00</td>
<td id = 'tabletd'>00</td>
<td id = 'tabletd'>00</td>
</tr>
<tr id = 'tr'>
<td id = 'tabletd'>c1r1</td>
<td id = 'tabletd' colspan = "2">c2r1</td>
</tr>
<tr id = 'tr'>
<td id = 'tabletd'>c1r2</td>
<td id = 'tabletd'>c2r2</td>
<td id = 'tabletd'>c3r2</td>
</tr>
<tr id = 'tr'>
<td id = 'tabletd' colspan = "2">c2r3</td>
<td id = 'tabletd'>c3r3</td>
</tr>
<tr id = 'tr'>
<td id = 'tabletd' colspan = "3">c2r3</td>
</tr>
<tr id = 'tr'>
<td id = 'tabletd'>c1r1</td>
<td id = 'tabletd' colspan = "2">c2r1</td>
</tr>
<tr id = 'tr'>
<td id = 'tabletd' colspan = "2">c1r2</td>
<td id = 'tabletd'>c2r2</td>
</tr>
<tr id = 'tr'>
<td id = 'tabletd'>c1r3</td>
<td id = 'tabletd' colspan = "2">c2r3</td>
</tr>
</table>
<br>
<input type = 'button' value = '&ò&Oacute;&' onclick = "tableinsert();">
<table id = 'thecpationtable' border = 0>
<tr>
<td align = 'cetner'>&Ograve;&&ò&Oacute;&&&Auml;±í&&</td>
</tr>
</table>
<span id = 'thespan'>
</span>
<script>

function tableinsert();
{
var rows = 0
var cols = 0
tablestr = "<table border = 1>"
var ExApp = new ActiveXObject("Excel.Application");
var ExWBk = ExApp.workbooks.add();
var ExWSh = ExWBk.worksheets.add();
ExApp.DisplayAlerts = false
ExApp.visible = true
for(var i = 0; i < tr.length; i ++);
{ cols = 0
var myrow = tr[i]
if(myrow.getAttribute("id"); != "");
{
rows ++
for(var j = 0; j < myrow.cells.length; j ++);
{
var mycell = myrow.cells[j]
if(mycell.getAttribute("id"); != "");
{
cols ++
var iColspan = getColspan(mycell);
if(iColspan > 0);
{
ExWSh.range(ExWSh.cells(rows,cols);, ExWSh.cells(rows,cols + iColspan););.MergeCells = true
}
ExWSh.cells(rows,cols);.value = mycell.innerText
cols = cols + iColspan
}
}
}
}
ExWSh.Range(ExWSh.cells(1,1);, ExWSh.cells(rows,cols););.borders.weight = 2
ExWSh.Range(ExWSh.cells(1,1);, ExWSh.cells(rows,cols););.borders.LineStyle = 1
ExWSh.PageSetup.CenterHorizontally = true 
}

function getColspan(obj);
{
var theColspan = obj.getAttribute("colspan");
if(theColspan != 1 && theColspan != null); return theColspan -1
else return 0
}
</script> 
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics