A complete guide to using javascript to generate reports in excel_javascript skills
WBOY
Release: 2016-05-16 16:50:12
Original
1629 people have browsed it
I recently worked on a project that used javascript to manipulate excel to generate reports. The following is an example marked with detailed annotations
< script language="javascript" type="text/javascript"> function MakeExcel(){ var i,j; try { var xls = new ActiveXObject ( "Excel.Application" ); } catch(e) { alert( "To print this table, you must install Excel spreadsheet software, and your browser must use "ActiveX Control", and your browser must allow execution controls.Please click [Help] to learn how to set up the browser!"); return ""; } xls.visible =true; //Set excel as visible var xlBook = xls.Workbooks.Add; var xlsheet = xlBook.Worksheets (1);
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,7)).mergecells=true; xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,7)).value="Card issuance record"; // xlsheet.Range(xlsheet.Cells(1,1),xlsheet. Cells(1,6)).Interior.ColorIndex=5;//Set the background color to blue // xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,6)). Font.ColorIndex=4;//Set the font color // xlsheet.Rows(1). Interior .ColorIndex = 5;//Set the background color to blue and set the background color Rows(1).Font.ColorIndex=4
//Set the cell content to wrap automatically range.WrapText = true; //Set the horizontal alignment of the cell content range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//Set the cell content to stack vertically Method //range.VerticalAlignment=Excel.XlVAlign.xlVAlignCenter //range.WrapText = true; .Cells(2,1).Value="Card number"; xlsheet.Cells(2,2).Value="Password"; xlsheet.Cells(2,3).Value="Billing method "; xlsheet.Cells(2,4).Value="Valid days"; xlsheet.Cells(2,5).Value="Amount"; xlsheet.Cells(2,6) .Value="Service items"; xlsheet.Cells(2,7).Value="Card issuance time"; var oTable=document.all['fors:data']; var rowNum =oTable.rows.length; for(i=2;i<=rowNum;i ){ for (j=1;j<=7;j ){ //html table class content Write to excel xlsheet.Cells(i 1,j).Value=oTable.rows(i-1).cells(j-1).innerHTML; }
}
// xlsheet.Range(xlsheet.Cells(i, 4), xlsheet.Cells(i-1, 6)).BorderAround , 4 // for(mn=1,mn<=6;mn ) . xlsheet.Range(xlsheet.Cells(1, mn), xlsheet.Cells(i1, j)).Columns.AutoFit; xlsheet.Columns.AutoFit; xlsheet.Range( xlsheet.Cells(1,1),xlsheet.Cells(rowNum 1,7)). HorizontalAlignment =-4108;//Centered xlsheet.Range( xlsheet.Cells(1,1),xlsheet.Cells(1,7)).VerticalAlignment =-4108; xlsheet.Range( xlsheet.Cells( 2,1),xlsheet.Cells(rowNum 1,7)).Font.Size=10; xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum 1,7)).Borders (3).Weight = 2; //Set the left margin xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum 1,7)).Borders(4).Weight = 2;/ /Set the right margin xlsheet.Range(xlsheet.Cells(2,1),xlsheet.Cells(rowNum 1,7)).Borders(1).Weight = 2;//Set the top margin xlsheet .Range(xlsheet.Cells(2,1),xlsheet.Cells(rowNum 1,7)).Borders(2).Weight = 2;//Set the bottom margin
xls.UserControl = true; //It is very important and cannot be omitted, otherwise problems will occur, which means that excel will be controlled by the user xls=null; xlBook=null; xlsheet=null; }
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn