JS generates Word documents

Source: Internet
Author: User

<HTML>
<Head>
<Title>
</Title>
</Head>
<Body>
<Form ID = "form">
<Table id = "printa" width = "100%" border = "1" cellspacing = "0" cellpadding = "0">
<Tr style = "text-align: center;">
<TD> cell 1 </TD>
<TD> Cell 2 </TD>
<TD> cell 3 </TD>
<TD> cell 4 </TD>
</Tr>
<Tr>
<TD colspan = 4 style = "text-align: center;"> <font color = "red" face = "verdana"> cell merging </font> </TD>
</Tr>
</Table>
<Br>
& Lt; Table id = "test" width = "100%" & gt;
<Tr>
<TD> <font color = "red"> test </font> </TD>
</Tr>
</Table>
</Form>
<Input type = "button" onclick = "javascript: makeword ();" value = "Export page to word">
<Script language = "JavaScript">
Function makeword ()
{
VaR word = new activexobject ("word. application ");
// Var Doc = word. Documents. Open ("C:/test.doc"); // open an existing template here
VaR Doc = word. Documents. Add ("",); // directly add content without opening the Template
VaR range = Doc. Range ();

VaR sel = Document. Body. createTextRange ();
Sel. movetoelementtext (form); // here form is the ID of the page form
Sel. Select ();
Sel.exe ccommand ("copy ");
Range. paste ();
Word. application. Visible = true;
Doc. saveas ("C: // ba.doc"); // save it to the specified location. Note that the path must be "//" or an error will be reported.
}
</SCRIPT>
</Body>

 

 

 

 


<Script language = "JavaScript">
Function word ()
{
WORD = new activexobject ('word. application ');
Word. application. Visible = true;
VaR Doc = word. Documents. Add );
Range = Doc. Range (0, 1 );
VaR selrentlayer1.doc ument. Body. createTextRange ();
Layer1.document.exe ccommand ('copy ');
Sel. moveend ('character ');
Range. paste ();
Location. Reload ();
Word. activewindow. activepane. View. type = 3; // The default page view is
}
</SCRIPT>
<Div id = layer1> <input type = "button" name = "button" value = "generate WORD" onclick = "Word ();"> </div>

 

1. Save the HTML page to word

**************************************** **************************************** *******

<HTML>

<Head>

<Title>

</Title>

</Head>

<Body>

<Form ID = "form">

<Table id = "printa" width = "100%" border = "1" cellspacing = "0" cellpadding = "0">

<Tr style = "text-align: center;">

<TD> cell 1 </TD>

<TD> Cell 2 </TD>

<TD> cell 3 </TD>

<TD> cell 4 </TD>

</Tr>

<Tr>

<TD colspan = 4 style = "text-align: center;"> <font color = "red" face = "verdana"> cell merging </font> </TD>

</Tr>

</Table>

<Br>

& Lt; Table id = "test" width = "100%" & gt;

<Tr>

<TD> <font color = "red"> test </font> </TD>

</Tr>

</Table>

</Form>

<Input type = "button" onclick = "javascript: makeword ();" value = "Export page to word">

<Script language = "JavaScript">

Function makeword ()

{

VaR word = new activexobject ("word. application ");

// Var Doc = word. Documents. Open ("C:/test.doc"); // open an existing template here

VaR Doc = word. Documents. Add ("",); // directly add content without opening the Template

VaR range = Doc. Range ();

VaR sel = Document. Body. createTextRange ();

Sel. movetoelementtext (form); // here form is the ID of the page form

Sel. Select ();

Sel.exe ccommand ("copy ");

Range. paste ();

Word. application. Visible = true;

Alert ("S ");

Word. application. selection. inlineshapes. addpicture ("C: // m1_gif ");

Alert ("N ");

Doc. saveas ("C: // ba.doc"); // save it to the specified location. Note that the path must be "//" or an error will be reported.

}

</SCRIPT>

</Body>

</Html>

**************************************** **************************************** *******************

2. Use js to generate WORD

**************************************** **************************************** *******************

<SCRIPT>

Function wordcontorl (){

Alert ("1111 ")

VaR wordapp = new activexobject ("word. application ");

VaR wdcharacter = 1

VaR wdorientlandscape = 1

Wordapp. application. Visible = true;

VaR mydoc = wordapp. Documents. Add ();

Wordapp. activedocument. pagesetup. Orientation = wdorientlandscape // 0 is a vertical page, and 1 is a horizontal page

Wordapp. selection. paragraphformat. Alignment = 1 // 1 align, 0 is right

Wordapp. selection. Font. Bold = true

Wordapp. selection. Font. size = 20

Wordapp. selection. typetext ("My title ");

Wordapp. selection. moveright (wdcharacter); // move the cursor to the right

Wordapp. selection. typeparagraph () // insert a paragraph

Wordapp. selection. Font. size = 12

Wordapp. selection. typetext ("subtitle"); // The date on which the Branch is inserted.

Wordapp. selection. typeparagraph () // insert a paragraph

 

VaR mytable = mydoc. Tables. Add (wordapp. selection. Range, 8, 7) // table with 8 rows and 7 Columns

// Mytable. Style = "Grid"

VaR AA = "my column title"

VaR tablerange; // assign values to cells in the table

For (I = 0; I <7; I ++)

{

With (mytable. Cell (1, I + 1). Range)

{

Font. size = 12;

Insertafter (AA );

Columnwidth = 4

}

}

For (I = 0; I <7; I ++)

{

For (n = 0; n <7; n ++)

{

With (mytable. Cell (I + 2, n + 1). Range)

{Font. size = 12;

Insertafter ("BBBB ");

}

}

}

Row_count = 0;

Col_count = 0

Mydoc. Protect (1)

}

Wordcontorl ()

</SCRIPT>

**************************************** **************************************** *******************

3. traverse and export to word

**************************************** **************************************** *******************

1. traverse and export the content in each text box.

<Script language = "JavaScript">

Function openword () {// export word

VaR TXT = "TXT ";

For (I = 0; I <table1.rows. length; I ++) // traverse and export charts and text

{

TXT = "TXT" + jilu [I];

Myrange = mydoc. Range (myrange. End-1, myrange. End); // set the start point.

VaR selrentlayer1.doc ument. Body. createTextRange ();

// SEL. movetoelementtext (Table1 );

Sel. movetoelementtext (document. All [TXT]);

Sel. Select ();

Layer1.document.exe ccommand ('copy ');

Sel. moveend ('character ');

Myrange. paste ();

Myrange = mydoc. Range (myrange. End-1, myrange. End );

Myrange. insertafter ("/N ");

Excelsheet. activewindow. View. tablegridlines = false; // hide the dotted box

}

}

</SCRIPT>

2. Copy the content in table 1 to the word

<Script language = "JavaScript">

Function openword () {// export word

Layer1.style. Border = 0;

Excelsheet = new activexobject ('word. application ');

Excelsheet. application. Visible = true;

VaR mydoc = excelsheet. Documents. Add );

Myrange = mydoc. Range (0, 1 );

Myrange = mydoc. Range (myrange. End-1, myrange. End); // set the start point.

VaR selrentlayer1.doc ument. Body. createTextRange ();

Sel. movetoelementtext (Table1 );

Sel. Select ();

Layer1.document.exe ccommand ('copy ');

Sel. moveend ('character ');

Myrange. paste ();

Myrange = mydoc. Range (myrange. End-1, myrange. End );

Myrange. insertafter ("/N ");

Excelsheet. activewindow. View. tablegridlines = false;

}

</SCRIPT>

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.