ASP.NET的HTMLTable原样导出到Excel

             function  PrintTableToExcelEx(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 CurX = 1;
                
var CurY = 1;
                
                
for (var i = 0; i < objTab.rows.length; i++)
                
{
                    
for (var j = 0; j < objTab.rows[i].cells.length; j++)
                    
{
                        
var cell = objTab.rows[i].cells[j];
                        
                        
var v = cell.outerText;
                                                            
                        
if ( cell.children != null && cell.children.length != 0 )
                        
{
                            v 
= "";
                            
for ( var k = 0; k < cell.children.length; k++ )
                            
{
                                
var cc = cell.children[k];
                                
                                
switch (cc.type)
                                
{
                                    
case "select-one":
                                        
for ( var l = 0; l < cc.options.length; l++ )
                                            
if ( cc.options[l].selected )    v += cc.options[l].text;
                                        
break;
                                    
case "text":
                                        v 
+= cc.value;
                                        
break;
                                    
case "checkbox":
                                        v 
+= cc.checked ? "" : "";
                                        
break;
                                    
case undefined:
                                        v 
+= cell.outerText;
                                        
break;
                                    
case "hidden":
                                        
break;
                                    
default:
                                        
//v += cc.id + "/";
                                        //v += cc.tagname + "/";
                                        //v += cc.type + "/"
                                        v += cc.value;
                                        
break;        
                                }

                            }

                        }


                        
while ( xlsheet.Cells(CurX,CurY).MergeCells )        CurY++;
                        
                        xlsheet.Cells(CurX, CurY).HorizontalAlignment 
= 2;                    
                        
var align = cell.getAttribute("align");
                        
switch ( align )
                        
{
                            
case "left":
                                xlsheet.Cells(CurX, CurY).HorizontalAlignment 
= 2;//XlHalign.xlHalignLeft;
                                break;
                            
case "center":
                                xlsheet.Cells(CurX, CurY).HorizontalAlignment 
= 3;;//XlHalign.xlHalignCenter;        
                                break;
                            
case "right":
                                xlsheet.Cells(CurX, CurY).HorizontalAlignment 
= 4;//XlHalign.xlHalignRight;
                                break;
                        }

                        xlsheet.Cells(CurX, CurY).WrapText 
= true;
                        xlsheet.Cells(CurX, CurY).VerticalAlignment 
= 2;
                        xlsheet.Cells(CurX, CurY).Value 
= v;
                        xlsheet.Cells(CurX, CurY).Borders.LineStyle 
= 1;

                        
var rowSpan = cell.getAttribute("RowSpan");
                        
var colSpan = cell.getAttribute("ColSpan");
                        
if ( rowSpan >= 2 || colSpan >= 2 )
                        
{
                            
var R = xls.Range(xls.Cells(CurX, CurY), xls.Cells(CurX + rowSpan - 1, CurY + colSpan - 1));
                            R.MergeCells 
= true;
                            R.Borders.LineStyle 
= 1;
                        }

                        CurY
++;                        
                    }

                    
                    CurX
++;
                    CurY 
= 1;
                }

                xlsheet.Columns.AutoFit; 
//自动适应大小
                return;
            }

调用:

 

< INPUT style = " WIDTH: 107px "  onclick = " javascript:PrintTableToExcelEx(Table1) "  type = " button "
                        value
= " 导  出 "  class = " input " >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值