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 };
程序员的基础教程:菜鸟程序员
分类:
javascript,js框架
, 功能封装
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现