转自: Excelに保存< ;/head> シーケンス 名前 年龄 特性别 01 张三 18 女 02 李四< /td> 20 gt;女 03 王五 22 男 04 张飞100男 table><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 中テーブルオブジェクトの ID プロパティ值<br>* 説明: <br>* 有効な HTML 中テーブルオブジェクトの自動移動、列展情報に基づいて自動実行可能<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(expelrowheight){this.rowheight = expelrowheight; <br>rreeee TableToExcel.prototype.isLineWrap = function (lineWrap) {<br> if (lineWrap == false || lineWrap == true) {</p> this.lineWrap = lineWrap;<p> }<br>};<br></p> <pre><br></pre>TableToExcel.prototype.setTextAlign = function (textAlign ) {<p> this.textAlign = textAlign;<br>};<br></p> <pre><br></pre> <br>TableToExcel.prototype.isAutoFit = function (autoFit) {<br> if (autoFit == true || autoFit == false) this.autoFit = autoFit;<p>}<br> //文件转换主関数<br>TableToExcel.prototype.getExcelFile = function () {</p> var jXls, myWorkbook, myWorksheet, myHTMLTableCell, myExcelCell, myExcelCell2;<p> var myCellColSpan, myCellRowSpan;<br></p> <pre><br></pre> <br>try {<br> jXls = new ActiveXObject('Excel .Application');<br> }<br> catch (e) {<br> alert("無法启アニメーションExcel!nn" + e.message +<br>"nn如果您确信您的電脑中已经装着了Excel," +"那么请调整IE の安全レベル。nn 具体的な操作:nn" +<p>"ツール → インターネット選択 → 安全 → 自動設定 → 安全な ActiveX の初期化とスクリプト実行 → 実行");<br> return false;<br> }<br></p> <pre><br></pre> <br>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; myWorksheet = myWorkbook.ActiveSheet;<p></p> <pre><br></pre> <br>var readRow = 0, readCol = 0;<br> var totalRow = 0, totalCol = 0;<br> var tabNum = 0;<br><pre><br></pre> <br>//設置行高、列宽<br> if (this.columnWidth != -1) myWorksheet.Columns.ColumnWidth = this.columnWidth;<p> else<br> myWorksheet.Columns.ColumnWidth = 7;<br> if (this.rowHeight != -1)</p> myWorksheet.Rows .RowHeight = this.rowHeight;<p></p> <pre><br></pre> <br>//検索必要转换のTable对象、获取对应行、列数<br> var obj = document.all.tags("table");<br> for (x = 0; ×< obj.length; x++) {<br> if (obj[x].id == this.tableID) {<br> tabNum = x;<br> totalRow = obj[x].rows.length; for (i = 0; i < obj[x] .rows[0].cells.length; i++) {<p> myHTMLTableCell = obj[x].rows(0).cells(i);<br> myCellColSpan = myHTMLTableCell.colSpan;<br> total Col = totalCol + myCellColSpan;<br> }<br> }<br> }<br></p> <pre><br></pre> <br>//开始构件模拟表格<br> var ExcelTable = new Array();<br> for (i = 0; i excelTable[i] = new Array();<br> for (t = 0; t excelTable[i][t] = false; }<p></p> <pre><br></pre> <p>//开始转换表格 <br> for (z = 0; z <obj z></obj> readRow = z + 1;<br> readCol = 0;<br> for (c = 0; < ; obj[tabNum].rows(z).cells.length; c++) {<br> myHTMLTableCell = obj[tabNum].rows(z).cells(c);<br> myCellColSpan = myHTMLTableCell.colSpan;<br> CellRowSpan = myHTMLTableCell.rowSpan; <br> for (y = 1; y 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, Cell2).Borders(4) ).Weight = this.tableBorder;<br> }</p> <pre><br></pre> <p>myExcelCell.Value = myHTMLTableCell.innerText;<br> for (row = readRow;行 for (col = readCol;col 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> my ExcelCell.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> </td> </tr> </tbody> </div>