使用javascript实现:
<script language="javascript">
function tableToExcel() {
window.clipboardData.setData("Text",document.all('theObjTable').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;
}
</script><table id=theObjTable>
<tr>
<td>A1</td>
<td>B1</td>
</tr>
<tr>
<td>A2</td>
<td>B2</td>
</tr>
</table><input type="button" value="导入到excel" onclick="tableToExcel()">
<script language="javascript">
function tableToExcel() {
window.clipboardData.setData("Text",document.all('theObjTable').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;
}
</script><table id=theObjTable>
<tr>
<td>A1</td>
<td>B1</td>
</tr>
<tr>
<td>A2</td>
<td>B2</td>
</tr>
</table><input type="button" value="导入到excel" onclick="tableToExcel()">
你给table加上border=1的属性,就可以清楚的看到这一点
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40"><head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 11">
<link rel=File-List href="t.files/filelist.xml">
<link rel=Edit-Time-Data href="t.files/editdata.mso">
<link rel=OLE-Object-Data href="t.files/oledata.mso">
<!--[if gte mso 9]><xml>
<o:DocumentProperties>
<o:LastAuthor>x</o:LastAuthor>
<o:LastSaved>2005-12-20T07:05:32Z</o:LastSaved>
<o:Version>11.6408</o:Version>
</o:DocumentProperties>
</xml><![endif]-->
<style>
<!--table
{mso-displayed-decimal-separator:"\.";
mso-displayed-thousand-separator:"\,";}
@page
{margin:1.0in .75in 1.0in .75in;
mso-header-margin:.5in;
mso-footer-margin:.5in;}
tr
{mso-height-source:auto;
mso-ruby-visibility:none;}
col
{mso-width-source:auto;
mso-ruby-visibility:none;}
br
{mso-data-placement:same-cell;}
.style0
{mso-number-format:General;
text-align:general;
vertical-align:middle;
white-space:nowrap;
mso-rotate:0;
mso-background-source:auto;
mso-pattern:auto;
color:windowtext;
font-size:12.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:SimSun;
mso-generic-font-family:auto;
mso-font-charset:134;
border:none;
mso-protection:locked visible;
mso-style-name:\5E38\89C4;
mso-style-id:0;}
td
{mso-style-parent:style0;
padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:windowtext;
font-size:12.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:SimSun;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-number-format:General;
text-align:general;
vertical-align:middle;
border:none;
mso-background-source:auto;
mso-pattern:auto;
mso-protection:locked visible;
white-space:nowrap;
mso-rotate:0;}
.xl24
{mso-style-parent:style0;
white-space:normal;}
ruby
{ruby-align:left;}
rt
{color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:SimSun;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-char-type:none;
display:none;}
-->
</style>
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>t</x:Name>
<x:WorksheetOptions>
<x:DefaultRowHeight>285</x:DefaultRowHeight>
<x:Selected/>
<x:DoNotDisplayGridlines/>
<x:Panes>
<x:Pane>
<x:Number>3</x:Number>
<x:ActiveRow>5</x:ActiveRow>
</x:Pane>
</x:Panes>
<x:ProtectContents>False</x:ProtectContents>
<x:ProtectObjects>False</x:ProtectObjects>
<x:ProtectScenarios>False</x:ProtectScenarios>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
<x:WindowHeight>6075</x:WindowHeight>
<x:WindowWidth>10620</x:WindowWidth>
<x:WindowTopX>360</x:WindowTopX>
<x:WindowTopY>105</x:WindowTopY>
<x:ProtectStructure>False</x:ProtectStructure>
<x:ProtectWindows>False</x:ProtectWindows>
</x:ExcelWorkbook>
</xml><![endif]-->
</head><body link=blue vlink=purple><table x:str border=0 cellpadding=0 cellspacing=0 width=72 style='border-collapse:
collapse;table-layout:fixed;width:54pt'>
<col width=28 style='mso-width-source:userset;mso-width-alt:896;width:21pt'>
<col width=44 style='mso-width-source:userset;mso-width-alt:1408;width:33pt'>
<tr height=38 style='height:28.5pt'>
<td height=38 class=xl24 width=28 style='height:28.5pt;width:21pt'>A1</td>
<td class=xl24 width=44 style='width:33pt'>B1<br>
C1</td>
</tr>
<tr height=19 style='height:14.25pt'>
<td height=19 class=xl24 width=28 style='height:14.25pt;width:21pt'>A2</td>
<td class=xl24 width=44 style='width:33pt'>B2</td>
</tr>
<![if supportMisalignedColumns]>
<tr height=0 style='display:none'>
<td width=28 style='width:21pt'></td>
<td width=44 style='width:33pt'></td>
</tr>
<![endif]>
</table></body></html>