如下代码可以实现:
<SCRIPT LANGUAGE="javascript">
function AutomateExcel()
{
var oXL = new ActiveXObject("Excel.Application");
oXL.DisplayAlerts = false
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var hang = parent.mainFrame.outtable.rows.length; var lie = parent.mainFrame.outtable.rows(1).cells.length; for (i=0;i<hang;i++)
{
for (j=0;j<lie;j++)
{
oSheet.Cells(i+1,j+1).value = parent.mainFrame.outtable.rows(i).cells(j).innerText; //outtable为你要导为excel的Table的ID
} }
oXL.Visible = true;
oXL.UserControl = true;
}
</SCRIPT>
---------------------------
<input type="button" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="button" style="width:120px;">
<SCRIPT LANGUAGE="javascript">
function AutomateExcel()
{
var oXL = new ActiveXObject("Excel.Application");
oXL.DisplayAlerts = false
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var hang = parent.mainFrame.outtable.rows.length; var lie = parent.mainFrame.outtable.rows(1).cells.length; for (i=0;i<hang;i++)
{
for (j=0;j<lie;j++)
{
oSheet.Cells(i+1,j+1).value = parent.mainFrame.outtable.rows(i).cells(j).innerText; //outtable为你要导为excel的Table的ID
} }
oXL.Visible = true;
oXL.UserControl = true;
}
</SCRIPT>
---------------------------
<input type="button" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="button" style="width:120px;">
<head>
<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; //处理第一行合并单元格
oSheet.Cells(1,1).value = table.rows(0).cells(0).innerText; // Add table headers going cell by cell.
for (i=1;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>
</head>
<body>
<table id="data" border=1 width=100% height=200 bordercolorlight="#000000" bordercolordark="#000000" style="border-collapse: collapse">
<tr>
<td width="20%" height="30">1</td>
<td width="20%" height="30">2</td>
<td width="20%" height="30">3</td>
<td width="20%" height="30">4</td>
<td width="20%" height="30">5</td>
</tr>
<tr>
<td width="20%" height="30">1</td>
<td width="20%" height="30">2</td>
<td width="20%" height="30">3</td>
<td width="20%" height="30">4</td>
<td width="20%" height="30">5</td>
</tr>
</table>
<input type="button" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="notPrint">
</body>
</html>
<BODY>
<SCRIPT LANGUAGE="VBScript">
Function CreateNamesArray()
' Create an array to set multiple values at once.
Dim saNames(5, 2)
saNames(0, 0) = "John"
saNames(0, 1) = "Smith"
saNames(1, 0) = "Tom"
saNames(1, 1) = "Brown"
saNames(2, 0) = "Sue"
saNames(2, 1) = "Thomas"
saNames(3, 0) = "Jane"
saNames(3, 1) = "Jones"
saNames(4, 0) = "Adam"
saNames(4, 1) = "Johnson"
CreateNamesArray = saNames
End Function
</SCRIPT><SCRIPT LANGUAGE="JScript">
function AutomateExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
oXL.Visible = true;
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
// Add table headers going cell by cell.
oSheet.Cells(1, 1).Value = "First Name";
oSheet.Cells(1, 2).Value = "Last Name";
oSheet.Cells(1, 3).Value = "Full Name";
oSheet.Cells(1, 4).Value = "Salary";
// Format A1:D1 as bold, vertical alignment = center.
oSheet.Range("A1", "D1").Font.Bold = true;
oSheet.Range("A1", "D1").VerticalAlignment = -4108; //xlVAlignCenter
// Create an array to set multiple values at once. // Fill A2:B6 with an array of values (from VBScript).
oSheet.Range("A2", "B6").Value = CreateNamesArray();
// Fill C2:C6 with a relative formula (=A2 & " " & B2).
var oRng = oSheet.Range("C2", "C6");
oRng.Formula = "=A2 & \" \" & B2";
// Fill D2:D6 with a formula(=RAND()*100000) and apply format.
oRng = oSheet.Range("D2", "D6");
oRng.Formula = "=RAND()*100000";
oRng.NumberFormat = "$0.00";
// AutoFit columns A:D.
oRng = oSheet.Range("A1", "D1");
oRng.EntireColumn.AutoFit();
// Manipulate a variable number of columns for Quarterly Sales Data.
DispalyQuarterlySales(oSheet);
// Make sure Excel is visible and give the user control
// of Excel's lifetime.
oXL.Visible = true;
oXL.UserControl = true;
}
function DispalyQuarterlySales(oWS)
{
var iNumQtrs, sMsg, iRet; // Number of quarters to display data for.
iNumQtrs = 4;
// Starting at E1, fill headers for the number of columns selected.
var oResizeRange = oWS.Range("E1", "E1").Resize(1,iNumQtrs);
oResizeRange.Formula = "=\"Q\" & COLUMN()-4 & CHAR(10) & \"Sales\"";
// Change the Orientation and WrapText properties for the headers.
oResizeRange.Orientation = 38;
oResizeRange.WrapText = true;
// Fill the interior color of the headers.
oResizeRange.Interior.ColorIndex = 36;
// Fill the columns with a formula and apply a number format.
oResizeRange = oWS.Range("E2", "E6").Resize(5,iNumQtrs);
oResizeRange.Formula = "=RAND()*100";
oResizeRange.NumberFormat = "$0.00";
// Apply borders to the Sales data and headers.
oResizeRange = oWS.Range("E1", "E6").Resize(6,iNumQtrs);
oResizeRange.Borders.Weight = 2; // xlThin
// Add a Totals formula for the sales data and apply a border.
oResizeRange = oWS.Range("E8", "E8").Resize(1,iNumQtrs);
oResizeRange.Formula = "=SUM(E2:E6)";
// 9 = xlEdgeBottom
oResizeRange.Borders(9).LineStyle = -4119; //xlDouble
oResizeRange.Borders(9).Weight = 4; //xlThick
// Add a Chart for the selected data.
oResizeRange = oWS.Range("E2:E6").Resize(5,iNumQtrs);
var oChart = oWS.Parent.Charts.Add();
oChart.ChartWizard(oResizeRange, -4100, null, 2); // -4100 = xl3dColumn
oChart.SeriesCollection(1).XValues = oWS.Range("A2", "A6");
for (iRet = 1; iRet <= iNumQtrs; iRet++) {
oChart.SeriesCollection(iRet).Name = "=\"Q" + iRet + "\"";
}
oChart.Location(2, oWS.Name); // 2 = xlLocationAsObject
// Move the chart so as not to cover your data.
oWS.Shapes("Chart 1").Top = oWS.Rows(10).Top;
oWS.Shapes("Chart 1").Left = oWS.Columns(2).Left;
}
</SCRIPT>
<P><INPUT id=button1 type=button value="创建Excel表格和柱状图" onclick="AutomateExcel"></P>
</BODY>
</HTML>
2) 不能修改浏览器安全设置
我也是这个错误
好象是这样改一下就可以。
<HEAD>
</HEAD>
<BODY>
<table id="tblData1">
<tr><td>gdsssa</td><td>445</td></tr>
<tr><td>gdsssa</td><td>445</td></tr>
</table>
<table id="tblData" width="100%" height="165" border="0" cellpadding="1" cellspacing="1" id="List" dwcopytype="CopyTableRow">
<tr class="tableHead">
<td width="51" height="9" style="TEXT-ALIGN: right">序号</td>
<td width="111">项目名称</td>
<td width="206" style="TEXT-ALIGN: left">机组选择</td>
<td width="216" height="9" style="TEXT-ALIGN: left">功能描述</td>
<td width="184" height="9" style="TEXT-ALIGN: left">备注</td>
</tr>
<tr class="cellCenter">
<td style="TEXT-ALIGN: right" height="5">1</td>
<td ><a href="www.sohu.com">主汽温度</a></td>
<td style="TEXT-ALIGN: left"> <input type="checkbox" id="checkbox1" name="checkbox1" value="2" checked onclick="runCheckSeclect()">
#5
<input type="checkbox" id="checkbox2" name="checkbox2" value="1" onclick="runCheckSeclect()">
#6 </td>
<td style="TEXT-ALIGN: left" height="4"></td>
<td height="4" style="TEXT-ALIGN: left"></td>
</tr>
<tr class="cellCenter">
<td style="TEXT-ALIGN: right" height="10">13</td>
<td>#2高加端差</td>
<td style="TEXT-ALIGN: left"> <input type="checkbox" id="checkbox12214" name="checkbox12213" value="2" checked onclick="runCheckSeclect()">
#5
<input type="checkbox" id="checkbox22214" name="checkbox22213" value="1" onclick="runCheckSeclect()">
#6 </td>
<td style="TEXT-ALIGN: left" height="10"> </td>
<td height="10" style="TEXT-ALIGN: left"> </td>
</tr>
<tr class="cellCenter">
<td style="TEXT-ALIGN: right" height="10">14</td>
<td>#3高加端差</td>
<td style="TEXT-ALIGN: left"> <input type="checkbox" id="checkbox12215" name="checkbox12214" value="2" checked onclick="runCheckSeclect()">
#5
<input type="checkbox" id="checkbox22215" name="checkbox22214" value="1" onclick="runCheckSeclect()">
#6 </td>
<td style="TEXT-ALIGN: left" height="10"> </td>
<td height="10" style="TEXT-ALIGN: left"> </td>
</tr>
</table><input type="button" value="export" onclick="exportExcel(tblData)">
<P> </P>
</BODY>
<script language="javascript">
function exportExcel(atblData)
{
if(typeof(EXPORT_OBJECT)!="object")
{
document.body.insertAdjacentHTML("afterBegin","<OBJECT style='display:none' classid=clsid:0002E510-0000-0000-C000-000000000046 id=EXPORT_OBJECT></Object>");
}
with (EXPORT_OBJECT)
{
DataType = "HTMLData";
HTMLData =atblData.outerHTML;
try{
ActiveSheet.Export("C:\\owcToExcel.xls", 0);
alert('汇出完毕');
}
catch (e){
alert('汇出Excel表失败,请确定已安装Excel2000(或更高版本),并且没打开同名xls文件');
}
}
}
</script>
</HTML>对office的版本有要求,office2002好像不行,大家装了officeXp或者2002的帮忙测试看看
<input type="hidden" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="notPrint"> <title>浏览器表格导出到Excel代码</title>
<input type="button" name="out_word" onclick="vbscript:buildDoc" value="导出到word" class="notPrint">
<input type="button" name="out_word1" onclick="javascript:AutomateExcel() " value="导出到excel" class="notPrint">
<table id="data" width="200" border="1">
<tr>
<td>11</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>导出到Word代码
<script language="vbscript">
Sub buildDoc
set table = document.all.data
row = table.rows.length
column = table.rows(1).cells.lengthSet objWordDoc = CreateObject("Word.Document")'objWordDoc.Application.Documents.Add theTemplate, False
objWordDoc.Application.Visible=TrueDim theArray(20,10000)
for i=0 to row-1
for j=0 to column-1
theArray(j+1,i+1) = table.rows(i).cells(j).innerTEXT
next
next
objWordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("综合查询结果集") //显示表格标题objWordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("")
Set rngPara = objWordDoc.Application.ActiveDocument.Paragraphs(1).Range
With rngPara
.Bold = True //将标题设为粗体
.ParagraphFormat.Alignment = 1 //将标题居中
.Font.Name = "隶书" //设定标题字体
.Font.Size = 18 //设定标题字体大小
End With
Set rngCurrent = objWordDoc.Application.ActiveDocument.Paragraphs(3).Range
Set tabCurrent = ObjWordDoc.Application.ActiveDocument.Tables.Add(rngCurrent,row,column)for i = 1 to column objWordDoc.Application.ActiveDocument.Tables(1).Rows(1).Cells(i).Range.InsertAfter theArray(i,1)
objWordDoc.Application.ActiveDocument.Tables(1).Rows(1).Cells(i).Range.ParagraphFormat.alignment=1
next
For i =1 to column
For j = 2 to row
objWordDoc.Application.ActiveDocument.Tables(1).Rows(j).Cells(i).Range.InsertAfter theArray(i,j)
objWordDoc.Application.ActiveDocument.Tables(1).Rows(j).Cells(i).Range.ParagraphFormat.alignment=1
Next
NextEnd Sub
</SCRIPT>
var oXL = new ActiveXObject("Excel.Application");
all need owc.:)
<HTML>
<HEAD>
</HEAD><BODY leftmargin="0" topMargin="0" MARGINHEIGHT="0" MARGINWIDTH="0"><script language="jscript">function SaveNote(){ var elem = window.document.all('aaa');
if (elem) {
var selectedTable = elem;
var tmpValue = selectedTable.parentElement.innerHTML;
selectedTable.border = '1';
var mWindow = window.open('about:blank','Save','top=100, height=10, width=10, top=2000, left=2000, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no');
mWindow.document.write('<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=gb2312"><meta name=ProgId content=Excel.Sheet><meta name=Generator content="Microsoft Excel 10"></head><body>' + selectedTable.outerHTML + '</body></html>');
mWindow.document.execCommand("saveAs","1","测试.xls");
mWindow.close();
selectedTable.parentElement.innerHTML = tmpValue;
return true;
}}
</script><table border="1" id="aaa" name="aaa" >
<tr>
<th>字段1</th>
<th>字段2</th>
<th>字段3</th>
</tr>
<tr>
<td>数据11</td>
<td>数据12</td>
<td>数据13</td>
</tr>
<tr>
<td>数据21</td>
<td>数据22</td>
<td>数据23</td>
</tr>
<tr>
<td>数据31</td>
<td>数据32</td>
<td>数据33</td>
</tr>
</table>
<button onclick="SaveNote();">保存</button>
</HTML>