Javascript exports a webpage table to word
<SCRIPT>
Function allareaword ()
{
If (document. All ("tab"). Rows. Length = 0)
{
Alert ("NO content to export! ");
Return;
}
Try {
VaR owd = new activexobject ("word. application ");
} Catch (E)
{
Alert ("The Office object cannot be called. Please make sure that your machine has installed office and the site name of the system has been added to the trusted site list of IE! ");
Return;
}
VaR ODC = owd. Documents. Add );
VaR Orange = ODC. Range (0, 1 );
VaR sel = Document. Body. createTextRange ();
Sel. movetoelementtext (Tab); // tab indicates the ID of the table where the data is exported.
Sel. Select ();
Sel.exe ccommand ("copy ");
Orange. paste ();
Owd. application. Visible = true;
}
</SCRIPT>
<Table id = "tab" align = "center" width = "300" border = "1" bordercolor = "#2 baeff" cellpadding = "0" cellspacing = "0" bgcolor = "lightblue">
<Tr>
<TD> 1 </TD>
<TD> 2 </TD>
<TD> 3 </TD>
</Tr>
<Tr>
<TD> 4 </TD>
<TD> 5 </TD>
<TD> 6 </TD>
</Tr>
<Tr>
<TD> 7 </TD>
<TD> 8 </TD>
<TD> 9 </TD>
</Tr>
</Table>
<Input type = button name = 'button _ export' Title = 'export to word' onclick = allareaword () value = download to word>
Export a webpage table to excel using JavaScript
<Script language = "JavaScript">
<! --
Function automateexcel ()
{
VaR eltable = Document. getelementbyid ("outtable"); // outtable is the ID of the table where the exported data is located;
VaR orangeref = Document. Body. createTextRange ();
Orangeref. movetoelementtext (eltable );
Orangeref.exe ccommand ("copy ");
Try {
VaR appexcel = new activexobject ("Excel. application ");
} Catch (E)
{
Alert ("The Office object cannot be called. Please make sure that your machine has installed office and the site name of the system has been added to the trusted site list of IE! ");
Return;
}
Appexcel. Visible = true;
Appexcel. workbooks. Add (). worksheets. Item (1). paste ();
Appexcel = NULL;
}
// -->
</SCRIPT>
<Input type = "button" name = "out_excel" onclick = "automateexcel ();" value = "export to excel" class = "notprint">
<Table border = "1" cellpadding = "0" cellspacing = "0" id = outtable>
<Tr Height = "28">
<TD width = "27" Height = "86" rowspan = "4" bgcolor = "# ffffcc"> NO. </TD>
<TD width = "111" rowspan = "4" bgcolor = "# ffffcc"> <Div align = "center"> service outlets </div> </TD>
<TD width = "402" colspan = "7" bgcolor = "# ffffcc"> <Div align = "center"> User Satisfaction </div> </TD>
</Tr>
<Tr Height = "19">
<TD width = "100" Height = "39" colspan = "3" rowspan = "2" bgcolor = "# ffffcc"> <Div align = "center"> Number of samples </div> </TD>
<TD width = "218" colspan = "3" rowspan = "2" bgcolor = "# ffffcc"> overall evaluation of the implementation of this activity by this service outlet, full score: 10 (average score) </TD>
<TD width = "84" rowspan = "3" bgcolor = "# ffffcc"> <Div align = "center"> quarterly average score </div> </TD>
</Tr>
<Tr Height = "20"> </tr>
<Tr Height = "19">
<TD width = "29" Height = "19" bgcolor = "# ffffcc"> <Div align = "center"> August 1, April </div> </TD>
<TD width = "29" bgcolor = "# ffffcc"> <Div align = "center"> August 1, May </div> </TD>
<TD width = "42" bgcolor = "# ffffcc"> <Div align = "center"> total </div> </TD>
<TD width = "68" bgcolor = "# ffffcc"> <Div align = "center"> August 1, April </div> </TD>
<TD width = "68" bgcolor = "# ffffcc"> <Div align = "center"> August 1, May </div> </TD>
<TD width = "82" bgcolor = "# ffffcc"> <Div align = "center"> total </div> </TD>
</Tr>
<Tr>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD bgcolor = "# ffffcc"> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD bgcolor = "# ffffcc"> & nbsp; </TD>
<TD bgcolor = "# ffffcc"> & nbsp; </TD>
</Tr>
<Tr>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD bgcolor = "# ffffcc"> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD bgcolor = "# ffffcc"> & nbsp; </TD>
<TD bgcolor = "# ffffcc"> & nbsp; </TD>
</Tr>
</Table>
The following dialog box is displayed during export:
1. First, add the website address to a trusted site, such:
2. Click "Custom Level" in "tools-Internet Options-security-trusted sites ", click "Disable" or "enable" in "initialize acitvex control without security labeling and run scripts.
How to obtain the Office version number, installation path, and user
Alert ("your" + WRD. application. caption + "installation path:/N" + WRD. application. path + "/n version:" + WRD. application. version
+ "/N registration:" + WRD. application. username)
A new requirement is raised in the project. When wodr needs to be imported, a table automatically occupies one page to facilitate printing, so the modification is made and automatic paging is supported. In addition, I found a bug that always caused too many errors in the syntax spelling check and fixed it. In Excel, a manual column width setting is added. Code that fixes and adds new features:
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
<Script language = "JavaScript">
Function automatewordautopaging (prefixion, count)
{
VaR owd = new activexobject ("word. application ");
VaR ODC = owd. Documents. Add );
ODC. showgrammaticalerrors = false; // block syntax check
ODC. showspellingerrors = false; // block syntax check
VaR Orange = ODC. Range (0, 1 );
For (I = 0; I <count; I ++)
{
VaR sel = Document. Body. createTextRange ();
VaR tablename = prefixion + I
VaR table = Document. getelementbyid (tablename)
Sel. movetoelementtext (table );
Sel. Select ();
Sel.exe ccommand ("copy ");
Owd. selection. paste ();
// Owd. selection. typetext ("AAA ")
Owd. selection. insertbreak (7) // insert a paging character
}
Owd. activedocument. activewindow. View. type = 3 // set the browsing mode
Owd. application. Visible = true;
}
Function automateexcel (prefixion)
{
VaR eltable = Document. getelementbyid ("automateexcel ");
VaR orangeref = Document. Body. createTextRange ();
Orangeref. movetoelementtext (eltable );
Orangeref.exe ccommand ("copy ");
Try {
VaR appexcel = new activexobject ("Excel. application ");
} Catch (E)
{
Alert ("The Office object cannot be called. Please make sure that your machine has installed office and the site name of the system has been added to the trusted site list of IE! ");
Return;
}
Appexcel. Visible = true;
Appexcel. workbooks. Add (). worksheets. Item (1). paste ();
Appexcel. workbooks (1). worksheets. Item (1). Columns ("A: A"). columnwidth = 100;
// Appexcel. workbooks (1). worksheets. Item (1). Columns ("B: B"). columnwidth = 21;
Appexcel = NULL;
}
</SCRIPT>
</Head>
<Body>
<Input name = "import" type = "button" id = "import" value = "export to word automatic paging" onclick = "automatewordautopaging ('table', 5)"/>
<Input name = "button" type = "button" class = "input-button-excel" onclick = "automateexcel ('table ') "value =" export to excel control column width "/>
<Div id = "automateexcel">
<Table class = TABP id = "table0" cellspacing = 0 cellpadding = 2 width = "100%"
Align = center border = 1>
<Tr>
<TD width = "100%" align = "center"> title 0 </TD>
</Tr>
<Tr>
<TD align = "center"> content 0 </TD>
</Tr>
</Table>
<Br>
<Table class = TABP id = "Table1" cellspacing = 0 cellpadding = 2 width = "100%"
Align = center border = 1>
<Tr>
<TD width = "100%" align = "center"> Title 1 </TD>
</Tr>
<Tr>
<TD align = "center"> content 1 </TD>
</Tr>
</Table>
<Br>
<Table class = TABP id = "Table2" cellspacing = 0 cellpadding = 2 width = "100%"
Align = center border = 1>
<Tr>
<TD width = "100%" align = "center"> Title 2 </TD>
</Tr>
<Tr>
<TD align = "center"> content 2 </TD>
</Tr>
</Table>
<Br>
<Table class = TABP id = "table3" cellspacing = 0 cellpadding = 2 width = "100%"
Align = center border = 1>
<Tr>
<TD width = "100%" align = "center"> Title 3 </TD>
</Tr>
<Tr>
<TD align = "center"> content 3 </TD>
</Tr>
</Table>
<Br>
<Table class = TABP id = "table4" cellspacing = 0 cellpadding = 2 width = "100%"
Align = center border = 1>
<Tr>
<TD width = "100%" align = "center"> Title 4 </TD>
</Tr>
<Tr>
<TD align = "center"> content 4 </TD>
</Tr>
</Table>
<Br>
</Div>
</Body>
</Html>