<script language="JavaScript" type="text/javascript">
/* * 默認(rèn)轉(zhuǎn)換實(shí)現(xiàn)函數(shù),如果需要其他功能,需自行擴(kuò)展
* 參數(shù):
* tableID : HTML中Table對(duì)象id屬性值
* 詳細(xì)用法參見以下 TableToExcel 對(duì)象定義
*/
function saveAsExcel(tableID){
var tb = new TableToExcel(tableID);
tb.setFontStyle("Courier New");
tb.setFontSize(10);
tb.setTableBorder(2);
tb.setColumnWidth(7);
tb.isLineWrap(true);
tb.getExcelFile();
}
/** 功能:HTML中Table對(duì)象轉(zhuǎn)換為Excel通用對(duì)象.
* 參數(shù):tableID HTML中Table對(duì)象的ID屬性值
* 說明:
* 能適應(yīng)復(fù)雜的HTML中Table對(duì)象的自動(dòng)轉(zhuǎn)換,能夠自動(dòng)根據(jù)行列擴(kuò)展信息
* 合并Excel中的單元格,客戶端需要安裝有Excel
* 詳細(xì)的屬性、方法引用說明參見:Excel的Microsoft Excel Visual Basic參考
* 示范:
* var tb = new TableToExcel('demoTable');
* tb.setFontStyle("Courier New");
* tb.setFontSize(10); //推薦取值10
* tb.setFontColor(6); //一般情況不用設(shè)置
* tb.setBackGround(4); //一般情況不用設(shè)置
* tb.setTableBorder(2); //推薦取值2
* tb.setColumnWidth(10); //推薦取值10
* tb.isLineWrap(false);
* tb.isAutoFit(true);
* * tb.getExcelFile();
* 如果設(shè)置了單元格自適應(yīng),則設(shè)置單元格寬度無效
* 版本:1.0*/
function TableToExcel(tableID) {
this.tableBorder = -1; //邊框類型,-1沒有邊框 可取1/2/3/4
this.backGround = 0; //背景顏色:白色 可取調(diào)色板中的顏色編號(hào) 1/2/3/4....
this.fontColor = 1; //字體顏色:黑色
this.fontSize = 10; //字體大小
this.fontStyle = "宋體"; //字體類型
this.rowHeight = 20; //行高
this.columnWidth = -1; //列寬
this.lineWrap = true; //是否自動(dòng)換行
this.textAlign = -4108; //內(nèi)容對(duì)齊方式 默認(rèn)為居中
this.autoFit = true; //是否自適應(yīng)寬度
this.tableID = tableID;
}
TableToExcel.prototype.setTableBorder = function (excelBorder) {
this.tableBorder = excelBorder ;
};
TableToExcel.prototype.setBackGround = function (excelColor) {
this.backGround = excelColor;
};
TableToExcel.prototype.setFontColor = function (excelColor) {
this.fontColor = excelColor;
};
TableToExcel.prototype.setFontSize = function (excelFontSize) {
this.fontSize = excelFontSize;
};
TableToExcel.prototype.setFontStyle = function (excelFont) {
this.fontStyle = excelFont;
};
TableToExcel.prototype.setRowHeight = function (excelRowHeight) {
this.rowHeight = excelRowHeight;
};
TableToExcel.prototype.setColumnWidth = function (excelColumnWidth) {
this.columnWidth = excelColumnWidth;
};
TableToExcel.prototype.isLineWrap = function (lineWrap) {
if (lineWrap == false || lineWrap == true) {
this.lineWrap = lineWrap;
}
};
TableToExcel.prototype.setTextAlign = function (textAlign) {
this.textAlign = textAlign;
};
TableToExcel.prototype.isAutoFit = function(autoFit){
if(autoFit == true || autoFit == false)this.autoFit = autoFit ;
}
//文件轉(zhuǎn)換主函數(shù)
TableToExcel.prototype.getExcelFile = function () {
var jXls, myWorkbook, myWorksheet, myHTMLTableCell, myExcelCell, myExcelCell2;
var myCellColSpan, myCellRowSpan;
try {
jXls = new ActiveXObject('Excel.Application');
}catch (e) {
alert("無法啟動(dòng)Excel!\n\n如果您確信您的電腦中已經(jīng)安裝了Excel,"+"那么請(qǐng)調(diào)整IE的安全級(jí)別。\n\n具體操作:\n\n"+"工具 → Internet選項(xiàng) → 安全 → 自定義級(jí)別 → 對(duì)沒有標(biāo)記為安全的ActiveX進(jìn)行初始化和腳本運(yùn)行 → 啟用");
return false;
}
jXls.Visible = true;
myWorkbook = jXls.Workbooks.Add();
jXls.DisplayAlerts = false;
myWorkbook.Worksheets(3).Delete();
myWorkbook.Worksheets(2).Delete();
jXls.DisplayAlerts = true;
myWorksheet = myWorkbook.ActiveSheet;
var readRow = 0, readCol = 0;var totalRow = 0, totalCol = 0;
var tabNum = 0;
//設(shè)置行高、列寬
if(this.columnWidth != -1)
myWorksheet.Columns.ColumnWidth = this.columnWidth;
else
myWorksheet.Columns.ColumnWidth = 7;
if(this.rowHeight != -1)
myWorksheet.Rows.RowHeight = this.rowHeight ;
//搜索需要轉(zhuǎn)換的Table對(duì)象,獲取對(duì)應(yīng)行、列數(shù)
var obj = document.all.tags("table");
for (x = 0; x < obj.length; x++) {
if (obj[x].id == this.tableID) {
tabNum = x;
totalRow = obj[x].rows.length;
for (i = 0; i < obj[x].rows[0].cells.length; i++) {
myHTMLTableCell = obj[x].rows(0).cells(i);
myCellColSpan = myHTMLTableCell.colSpan;
totalCol = totalCol + myCellColSpan;
}
}
}
//開始構(gòu)件模擬表格
var excelTable = new Array();
for (i = 0; i <= totalRow; i++) {
excelTable[i] = new Array();
for (t = 0; t <= totalCol; t++) {
excelTable[i][t] = false;
}
}
//開始轉(zhuǎn)換表格
for (z = 0; z < obj[tabNum].rows.length; z++) {
readRow = z + 1;
readCol = 0;
for (c = 0; c < obj[tabNum].rows(z).cells.length; c++) {
myHTMLTableCell = obj[tabNum].rows(z).cells(c);
myCellColSpan = myHTMLTableCell.colSpan;
myCellRowSpan = myHTMLTableCell.rowSpan;
for (y = 1; y <= totalCol; y++) {
if (excelTable[readRow][y] == false) {
readCol = y;
break;
}
}
if (myCellColSpan * myCellRowSpan > 1) {
myExcelCell = myWorksheet.Cells(readRow, readCol);
myExcelCell2 = myWorksheet.Cells(readRow + myCellRowSpan - 1, readCol + myCellColSpan - 1);
myWorksheet.Range(myExcelCell, myExcelCell2).Merge();
myExcelCell.HorizontalAlignment = this.textAlign;
myExcelCell.Font.Size = this.fontSize;
myExcelCell.Font.Name = this.fontStyle;
myExcelCell.wrapText = this.lineWrap;
myExcelCell.Interior.ColorIndex = this.backGround;
myExcelCell.Font.ColorIndex = this.fontColor;
if(this.tableBorder != -1){
myWorksheet.Range(myExcelCell, myExcelCell2).Borders(1).Weight = this.tableBorder ;
myWorksheet.Range(myExcelCell, myExcelCell2).Borders(2).Weight = this.tableBorder ;
myWorksheet.Range(myExcelCell, myExcelCell2).Borders(3).Weight = this.tableBorder ;
myWorksheet.Range(myExcelCell, myExcelCell2).Borders(4).Weight = this.tableBorder ;
}
myExcelCell.Value = myHTMLTableCell.innerText;
for (row = readRow; row <= myCellRowSpan + readRow - 1; row++) {
for (col = readCol; col <= myCellColSpan + readCol - 1; col++) {
excelTable[row][col] = true;
}
}
readCol = readCol + myCellColSpan;
} else {
myExcelCell = myWorksheet.Cells(readRow, readCol);
myExcelCell.Value = myHTMLTableCell.innerText;
myExcelCell.HorizontalAlignment = this.textAlign;
myExcelCell.Font.Size = this.fontSize;
myExcelCell.Font.Name = this.fontStyle;
myExcelCell.wrapText = this.lineWrap;
myExcelCell.Interior.ColorIndex = this.backGround;
myExcelCell.Font.ColorIndex = this.fontColor;
if(this.tableBorder != -1){
myExcelCell.Borders(1).Weight = this.tableBorder ;
myExcelCell.Borders(2).Weight = this.tableBorder ;
myExcelCell.Borders(3).Weight = this.tableBorder ;
myExcelCell.Borders(4).Weight = this.tableBorder ;
}
excelTable[readRow][readCol] = true;readCol = readCol + 1;
}
}
}
if(this.autoFit == true)
myWorksheet.Columns.AutoFit;
jXls.UserControl = true;
jXls = null;
myWorkbook = null;
myWorksheet = null;
};
</script>
該文章在 2011/10/10 10:31:49 編輯過