Full introduction to Excel operations using JavaScript

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.

 

<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 = "& #29983; & #25104; Excel & #25991; & #20214; "onclick =" makeexcel () "/> <input type =" Submit "name =" fors: _ id8 "value =" & #36820; & #22238; "/> </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" >&# 21345 ;&# 21495; </span> </Th>
<TH scope = "col"> <span id = "fors: Data: headertext2" >&# 23494 ;&# 30721; </span> </Th>
<TH scope = "col"> <span id = "fors: Data: headertext3" >&# 35745; & #36153; & #26041; & #24335; </span> </Th>
<TH scope = "col"> <span id = "fors: Data: headertext4" >&# 26377; & #25928; & #22825; & #25968; </span> </Th>
<TH scope = "col" >&# 37329 ;## 39069; </Th>
<TH scope = "col"> <span id = "fors: Data: headertext6" >&# 25152; & #23646; & #26381; & #21153; & #39033; & #30446; </span> </Th>
<TH scope = "col"> <span id = "fors: Data: headertext7" >&# 21457; & #21345; & #26102; & #38388; </span> </Th>
</Tr>
</Thead>
<Tbody>
<Tr>
<TD> h000010010 </TD>
<TD> 543860 </TD>
<TD> & #35745; & #28857; </TD>
<TD> </TD>
<TD> 2.0 </TD>
<TD> & #27979; & #35797; & #39033; & #30446; </TD>
<TD> 10:14:40. 843 </TD>
</Tr>
<Tr>
<TD> hsf-10011 </TD>
<TD> 683352 </TD>
<TD> & #35745; & #28857; </TD>
<TD> </TD>
<TD> 2.0 </TD>
<TD> & #27979; & #35797; & #39033; & #30446; </TD>
<TD> 10:14:40. 843 </TD>
</Tr>
<Tr>
<TD> h000010012 </TD>
<TD> 433215 </TD>
<TD> & #35745; & #28857; </TD>
<TD> </TD>
<TD> 2.0 </TD>
<TD> & #27979; & #35797; & #39033; & #30446; </TD>
<TD> 10:14:40. 843 </TD>
</Tr>
<Tr>
<TD> h000010013 </TD>
<TD> 393899 </TD>
<TD> & #35745; & #28857; </TD>
<TD> </TD>
<TD> 2.0 </TD>
<TD> & #27979; & #35797; & #39033; & #30446; </TD>
<TD> 10:14:40. 843 </TD>
</Tr>
<Tr>
<TD> h000010014 </TD>
<TD> 031736 </TD>
<TD> & #35745; & #28857; </TD>
<TD> </TD>
<TD> 2.0 </TD>
<TD> & #27979; & #35797; & #39033; & #30446; </TD>
<TD> 10:14:40. 843 </TD>
</Tr>
<Tr>
<TD> h000010015 </TD>
<TD> 188600 </TD>
<TD> & #35745; & #28857; </TD>
<TD> </TD>
<TD> 2.0 </TD>
<TD> & #27979; & #35797; & #39033; & #30446; </TD>
<TD> 10:14:40. 843 </TD>
</Tr>
<Tr>
<TD> h000010016 </TD>
<TD> 363407 </TD>
<TD> & #35745; & #28857; </TD>
<TD> </TD>
<TD> 2.0 </TD>
<TD> & #27979; & #35797; & #39033; & #30446; </TD>
<TD> 10:14:40. 843 </TD>
</Tr>
<Tr>
<TD> hsf-10017 </TD>
<TD> 175315 </TD>
<TD> & #35745; & #28857; </TD>
<TD> </TD>
<TD> 2.0 </TD>
<TD> & #27979; & #35797; & #39033; & #30446; </TD>
<TD> 10:14:40. 843 </TD>
</Tr>
<Tr>
<TD> h000010018 </TD>
<TD> 354437 </TD>
<TD> & #35745; & #28857; </TD>
<TD> </TD>
<TD> 2.0 </TD>
<TD> & #27979; & #35797; & #39033; & #30446; </TD>
<TD> 10:14:40. 843 </TD>
</Tr>
<Tr>
<TD> h000010019 </TD>
<TD> 234750 </TD>
<TD> & #35745; & #28857; </TD>
<TD> </TD>
<TD> 2.0 </TD>
<TD> & #27979; & #35797; & #39033; & #30446; </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>
Welcome to the QQ group: 30406099

 

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.