1 /* 2 * 默認轉換實現函數,如果需要其他功能,需自行擴展 3 * 參數: 4 * tableID : HTML中Table對象id屬性值 5 * 詳細用法參見以下 TableToExcel 對象定義 6 */ 7 function saveAsExcel(tableID) { 8 var tb = new TableToExcel(tableID); 9 tb.setFontStyle("Courier New"); 10 tb.setFontSize(10); 11 tb.setTableBorder(2); 12 tb.setColumnWidth(7); 13 tb.isLineWrap(true); 14 tb.getExcelFile(); 15 } 16 17 /* 18 * 功能:HTML中Table對象轉換為Excel通用對象. 19 * 參數:tableID HTML中Table對象的ID屬性值 20 * 說明: 21 * 能適應復雜的HTML中Table對象的自動轉換,能夠自動根據行列擴展信息 22 * 合並Excel中的單元格,客戶端需要安裝有Excel 23 * 詳細的屬性、方法引用說明參見:Excel的Microsoft Excel Visual Basic參考 24 * 示范: 25 * var tb = new TableToExcel('demoTable'); 26 * tb.setFontStyle("Courier New"); 27 * tb.setFontSize(10); //推薦取值10 28 * tb.setFontColor(6); //一般情況不用設置 29 * tb.setBackGround(4); //一般情況不用設置 30 * tb.setTableBorder(2); //推薦取值2 31 * tb.setColumnWidth(10); //推薦取值10 32 * tb.isLineWrap(false); 33 * tb.isAutoFit(true); 34 * 35 * tb.getExcelFile(); 36 * 如果設置了單元格自適應,則設置單元格寬度無效 37 * 版本:1.0 38 * BUG提交:QQ:18234348 或者 http://jeva.bokee.com 39 */ 40 function TableToExcel(tableID) { 41 this.tableBorder = -1; //邊框類型,-1沒有邊框 可取1/2/3/4 42 this.backGround = 0; //背景顏色:白色 可取調色板中的顏色編號 1/2/3/4.... 43 this.fontColor = 1; //字體顏色:黑色 44 this.fontSize = 10; //字體大小 45 this.fontStyle = "宋體"; //字體類型 46 this.rowHeight = -1; //行高 47 this.columnWidth = -1; //列寬 48 this.lineWrap = true; //是否自動換行 49 this.textAlign = -4108; //內容對齊方式 默認為居中 50 this.autoFit = false; //是否自適應寬度 51 this.tableID = tableID; 52 } 53 54 TableToExcel.prototype.setTableBorder = function (excelBorder) { 55 this.tableBorder = excelBorder; 56 }; 57 58 TableToExcel.prototype.setBackGround = function (excelColor) { 59 this.backGround = excelColor; 60 }; 61 62 TableToExcel.prototype.setFontColor = function (excelColor) { 63 this.fontColor = excelColor; 64 }; 65 66 TableToExcel.prototype.setFontSize = function (excelFontSize) { 67 this.fontSize = excelFontSize; 68 }; 69 70 TableToExcel.prototype.setFontStyle = function (excelFont) { 71 this.fontStyle = excelFont; 72 }; 73 74 TableToExcel.prototype.setRowHeight = function (excelRowHeight) { 75 this.rowHeight = excelRowHeight; 76 }; 77 78 TableToExcel.prototype.setColumnWidth = function (excelColumnWidth) { 79 this.columnWidth = excelColumnWidth; 80 }; 81 82 TableToExcel.prototype.isLineWrap = function (lineWrap) { 83 if (lineWrap == false || lineWrap == true) { 84 this.lineWrap = lineWrap; 85 } 86 }; 87 88 TableToExcel.prototype.setTextAlign = function (textAlign) { 89 this.textAlign = textAlign; 90 }; 91 92 TableToExcel.prototype.isAutoFit = function (autoFit) { 93 if (autoFit == true || autoFit == false) 94 this.autoFit = autoFit; 95 } 96 //文件轉換主函數 97 TableToExcel.prototype.getExcelFile = function () { 98 var jXls, myWorkbook, myWorksheet, myHTMLTableCell, myExcelCell, myExcelCell2; 99 var myCellColSpan, myCellRowSpan; 100 101 try { 102 jXls = new ActiveXObject('Excel.Application'); 103 } 104 catch (e) { 105 alert("無法啟動Excel!\n\n" + e.message + 106 "\n\n如果您確信您的電腦中已經安裝了Excel," + 107 "那么請調整IE的安全級別。\n\n具體操作:\n\n" + 108 "工具 → Internet選項 → 安全 → 自定義級別 → 對沒有標記為安全的ActiveX進行初始化和腳本運行 → 啟用"); 109 return false; 110 } 111 112 jXls.Visible = true; 113 myWorkbook = jXls.Workbooks.Add(); 114 jXls.DisplayAlerts = false; 115 myWorkbook.Worksheets(3).Delete(); 116 myWorkbook.Worksheets(2).Delete(); 117 jXls.DisplayAlerts = true; 118 myWorksheet = myWorkbook.ActiveSheet; 119 120 var readRow = 0, readCol = 0; 121 var totalRow = 0, totalCol = 0; 122 var tabNum = 0; 123 124 //設置行高、列寬 125 if (this.columnWidth != -1) 126 myWorksheet.Columns.ColumnWidth = this.columnWidth; 127 else 128 myWorksheet.Columns.ColumnWidth = 7; 129 if (this.rowHeight != -1) 130 myWorksheet.Rows.RowHeight = this.rowHeight; 131 132 //搜索需要轉換的Table對象,獲取對應行、列數 133 var obj = document.all.tags("table"); 134 for (x = 0; x < obj.length; x++) { 135 if (obj[x].id == this.tableID) { 136 tabNum = x; 137 totalRow = obj[x].rows.length; 138 for (i = 0; i < obj[x].rows[0].cells.length; i++) { 139 myHTMLTableCell = obj[x].rows(0).cells(i); 140 myCellColSpan = myHTMLTableCell.colSpan; 141 totalCol = totalCol + myCellColSpan; 142 } 143 } 144 } 145 146 //開始構件模擬表格 147 var excelTable = new Array(); 148 for (i = 0; i <= totalRow; i++) { 149 excelTable[i] = new Array(); 150 for (t = 0; t <= totalCol; t++) { 151 excelTable[i][t] = false; 152 } 153 } 154 155 //開始轉換表格 156 for (z = 0; z < obj[tabNum].rows.length; z++) { 157 readRow = z + 1; 158 readCol = 0; 159 for (c = 0; c < obj[tabNum].rows(z).cells.length; c++) { 160 myHTMLTableCell = obj[tabNum].rows(z).cells(c); 161 myCellColSpan = myHTMLTableCell.colSpan; 162 myCellRowSpan = myHTMLTableCell.rowSpan; 163 for (y = 1; y <= totalCol; y++) { 164 if (excelTable[readRow][y] == false) { 165 readCol = y; 166 break; 167 } 168 } 169 if (myCellColSpan * myCellRowSpan > 1) { 170 myExcelCell = myWorksheet.Cells(readRow, readCol); 171 myExcelCell2 = myWorksheet.Cells(readRow + myCellRowSpan - 1, readCol + myCellColSpan - 1); 172 myWorksheet.Range(myExcelCell, myExcelCell2).Merge(); 173 myExcelCell.HorizontalAlignment = this.textAlign; 174 myExcelCell.Font.Size = this.fontSize; 175 myExcelCell.Font.Name = this.fontStyle; 176 myExcelCell.wrapText = this.lineWrap; 177 myExcelCell.Interior.ColorIndex = this.backGround; 178 myExcelCell.Font.ColorIndex = this.fontColor; 179 if (this.tableBorder != -1) { 180 myWorksheet.Range(myExcelCell, myExcelCell2).Borders(1).Weight = this.tableBorder; 181 myWorksheet.Range(myExcelCell, myExcelCell2).Borders(2).Weight = this.tableBorder; 182 myWorksheet.Range(myExcelCell, myExcelCell2).Borders(3).Weight = this.tableBorder; 183 myWorksheet.Range(myExcelCell, myExcelCell2).Borders(4).Weight = this.tableBorder; 184 } 185 186 myExcelCell.Value = myHTMLTableCell.innerText; 187 for (row = readRow; row <= myCellRowSpan + readRow - 1; row++) { 188 for (col = readCol; col <= myCellColSpan + readCol - 1; col++) { 189 excelTable[row][col] = true; 190 } 191 } 192 193 readCol = readCol + myCellColSpan; 194 } else { 195 myExcelCell = myWorksheet.Cells(readRow, readCol); 196 myExcelCell.Value = myHTMLTableCell.innerText; 197 myExcelCell.HorizontalAlignment = this.textAlign; 198 myExcelCell.Font.Size = this.fontSize; 199 myExcelCell.Font.Name = this.fontStyle; 200 myExcelCell.wrapText = this.lineWrap; 201 myExcelCell.Interior.ColorIndex = this.backGround; 202 myExcelCell.Font.ColorIndex = this.fontColor; 203 if (this.tableBorder != -1) { 204 myExcelCell.Borders(1).Weight = this.tableBorder; 205 myExcelCell.Borders(2).Weight = this.tableBorder; 206 myExcelCell.Borders(3).Weight = this.tableBorder; 207 myExcelCell.Borders(4).Weight = this.tableBorder; 208 } 209 excelTable[readRow][readCol] = true; 210 readCol = readCol + 1; 211 } 212 } 213 } 214 if (this.autoFit == true) 215 myWorksheet.Columns.AutoFit; 216 217 jXls.UserControl = true; 218 jXls = null; 219 myWorkbook = null; 220 myWorksheet = null; 221 };
程序員的基礎教程:菜鳥程序員