| JavaScript Document Call method var test=new pagetoexcel ("Data", 0,255, "test. xls");//table ID, line start, last line color, saved file name Test. Createexcel (FALSE); Test. Exec (); Test. SaveAs (); Test. Closeexcel (); Lastrowcolor 0 Black 255 Red // function Pagetoexcel (tableid,firstrow,lastrowcolor,saveasname) { this.lastrowcolor=lastrowcolor== ""? 0:lastrowcolor; var today=new Date (); This.saveasname= (saveasname== "" "" Today.getyear () + "year" + (Today.getmonth () +1) + "month" +today.getdate () + "day. xls": Saveasname); This.tableid=tableid; This.table=document.getelementbyid (This.tableid);//Exported Table object this.rows=this.table.rows.length;//the number of table rows exported This.colsumcols=this.table.rows (0). cells.length;//The total number of columns in the first row This.fromrow=firstrow; this.begincol=0; Number of starting columns This.cols=this.colsumcols; This.oxl=null; This.owb=null; This.osheet=null; This.rowspans=1; Row merge This.colspans=1; Column Merge This.colsname={0: "A", 1: "B", 2: "C", 3: "D", 4: "E", 5: "F", 6: "G", 7: "H", 8: "I", 9: "J", "K", One: "L", One: "M", "N", "O", "P": "Q", "" R "," S "," T "," U "," V "," W "," X "," Y "," "Z"}; " } Pagetoexcel.prototype.deleteexcelcols=function (notshowcollist) {//array notshowcollist this.notshowcollist=notshowcollist;//does not display the column collection, 1,2,3,1 Delete a column in Excel var m=0; for (Var i=0;i<notshowcollist.length;i++) { if (i>0) { m++; } var temp=notshowcollist[i]-m; var index=this.colsname[temp]; This.oSheet.Columns (Index). delete;//Delete } m=0; } Pagetoexcel.prototype.createexcel=function (excelvisible) { try{ THIS.OXL = new ActiveXObject ("Excel.Application"); Create a should object this.oXL.Visible = excelvisible; THIS.OWB = This.oxl. Workbooks.Add ()//Create a new Excel workbook This.osheet = this.owb.activesheet;//Specifies that the worksheet to be written to the content is the active worksheet Do not show grid lines This.oxl.activewindow.displaygridlines=false; }catch (e) { Alert ("Verify that the non-green version of the excel! is installed "+e.description); Closeexcel (); } } Pagetoexcel.prototype.closeexcel=function () { This.oXL.DisplayAlerts = false; This.oXL.Quit (); THIS.OXL = null; This.owb=null; This.osheet=null; } Pagetoexcel.prototype.changeelementtolabel=function (elementobj) { var gettext= ""; try{ var childres=elementobj.childnodes; }catch (e) {return GetText} if (childres.length<=0) return GetText; for (Var i=0;i<childres.length;i++) { Try{if (childres[i].style.display== "None" | | Childres[i].type.tolowercase () = = "hidden") {continue}} catch (e) {} try{ Switch (Childres[i].nodename.tolowercase ()) { Case "#text": GetText +=childres[i].nodevalue; Break Case "BR": GetText + = "n"; Break Case "IMG": GetText + = ""; Break Case "SELECT": GetText +=childres[i].options[childres[i].selectedindex].innertext; Break Case "Input": if (childres[i].type.tolowercase () = = "Submit" | | Childres[i].type.tolowercase () = = "button") { GetText + = ""; }else if (childres[i].type.tolowercase () = = "textarea") { GetText +=childres[i].innertext; }else{ GetText +=childres[i].value; } Break Default: GetText = this. Changeelementtolabel (Childres[i]); Break } }catch (e) {} } return GetText; } Pagetoexcel.prototype.saveas=function () { Save try{ This.oXL.Visible =true; var fname = This.oXL.Application.GetSaveAsFilename (this.saveasname, "Excel spreadsheets (*.xls), *.xls"); if (fname) { This.oWB.SaveAs (fname); This.oXL.Visible =false; } }catch (e) {}; } Pagetoexcel.prototype.exec=function () { Look for the number of columns, considering that the first row may exist for (var i=0; i<this.colsumcols;i++) { var tmpcolspan = this.table.rows (0). Cells (i). ColSpan; if (tmpcolspan>1) { This.cols + = tmpcolspan-1; } } Defines 2-D container data, 1: Rows, 2: Columns, values (0 can be populated, 1 are filled) var container=new Array (this.rows); for (Var i=0;i<this.rows;i++) { Container[i]=new Array (This.cols); for (j=0;j<this.cols;j++) { container[i][j]=0; } } //Convert all cells to text to avoid non-numeric columns being automatically turned into scientific notation and missing prefixes of 0 This.oSheet.Range (This.oSheet.Cells ( this.fromrow+1,1), This.oSheet.Cells (This.fromrow+this.rows,this.cols)). NumberFormat = "@"; //looping rows for (i=0;i<this.rows;i++) { //Looping columns & nbsp for (j=0;j<this.cols;j++) { //Find start column for (k=j;k< this.cols;k++) { if (container[i][k]==0) { This.begincol=k; k=this.cols;//Exit Loops } } //try{ //Assignment //Here corresponding to the type of label , replace the related parameter this.oSheet.Cells (i+1+this.fromrow,this.begincol+1). Value = this. Changeelementtolabel (This.table.rows (i). Cells (j)); //Calculate merge Columns try{ This.colspans = This.table.rows (i). Cells (j). ColSpan; }catch (e) { this.colspans=0 } if (this.colspans>1) { //merge This.oSheet.Range (This.oSheet.Cells (i+1+this.fromrow,this.begincol+1), This.oSheet.Cells (I+1+this.fromrow,this.begincol+this.colspans)). Merge (); } //fill in the current table position in the corresponding container for (k=0; k<this.colspans;k++) { container[i][this.begincol+k]= 1; } /Calculate merge rows try{ This.rowspans = this.table.rows (i). Cells (j). RowSpan; }catch (e) { This.rowspans = 0; } if (this.rowspans>1) {//row merge This.oSheet.Range ( This.oSheet.Cells (i+1+this.fromrow,this.begincol+1), This.oSheet.Cells (I+this.rowspans+this.fromrow, This.begincol+this.colspans)). Merge (); //fill in the current table position in the corresponding container for (k=1 k< this.rowspans;k++) {//Since line No. 0 is already populated by the Colspans corresponding code, this is done from line 1th for (l=0;l< this.colspans;l++) { container[i+k][this.begincol+l]=1; } } } //If the Start column + merge column is already equal to the number of columns, there is no need to recycle the HTML table if (this.begincol+ This.colspans>=this.cols) J=this.cols; } if (i==0) { Title bar This.oSheet.Range (This.oSheet.Cells (1,1), This.oSheet.Cells (1,1)). font.size=20; This.oSheet.Range (This.oSheet.Cells (1,1), This.oSheet.Cells (1,1)). Font.Bold = true; This.oSheet.Range (This.oSheet.Cells (1,1), This.oSheet.Cells (1,1)). HorizontalAlignment =-4108; Center This.oSheet.Range (This.oSheet.Cells (1,1), This.oSheet.Cells (1,1)). Rows.rowheight = 40; } Auto Adjust Row height } Whether the last line is empty try{ This.oSheet.Range (This.oSheet.Cells (this.rows,1), This.oSheet.Cells (this.rows,1)). Font.color=this.lastrowcolor; }catch (e) {} This.oSheet.Range (This.oSheet.Cells (this.fromrow+2,1), This.oSheet.Cells (This.fromrow+this.rows,this.cols)). rows.rowheight=20; This.oSheet.Range (This.oSheet.Cells (this.fromrow+2,1), This.oSheet.Cells (This.fromrow+this.rows,this.cols)). font.size=10; Wrap Line This.oSheet.Range (This.oSheet.Cells (this.fromrow+2,1), This.oSheet.Cells (This.fromrow+this.rows,this.cols)). WrapText = true; Automatically adjust column widths This.oSheet.Range (This.oSheet.Cells (this.fromrow+1,1), This.oSheet.Cells (This.fromrow+this.rows,this.cols)). Columns.AutoFit (); Dot Dash This.oSheet.Range (This.oSheet.Cells (this.fromrow+1,1), This.oSheet.Cells (This.fromrow+this.rows,this.cols)). Borders.LineStyle =-4118; return this.rows; } |