Introduction to using javascript to generate reports in excel

Source: Internet
Author: User

Recently I made a project and used javascript to manipulate excel to generate reports. Below is an example with detailed annotations.
Copy codeThe Code is as follows:
<Html>
<Head>
<Script language = "javascript" type = "text/javascript">

</Script> <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 the Excel spreadsheet software, and the browser must use the" ActiveX Control ". Your browser must allow the execution of the control. Click [help] to learn about browser settings! ");
Return "";
}
Xls. visible = true; // set excel to be visible
Var xlBook = xls. Workbooks. Add;
Var xlsheet = xlBook. Worksheets (1 );
<! -- Merge -->
Xlsheet. Range (xlsheet. Cells (), xlsheet. Cells (). mergecells = true;
Xlsheet. Range (xlsheet. Cells (), xlsheet. Cells (). value = "card issuance record ";
// Xlsheet. Range (xlsheet. Cells (), xlsheet. Cells (). Interior. ColorIndex = 5; // set the background color to blue.
// Xlsheet. Range (xlsheet. Cells (), xlsheet. Cells (). 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 Row Height -->
Xlsheet. Rows (1). RowHeight = 25;
<! -- Set the Font ws. Range (ws. Cells (i0 + 1, j0), ws. Cells (i0 + 1, j1). Font. Size = 13 -->
Xlsheet. Rows (1). Font. Size = 14;
<! -- Set the Font to xlsheet. Range (xlsheet. Cells (i0, j0), ws. Cells (i0, j0). Font. Name = "" -->
Xlsheet. Rows (1). Font. Name = "";
<! -- Set column width xlsheet. Columns (2) = 14; -->
Xlsheet. Columns ("A: D"). ColumnWidth = 18;
<! -- Set display characters rather than numbers -->
Xlsheet. Columns (2). NumberFormatLocal = "@";
Xlsheet. Columns (7). NumberFormatLocal = "@";

// Set the automatic line feed range. WrapText = true for the cell content;
// Sets the horizontal alignment of cell content. range. HorizontalAlignment = Excel. XlHAlign. xlHAlignCenter; // sets the vertical stacking mode of cell content.
// Range. VerticalAlignment = Excel. XlVAlign. xlVAlignCenter
// Range. WrapText = true; xlsheet. Rows (3). WrapText = true automatic line feed
// Set the title bar
Xlsheet. 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 (). Value = "Service Project ";
Xlsheet. Cells (). Value = "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 ++ ){
// Write the html table class to excel
Xlsheet. Cells (I + 1, j). Value = oTable. rows (I-1). cells (J-1). innerHTML;
}

}
<! -- Xlsheet. Range (xls. Cells (I + 4, 2), xls. Cells (rowNum, 4). Merge; -->
// 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 (4108), xlsheet. Cells (rowNum +). HorizontalAlignment =-; // center
Xlsheet. Range (xlsheet. Cells (4108), xlsheet. Cells (). verticalignment =;
Xlsheet. Range (xlsheet. Cells (), xlsheet. Cells (rowNum +). Font. Size = 10;
Xlsheet. Range (xlsheet. Cells (), xlsheet. Cells (rowNum +). Borders (3). Weight = 2; // set the left margin
Xlsheet. Range (xlsheet. Cells (), xlsheet. Cells (rowNum +). Borders (4). Weight = 2; // set the right margin
Xlsheet. Range (xlsheet. Cells (), xlsheet. Cells (rowNum +). Borders (1). Weight = 2; // set the top margin
Xlsheet. Range (xlsheet. Cells (), xlsheet. Cells (rowNum +). Borders (2). Weight = 2; // set the bottom margin


Xls. UserControl = true; // very important. It cannot be omitted. Otherwise, the excel file is handed over to the user for control.
Xls = null;
XlBook = null;
Xlsheet = null;
}


</Script> <link href = "css/styles3.css" rel = "stylesheet" type = "text/css"/>
<Title> ziyuanweihu </title>
</Head>
<Body>
<Form id = "fors" method = "post" action = "/WebModule/admins/card/showcard. faces" enctype = "application/x-www-form-urlencoded">


<Table id = "fors: top" border = "0" cellpadding = "0" cellspacing = "0" width = "100%">
<Tbody>
<Tr>
<Td class = "left"> </td>
<Td class = "topMiddle"> </td>
<Td class = "right"> </td>
</Tr>
</Tbody>
</Table>


<Table border = "0" cellpadding = "0" cellspacing = "0" width = "100%">
<Tbody>
<Tr>
<Td class = "middleLeft"> </td>
<Td class = "btstyle"> <table id = "fors: sort" border = "0" cellpadding = "0" cellspacing = "0" style = "valign: center "width =" 100% ">
<Tbody>
<Tr>
<Td class = "btstyle"> <input type = "button" name = "fors: _ id7" value = "generate an excel file" onclick = "MakeExcel () "/> <input type =" submit "name =" fors: _ id8 "value =" return "/> </td>
</Tr>
</Tbody>
</Table>
<Table id = "fors: data" border = "1" cellpadding = "0" cellspacing = "1" width = "100%">
<Thead>
<Tr>
<Th scope = "col"> <span id = "fors: data: headerText1"> card number </span> </th>
<Th scope = "col"> <span id = "fors: data: headerText2"> password </span> </th>
<Th scope = "col"> <span id = "fors: data: headerText3"> billing method </span> </th>
<Th scope = "col"> <span id = "fors: data: headerText4"> valid days </span> </th>
<Th scope = "col"> amount </th>
<Th scope = "col"> <span id = "fors: data: headerText6"> service project </span> </th>
<Th scope = "col"> <span id = "fors: data: headerText7"> card issuance time </span> </th>
</Tr>
</Thead>
<Tbody>
<Tr>
<Td> h000010010 </td>
<Td> 543860 </td>
<Td> counting point </td>
<Td> </td>
<Td> 2.0 </td>
<Td> test project </td>
<Td> 10:14:40. 843 </td>
</Tr>
<Tr>
<Td> hsf-10011 </td>
<Td> 683352 </td>
<Td> counting point </td>
<Td> </td>
<Td> 2.0 </td>
<Td> test project </td>
<Td> 10:14:40. 843 </td>
</Tr>
<Tr>
<Td> h000010012 </td>
<Td> 433215 </td>
<Td> counting point </td>
<Td> </td>
<Td> 2.0 </td>
<Td> test project </td>
<Td> 10:14:40. 843 </td>
</Tr>
<Tr>
<Td> h000010013 </td>
<Td> 393899 </td>
<Td> counting point </td>
<Td> </td>
<Td> 2.0 </td>
<Td> test project </td>
<Td> 10:14:40. 843 </td>
</Tr>
<Tr>
<Td> h000010014 </td>
<Td> 031736 </td>
<Td> counting point </td>
<Td> </td>
<Td> 2.0 </td>
<Td> test project </td>
<Td> 10:14:40. 843 </td>
</Tr>
<Tr>
<Td> h000010015 </td>
<Td> 188600 </td>
<Td> counting point </td>
<Td> </td>
<Td> 2.0 </td>
<Td> test project </td>
<Td> 10:14:40. 843 </td>
</Tr>
<Tr>
<Td> h000010016 </td>
<Td> 363407 </td>
<Td> counting point </td>
<Td> </td>
<Td> 2.0 </td>
<Td> test project </td>
<Td> 10:14:40. 843 </td>
</Tr>
<Tr>
<Td> hsf-10017 </td>
<Td> 175315 </td>
<Td> counting point </td>
<Td> </td>
<Td> 2.0 </td>
<Td> test project </td>
<Td> 10:14:40. 843 </td>
</Tr>
<Tr>
<Td> h000010018 </td>
<Td> 354437 </td>
<Td> counting point </td>
<Td> </td>
<Td> 2.0 </td>
<Td> test project </td>
<Td> 10:14:40. 843 </td>
</Tr>
<Tr>
<Td> h000010019 </td>
<Td> 234750 </td>
<Td> counting point </td>
<Td> </td>
<Td> 2.0 </td>
<Td> test project </td>
<Td> 10:14:40. 843 </td>
</Tr>
</Tbody>
</Table>
</Td>
<Td class = "middleRight"> </td>
</Tr>
</Tbody>
</Table>
<Table id = "fors: bottom" border = "0" cellpadding = "0" cellspacing = "0" width = "100%">
<Tbody>
<Tr>
<Td class = "left">

</Td>
<Td class = "bottomMiddle"> </td>
<Td class = "right">

</Td>
</Tr>
</Tbody>
</Table>
<Input type = "hidden" name = "fors" value = "fors"/> </form>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.