关闭

javascript 将table数据导出到excel中(支持表格线的导出)

标签: javascriptexceltablefunctionbuttonmicrosoft
3264人阅读 评论(0) 收藏 举报

正文

<HTML>
  <HEAD>
  <META http-equiv=Content-Type content="text/html; charset=utf-8">
    <title>WEB页面导出为EXCEL文档的方法
    </title>
  </HEAD>
<body>
<BR>
<table id = "PrintA" width="100%" border="1" cellspacing="0" cellpadding="0" bgcolor = "#61FF13">
<TR style="text-align : center;">
 <TD>单元格A</TD>
 <TD>单元格A</TD>
 <TD>单元格A</TD>
 <TD>单元格A</TD>
</TR>
<TR>
 <TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
<TR>
 <TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
<TR>
 <TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
<TR>
 <TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
<TR>
 <TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
<TR>
 <TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
</TABLE>
<BR>
<table id = "PrintB" width="100%" border="1" cellspacing="0" cellpadding="0">
<TR style="text-align : center;">
 <TD>单元格B</TD>
 <TD>单元格B</TD>
 <TD>单元格B</TD>
 <TD>单元格B</TD>
</TR>
<TR>
 <TD colSpan=4 style="text-align : center;">单元格合并行B</TD>
</TR>
</TABLE>
<br><br><br>
<input type="button" onClick="javascript:AllAreaWord();" value="导出页面指定区域内容到Word">
<input type="button" onClick="javascript:CellAreaExcel();" value="导出页面指定区域内容到Excel">
<input type="button" onClick="javascript:AllAreaExcel2();" value="导出表单单元格内容到Excel">
<SCRIPT LANGUAGE="javascript">
 //指定页面区域内容导入Excel
 function AllAreaExcel() 
 {
 	//window.clipboardData.setData("Text",document.all('PrintA').outerHTML);
  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;
 }
 
 function AllAreaExcel2() 
 {
 	alert("AllAreaExcel2");
 	window.clipboardData.setData("Text",document.all('PrintA').outerHTML);   
try  
{   
var ExApp = new ActiveXObject("Excel.Application")   
var ExWBk = ExApp.workbooks.add()   
var ExWSh = ExWBk.worksheets(1)   
ExApp.DisplayAlerts = false  
ExApp.visible = true  
}     
catch(e)   
{   
alert("您的电脑没有安装Microsoft Excel软件!")   
return false  
}    
 ExWBk.worksheets(1).Paste; 
}
 
 //指定页面区域“单元格”内容导入Excel
 function CellAreaExcel() 
 {
  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; 
 }

 //指定页面区域内容导入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();
  sel.moveToElementText(PrintA);
  sel.select();
  sel.execCommand("Copy");
  oRange.Paste();
  oWD.Application.Visible = true;
  //window.close();
 }
</SCRIPT>
</body>
</html>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:150010次
    • 积分:1751
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:57篇
    • 译文:0篇
    • 评论:7条
    文章分类
    最新评论