前提: 这个 很简答的说!!
js 代码贴上
<script type="text/javascript">
function saveAsExcel() {
var oXL = null;
try {
oXL = GetObject("", "Excel.Application");
} catch (E) {
try {
oXL = new ActiveXObject("Excel.Application");
} catch (E2) {
alert("请确认:\n1.安装Microsoft Excel.\n2.设置浏览器安全级别:Internet 选项=>安全=>自定义级别 \n \"对未标记为可安全执行脚本的ActiveX控件初始化并执行脚本\"");
return false;
}
}
// 创建AX对象excel
var oWB = oXL.Workbooks.Add();
// 获取workbook对象
var oSheet = oWB.ActiveSheet;
oSheet.Columns('A:A').ColumnWidth = 14;
oSheet.Columns('B:B').ColumnWidth = 16;
oSheet.Columns('D:D').ColumnWidth = 16;
oSheet.Columns('F:F').ColumnWidth = 16;
oSheet.Columns('H:H').ColumnWidth = 11;
oSheet.Columns('J:J').ColumnWidth = 20;
oSheet.Columns('O:O').ColumnWidth = 16;
oSheet.Columns('E:E').ColumnWidth = 12;
var tableBody = $("#GridSrarchView table").get(0); //当然这个是js对象
var Lenr = tableBody.rows.length;
//取得表格行数
var count = 0;
for (i = 0; i < Lenr; i++) {
var Lenc = tableBody.rows(i).cells.length;
if (Lenc < 15) {
count++;
continue;
}
//取得每行的列数
for (j = 3; j < Lenc; j++) {
var tempCell = oSheet.Cells(i - count + 1, j - 2);
if (j == 14 || j == 15) {
var obj = tableBody.rows(i).cells(j);
if (obj.innerText == "") {
if ($.trim(obj.innerHTML) != "") {
tempCell.value = "true";
} else {
tempCell.value = "false";
}
} else {
tempCell.value = tableBody.rows(i).cells(j).innerText.toString();
}
continue;
}
tempCell.value = tableBody.rows(i).cells(j).innerText.toString();
}
}
oXL.Visible = true;
return false;
}
</script>