変換:Javascriptはウェブページの中でtableの内容をexcelの中のいくつかの方法にエクスポートすることを実現します.
9787 ワード
、 EXCEL
、 , EXCEL 。
、 , IE .csv 。
:
1. , , , , , , ,
2. , , 。
3. IE , ActiveXObject , 。
:
1. :Automation 。
: IE : ActiveX 。
EXCEL , , EXCEL 。
:
<tr><td colspan="x" align="center">&</td></tr>
X, , , ,oSheet sheet.
oSheet.Range(oSheet.Cells(1, 1), oSheet.Cells(1, x)).value = " ";//
oSheet.Rows(1).Font.Size = 16; //
oSheet.Rows(1).Font.Name = " ";//
: , ,
oSheet.Range(oSheet.Cells(1,1), oSheet.Cells(1,14)).mergecells=true; //
oSheet.Range(oSheet.Cells(1,1), oSheet.Cells(1,14)).Interior.ColorIndex=6;// ?
oSheet.Range(oSheet.Cells(1,1), oSheet.Cells(1,14)).Font.ColorIndex=5;// ?
oSheet.Rows(1).RowHeight=20; //
oSheet.Cells(iRow,iCol).Halignment=’2’//
2. :Automation ( )。
EXCEL ( )
, : 。
oSheet.Range(oSheet.Cells(1,1), oSheet.Cells(1,14)).mergecells=true; //
3. EXCEL ( )
, ,(rowspan>1 or colspan>1), : , , 。
:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td colspan="5" align="center">WEB EXCEL </td>
</tr>
<tr>
<td> 1</td>
<td> 2</td>
<td> 3</td>
<td> 4</td>
<td> 5</td>
</tr>
<tr>
<td>aaa</td>
<td>bbb</td>
<td>ccc</td>
<td>ddd</td>
<td>eee</td>
</tr>
<tr>
<td>AAA</td>
<td>BBB</td>
<td>CCC</td>
<td>DDD</td>
<td>EEE</td>
</tr>
<tr>
<td>FFF</td>
<td>GGG</td>
<td>HHH</td>
<td>III</td>
<td>JJJ</td>
</tr>
</table>
<input type="button" onclick="javascript:method1('tableExcel');" value=" EXCEL">
<input type="button" onclick="javascript:method2('tableExcel');" value=" EXCEL">
<input type="button" onclick="javascript:getXlsFromTbl('tableExcel',null);" value=" EXCEL">
<SCRIPT LANGUAGE="javascript">
function method1(tableid)
{// EXCEL
var curTbl = document.getElementById(tableid);
var oXL = new ActiveXObject("Excel.Application");
// AX excel
var oWB = oXL.Workbooks.Add();
// workbook
var oSheet = oWB.ActiveSheet;
// sheet
var sel = document.body.createTextRange();
sel.moveToElementText(curTbl);
// TextRange
sel.select();
// TextRange
sel.execCommand("Copy");
// TextRange
oSheet.Paste();
// EXCEL
oXL.Visible = true;
// excel
}
function method2(tableid) // EXCEL
{
var curTbl = document.getElementById(tableid);
var oXL = new ActiveXObject("Excel.Application");
// AX excel
var oWB = oXL.Workbooks.Add();
// workbook
var oSheet = oWB.ActiveSheet;
// sheet
var Lenr = curTbl.rows.length;
//
for (i = 0; i < Lenr; i++)
{
var Lenc = curTbl.rows(i).cells.length;
//
for (j = 0; j < Lenc; j++)
{
oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText;
//
}
}
oXL.Visible = true;
// excel
}
function getXlsFromTbl(inTblId, inWindow) {
try {
var allStr = "";
var curStr = "";
//alert("getXlsFromTbl");
if (inTblId != null && inTblId != "" && inTblId != "null") {
curStr = getTblData(inTblId, inWindow);
}
if (curStr != null) {
allStr += curStr;
}
else {
alert(" !");
return;
}
var fileName = getExcelFileName();
doFileExport(fileName, allStr);
}
catch(e) {
alert(" :" + e.name + "->" + e.description + "!");
}
}
//---------------------------------------------
function getTblData(inTbl, inWindow) {
var rows = 0;
//alert("getTblData is " + inWindow);
var tblDocument = document;
if (!!inWindow && inWindow != "") {
if (!document.all(inWindow)) {
return null;
}
else {
tblDocument = eval(inWindow).document;
}
}
var curTbl = tblDocument.getElementById(inTbl);
var outStr = "";
if (curTbl != null) {
for (var j = 0; j < curTbl.rows.length; j++) {
//alert("j is " + j);
for (var i = 0; i < curTbl.rows[j].cells.length; i++) {
//alert("i is " + i);
if (i == 0 && rows > 0) {
outStr += " ";
rows -= 1;
}
outStr += curTbl.rows[j].cells[i].innerText + " ";
if (curTbl.rows[j].cells[i].colSpan > 1) {
for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) {
outStr += " ";
}
}
if (i == 0) {
if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) {
rows = curTbl.rows[j].cells[i].rowSpan - 1;
}
}
}
outStr += " ";
}
}
else {
outStr = null;
alert(inTbl + " !");
}
return outStr;
}
function getExcelFileName() {
var d = new Date();
var curYear = d.getYear();
var curMonth = "" + (d.getMonth() + 1);
var curDate = "" + d.getDate();
var curHour = "" + d.getHours();
var curMinute = "" + d.getMinutes();
var curSecond = "" + d.getSeconds();
if (curMonth.length == 1) {
curMonth = "0" + curMonth;
}
if (curDate.length == 1) {
curDate = "0" + curDate;
}
if (curHour.length == 1) {
curHour = "0" + curHour;
}
if (curMinute.length == 1) {
curMinute = "0" + curMinute;
}
if (curSecond.length == 1) {
curSecond = "0" + curSecond;
}
var fileName = "leo_zhang" + "_" + curYear + curMonth + curDate + "_"
+ curHour + curMinute + curSecond + ".csv";
//alert(fileName);
return fileName;
}
function doFileExport(inName, inStr) {
var xlsWin = null;
if (!!document.all("glbHideFrm")) {
xlsWin = glbHideFrm;
}
else {
var width = 6;
var height = 4;
var openPara = "left=" + (window.screen.width / 2 - width / 2)
+ ",top=" + (window.screen.height / 2 - height / 2)
+ ",scrollbars=no,width=" + width + ",height=" + height;
xlsWin = window.open("", "_blank", openPara);
}
xlsWin.document.write(inStr);
xlsWin.document.close();
xlsWin.document.execCommand('Saveas', true, inName);
xlsWin.close();
}
</SCRIPT>
</BODY>
</HTML>
CSDN , :http://blog.csdn.net/zhongzhengfeng/archive/2009/04/20/4094831.aspx