转自: 保存到Excel 序列 名字 年龄 性别 01 张三 18 女 02 李四 20 女 03 王五 22 男 04 张飞 100 男 <br> /* <br>* 默认转换实现函数,如果需要其他功能,需自行扩展<br>* 参数:<br>* tableID : HTML中Table对象id属性值<br>* 详细用法参见以下 TableToExcel 对象定义 <br>*/<br>function saveAsExcel(tableID) {<br> var tb = new TableToExcel(tableID);<br> tb.setFontStyle("Courier New");<br> tb.setFontSize(10);<br> tb.setTableBorder(2);<br> tb.setColumnWidth(7);<br> tb.isLineWrap(true);<br> tb.getExcelFile();<br>}</p> <pre><br></pre> <p>/*<br>* 功能:HTML中Table对象转换为Excel通用对象.<br>* 参数:tableID HTML中Table对象的ID属性值<br>* 说明:<br>* 能适应复杂的HTML中Table对象的自动转换,能够自动根据行列扩展信息<br>* 合并Excel中的单元格,客户端需要安装有Excel<br>* 详细的属性、方法引用说明参见:Excel的Microsoft Excel Visual Basic参考<br>* 示范:<br>* var tb = new TableToExcel('demoTable');<br>* tb.setFontStyle("Courier New");<br>* tb.setFontSize(10); //推荐取值10<br>* tb.setFontColor(6); //一般情况不用设置<br>* tb.setBackGround(4); //一般情况不用设置<br>* tb.setTableBorder(2); //推荐取值2<br>* tb.setColumnWidth(10); //推荐取值10<br>* tb.isLineWrap(false);<br>* tb.isAutoFit(true);<br>* <br>* tb.getExcelFile();<br>* 如果设置了单元格自适应,则设置单元格宽度无效<br>* 版本:1.0<br>* BUG提交:QQ:18234348 或者 <br>*/<br>function TableToExcel(tableID) {<br> this.tableBorder = -1; //边框类型,-1没有边框 可取1/2/3/4<br> this.backGround = 0; //背景颜色:白色 可取调色板中的颜色编号 1/2/3/4....<br> this.fontColor = 1; //字体颜色:黑色<br> this.fontSize = 10; //字体大小<br> this.fontStyle = "宋体"; //字体类型<br> this.rowHeight = -1; //行高<br> this.columnWidth = -1; //列宽<br> this.lineWrap = true; //是否自动换行<br> this.textAlign = -4108; //内容对齐方式 默认为居中<br> this.autoFit = false; //是否自适应宽度<br> this.tableID = tableID;<br>}</p> <pre><br></pre> <p>TableToExcel.prototype.setTableBorder = function (excelBorder) {<br> this.tableBorder = excelBorder;<br>};</p> <pre><br></pre> <p>TableToExcel.prototype.setBackGround = function (excelColor) {<br> this.backGround = excelColor;<br>};</p> <pre><br></pre> <p>TableToExcel.prototype.setFontColor = function (excelColor) {<br> this.fontColor = excelColor;<br>};</p> <pre><br></pre> <p>TableToExcel.prototype.setFontSize = function (excelFontSize) {<br> this.fontSize = excelFontSize;<br>};</p> <pre><br></pre> <p>TableToExcel.prototype.setFontStyle = function (excelFont) {<br> this.fontStyle = excelFont;<br>};</p> <pre><br></pre> <p>TableToExcel.prototype.setRowHeight = function (excelRowHeight) {<br> this.rowHeight = excelRowHeight;<br>};</p> <pre><br></pre> <p>TableToExcel.prototype.setColumnWidth = function (excelColumnWidth) {<br> this.columnWidth = excelColumnWidth;<br>};</p> <pre><br></pre> <p>TableToExcel.prototype.isLineWrap = function (lineWrap) {<br> if (lineWrap == false || lineWrap == true) {<br> this.lineWrap = lineWrap;<br> }<br>};</p> <pre><br></pre> <p>TableToExcel.prototype.setTextAlign = function (textAlign) {<br> this.textAlign = textAlign;<br>};</p> <pre><br></pre> <p>TableToExcel.prototype.isAutoFit = function (autoFit) {<br> if (autoFit == true || autoFit == false)<br> this.autoFit = autoFit;<br>}<br>//文件转换主函数<br>TableToExcel.prototype.getExcelFile = function () {<br> var jXls, myWorkbook, myWorksheet, myHTMLTableCell, myExcelCell, myExcelCell2;<br> var myCellColSpan, myCellRowSpan;</p> <pre><br></pre> <p>try {<br> jXls = new ActiveXObject('Excel.Application');<br> }<br> catch (e) {<br> alert("无法启动Excel!\n\n" + e.message +<br>"\n\n如果您确信您的电脑中已经安装了Excel," +<br>"那么请调整IE的安全级别。\n\n具体操作:\n\n" +<br>"工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用");<br> return false;<br> }</p> <pre><br></pre> <p>jXls.Visible = true;<br> myWorkbook = jXls.Workbooks.Add();<br> jXls.DisplayAlerts = false;<br> myWorkbook.Worksheets(3).Delete();<br> myWorkbook.Worksheets(2).Delete();<br> jXls.DisplayAlerts = true;<br> myWorksheet = myWorkbook.ActiveSheet;</p> <pre><br></pre> <p>var readRow = 0, readCol = 0;<br> var totalRow = 0, totalCol = 0;<br> var tabNum = 0;</p> <pre><br></pre> <p>//设置行高、列宽<br> if (this.columnWidth != -1)<br> myWorksheet.Columns.ColumnWidth = this.columnWidth;<br> else<br> myWorksheet.Columns.ColumnWidth = 7;<br> if (this.rowHeight != -1)<br> myWorksheet.Rows.RowHeight = this.rowHeight;</p> <pre><br></pre> <p>//搜索需要转换的Table对象,获取对应行、列数<br> var obj = document.all.tags("table");<br> for (x = 0; x < obj.length; x++) {<br/> if (obj[x].id == this.tableID) {<br/> tabNum = x;<br/> totalRow = obj[x].rows.length;<br/> for (i = 0; i < obj[x].rows[0].cells.length; i++) {<br/> myHTMLTableCell = obj[x].rows(0).cells(i);<br/> myCellColSpan = myHTMLTableCell.colSpan;<br/> totalCol = totalCol + myCellColSpan;<br/> }<br/> }<br/> }</p><pre><br/></pre><p>//开始构件模拟表格<br/> var excelTable = new Array();<br/> for (i = 0; i <= totalRow; i++) {<br/> excelTable[i] = new Array();<br/> for (t = 0; t <= totalCol; t++) {<br/> excelTable[i][t] = false;<br/> }<br/> }</p><pre><br/></pre><p>//开始转换表格 <br/> for (z = 0; z < obj[tabNum].rows.length; z++) {<br/> readRow = z + 1;<br/> readCol = 0;<br/> for (c = 0; c < obj[tabNum].rows(z).cells.length; c++) {<br/> myHTMLTableCell = obj[tabNum].rows(z).cells(c);<br/> myCellColSpan = myHTMLTableCell.colSpan;<br/> myCellRowSpan = myHTMLTableCell.rowSpan;<br/> for (y = 1; y <= totalCol; y++) {<br/> if (excelTable[readRow][y] == false) {<br/> readCol = y;<br/> break;<br/> }<br/> }<br/> if (myCellColSpan * myCellRowSpan > 1) {<br> myExcelCell = myWorksheet.Cells(readRow, readCol);<br> myExcelCell2 = myWorksheet.Cells(readRow + myCellRowSpan - 1, readCol + myCellColSpan - 1);<br> myWorksheet.Range(myExcelCell, myExcelCell2).Merge();<br> myExcelCell.HorizontalAlignment = this.textAlign;<br> myExcelCell.Font.Size = this.fontSize;<br> myExcelCell.Font.Name = this.fontStyle;<br> myExcelCell.wrapText = this.lineWrap;<br> myExcelCell.Interior.ColorIndex = this.backGround;<br> myExcelCell.Font.ColorIndex = this.fontColor;<br> if (this.tableBorder != -1) {<br> myWorksheet.Range(myExcelCell, myExcelCell2).Borders(1).Weight = this.tableBorder;<br> myWorksheet.Range(myExcelCell, myExcelCell2).Borders(2).Weight = this.tableBorder;<br> myWorksheet.Range(myExcelCell, myExcelCell2).Borders(3).Weight = this.tableBorder;<br> myWorksheet.Range(myExcelCell, myExcelCell2).Borders(4).Weight = this.tableBorder;<br> }</p> <pre><br></pre> <p>myExcelCell.Value = myHTMLTableCell.innerText;<br> for (row = readRow; row <= myCellRowSpan + readRow - 1; row++) {<br/> for (col = readCol; col <= myCellColSpan + readCol - 1; col++) {<br/> excelTable[row][col] = true;<br/> }<br/> }</p><pre><br/></pre><p>readCol = readCol + myCellColSpan;<br/> } else {<br/> myExcelCell = myWorksheet.Cells(readRow, readCol);<br/> myExcelCell.Value = myHTMLTableCell.innerText;<br/> myExcelCell.HorizontalAlignment = this.textAlign;<br/> myExcelCell.Font.Size = this.fontSize;<br/> myExcelCell.Font.Name = this.fontStyle;<br/> myExcelCell.wrapText = this.lineWrap;<br/> myExcelCell.Interior.ColorIndex = this.backGround;<br/> myExcelCell.Font.ColorIndex = this.fontColor;<br/> if (this.tableBorder != -1) {<br/> myExcelCell.Borders(1).Weight = this.tableBorder;<br/> myExcelCell.Borders(2).Weight = this.tableBorder;<br/> myExcelCell.Borders(3).Weight = this.tableBorder;<br/> myExcelCell.Borders(4).Weight = this.tableBorder;<br/> }<br/> excelTable[readRow][readCol] = true;<br/> readCol = readCol + 1;<br/> }<br/> }<br/> }<br/> if (this.autoFit == true)<br/> myWorksheet.Columns.AutoFit;</p><pre><br/></pre><p>jXls.UserControl = true;<br/> jXls = null;<br/> myWorkbook = null;<br/> myWorksheet = null;<br/>};<br/>