Home > Web Front-end > JS Tutorial > body text

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

Copy the code The code is as follows :



< 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

xlsheet.Rows(1).RowHeight = 25;

xlsheet.Rows(1).Font.Size=14;

xlsheet.Rows(1).Font .Name="Helvetica";

xlsheet.Columns("A:D").ColumnWidth =18;

xlsheet.Columns(2).NumberFormatLocal="@";
xlsheet.Columns(7).NumberFormatLocal="@";

//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;
}



ziyuanweihu







































































































































卡号 密码 计费方式 有效天数 金额 所属服务项目 发卡时间
h000010010 543860 计点 2.0 测试项目 2006-06-23 10:14:40.843
h000010011 683352 计点 2.0 测试项目 2006-06-23 10:14:40.843
h000010012 433215 计点 2.0 测试项目 2006-06-23 10:14:40.843
h000010013 393899 计点 2.0 测试项目 2006-06-23 10:14:40.843
h000010014 031736 计点 2.0 测试项目 2006-06-23 10:14:40.843
h000010015 188600 计点 2.0 测试项目 2006-06-23 10:14:40.843
h000010016 363407 计点 2.0 测试项目 2006-06-23 10:14:40.843
h000010017 175315 计点 2.0 测试项目 2006-06-23 10:14:40.843
h000010018 354437 计点 2.0 测试项目 2006-06-23 10:14:40.843
h000010019 234750 计点 2.0 测试项目 2006-06-23 10:14:40.843


















Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template