JavaScript to manipulate Word and Excel implementation code _javascript tips

Source: Internet
Author: User
1. Save HTML page to Word
Copy Code code as follows:

<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>
&LT;TD colspan=4 style= "Text-align:center;" ><font color= "Red" face= "Verdana" > Cell merge </FONT></TD>
</TR>
</TABLE>
<BR>
<table id = "Test" width= "100%" >
<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"); To open an existing template here
var doc = Word. Documents.Add ("", 0, 1);/do not open template directly add content
var range=doc. Range ();

var sel = Document.body.createTextRange ();
Sel.movetoelementtext (form)//Here form is the ID of the page form
Sel.select ();
Sel.execcommand ("Copy");
Range.Paste ();
Word. Application.Visible = true;
Alert ("s");
Word. Application.Selection.InlineShapes.AddPicture ("C:\\m20.gif");
Alert ("n");
Doc. SaveAs ("C:\\ba.doc"); Store to specified location note the path must be "\" or it will be an error.
}
</SCRIPT>
</body>



2. Generate Word with JS

Copy Code code as follows:


<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

WordApp. Selection.paragraphformat.alignment=1//1 Center Alignment, 0 to right
WordApp. Selection.font.bold=true
WordApp. Selection.font.size=20

WordApp. Selection.TypeText ("my title");
WordApp.    Selection.MoveRight (wdcharacter); The cursor moves the character right
WordApp.Selection.TypeParagraph ()//Insert Paragraph
WordApp. Selection.font.size=12
WordApp. Selection.TypeText ("subtitle"); Branch Insert Date
WordApp.Selection.TypeParagraph ()//Insert Paragraph



Table of the Var mytable=mydoc.tables.add (WordApp.Selection.Range, 8,7)//8 row 7 columns
mytable.style= "Grid Type"
var AA = "My column header"

var Tablerange; Here's how to assign a value to a cell in a 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 Export to Word
***************************************************************************************************
1, traverse the export of each text box content.
Copy Code code as follows:

<script language= "JavaScript" >
function Openword () {//Export word
var txt= "TXT";
for (i=0;i<table1.rows.length;i++)/traverse Export chart and text
{
txt= "TXT" +jilu[i];
MyRange =mydoc. Range (myrange.end-1,myrange.end);//Set starting point
var sel=layer1.document.body.createtextrange ();
Sel.movetoelementtext (table1);
Sel.movetoelementtext (Document.all[txt]);
Sel.select ();
Layer1.document.execCommand (' Copy ');
Sel.moveend (' character ');
Myrange.paste ();
MyRange =mydoc. Range (Myrange.end-1,myrange.end);
Myrange.insertafter ("\ n");
ExcelSheet.ActiveWindow.View.TableGridlines = false;//Hide Virtual box
}
}
</script>

2, copy the contents of table1 to Word
Copy Code code as follows:

<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 (', 0,0);
MyRange =mydoc. Range (0,1);
MyRange =mydoc. Range (myrange.end-1,myrange.end);//Set starting point
var sel=layer1.document.body.createtextrange ();
Sel.movetoelementtext (table1);
Sel.select ();
Layer1.document.execCommand (' Copy ');
Sel.moveend (' character ');
Myrange.paste ();

MyRange =mydoc. Range (Myrange.end-1,myrange.end);
Myrange.insertafter ("\ n");
ExcelSheet.ActiveWindow.View.TableGridlines = false;
}
</script>

**************************************************************************************************
4. Operation Excel
**************************************************************************************************
Copy Code code as follows:

<%@ page C%>
<script language= "javascript" type= "Text/javascript" >
function Makeexcel () {
var i, J, N;
try {
var xls = new ActiveXObject ("Excel.Application");
}
catch (e) {
Alert ("To print the table, you must have the Excel spreadsheet software installed, and the browser must use ActiveX controls, and your browser must allow the control to be executed.") Click "Help" to learn how to set up your browser! ");
Return "";
}
Xls.visible =true; Set Excel to Visible
var xlbook = xls. Workbooks.Add;
var xlsheet = xlbook.worksheets (1);
<!--combined-->
Xlsheet. Range (Xlsheet. Cells (1,1), Xlsheet. Cells (1,7)). Mergecells=true;
Xlsheet. Range (Xlsheet. Cells (1,1), Xlsheet. Cells (1,7)). value= "card issuing record";
Xlsheet. Range (Xlsheet. Cells (1,1), Xlsheet. Cells (1,3)). interior.colorindex=5; Set the background color to blue
Xlsheet. Range (Xlsheet. Cells (1,1), Xlsheet. Cells (1,6)). font.colorindex=4; Set Font Color
Xlsheet. Rows (1). Interior. ColorIndex = 5//Set background color to blue set back to Rows (1). Font.colorindex=4
<!--set row height-->
Xlsheet. Rows (1). RowHeight = 25;
<!--set the font ws. Range (ws. Cells (i0+1,j0), WS. Cells (I0+1,J1)). Font.Size =-->
Xlsheet. Rows (1). font.size=14;
<!--set Font xlsheet font settings for the selected area. Range (Xlsheet. Cells (i0,j0), WS. Cells (i0,j0)). Font.Name = "Blackbody"-->
Xlsheet. Rows (1). Font.name= "Blackbody";
<!--set column width xlsheet. Columns (2) =14;-->
Xlsheet. Columns ("A:d"). ColumnWidth = 18;
<!--set display characters instead of numbers-->
Xlsheet. Columns (2). Numberformatlocal= "@";
Xlsheet. Columns (7). Numberformatlocal= "@";
Sets the cell content wrap range. WrapText = true;
Sets the horizontal alignment range of the cell contents. HorizontalAlignment = excel.xlhalign.xlhaligncenter;//Set cell content vertical Stack method
Range. Verticalalignment=excel.xlvalign.xlvaligncenter
Range. WrapText = true; Xlsheet. Rows (3). Wraptext=true Automatic Line Wrapping
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 = "Effective Days";
Xlsheet. Cells (2, 5). Value = "Amount";
Xlsheet. Cells (2, 6). Value = "Owning service item";
Xlsheet. Cells (2, 7). Value = "Card issuing 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 is written 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 (1,1), Xlsheet. Cells (rownum+1,7)). HorizontalAlignment =-4108;//Center
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 left margin
Xlsheet. Range (Xlsheet. Cells (2,1), Xlsheet. Cells (rownum+1,7)). Borders (4). Weight = 2;//Set Right margin
Xlsheet. Range (Xlsheet. Cells (2,1), Xlsheet. Cells (rownum+1,7)). Borders (1). Weight = 2;//Set Top margin
Xlsheet. Range (Xlsheet. Cells (2,1), Xlsheet. Cells (rownum+1,7)). Borders (2). Weight = 2;//Set Bottom margin
Xls. UserControl = true; It's important, you can't omit it, or it's going to go wrong. Excel to User control
Xls=null;
Xlbook=null;
Xlsheet=null;
}
</script>
<link href= "Css/styles3.css" rel= "stylesheet" type= "Text/css"/>
<title>ziyuanweihu</title>
<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>
&LT;TD class= "left" ></td>
&LT;TD class= "Topmiddle" ></td>
&LT;TD class= "right" ></td>
</tr>
</tbody>
</table>
<table border= "0" cellpadding= "0" cellspacing= "0" width= "100%" >
<tbody>
<tr>
&LT;TD class= "Middleleft" ></td>
&LT;TD class= "Btstyle" >
<table id= "Fors:sort" border= "0" cellpadding= "0" cellspacing= "0" style= "valign:center" width= "100%" >
<tbody>
<tr>
&LT;TD class= "Btstyle" >
<input type= "button" Name= "Fors:_id7" value= "Generate Excel File"/>
</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" > Effective days </span></th>
<th scope= "col" > Amount </th>
<th scope= "col" ><span id= "FORS:DATA:HEADERTEXT6" > Affiliated Service Project </span></th>
<th scope= "col" ><span id= "FORS:DATA:HEADERTEXT7" > Card issuing Time </span></th>
</tr>
</thead>
<tbody>
<tr>
<td>h000010010</td>
<td>543860</td>
<td> Meter Point </td>
<td></td>
<td>2.0</td>
<td> Test Project </td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010011</td>
<td>683352</td>
<td> Meter Point </td>
<td></td>
<td>2.0</td>
<td> Test Project </td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010012</td>
<td>433215</td>
<td> Meter Point </td>
<td></td>
<td>2.0</td>
<td> Test Project </td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010013</td>
<td>393899</td>
<td> Meter Point </td>
<td></td>
<td>2.0</td>
<td> Test Project </td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010014</td>
<td>031736</td>
<td> Meter Point </td>
<td></td>
<td>2.0</td>
<td> Test Project </td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010015</td>
<td>188600</td>
<td> Meter Point </td>
<td></td>
<td>2.0</td>
<td> Test Project </td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010016</td>
<td>363407</td>
<td> Meter Point </td>
<td></td>
<td>2.0</td>
<td> Test Project </td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010017</td>
<td>175315</td>
<td> Meter Point </td>
<td></td>
<td>2.0</td>
<td> Test Project </td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010018</td>
<td>354437</td>
<td> Meter Point </td>
<td></td>
<td>2.0</td>
<td> Test Project </td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010019</td>
<td>234750</td>
<td> Meter Point </td>
<td></td>
<td>2.0</td>
<td> Test Project </td>
<td>2006-06-23 10:14:40.843</td>
</tr>
</tbody>
</table>
</td>
&LT;TD class= "MiddleRight" ></td>
</tr>
</tbody>
</table>
<table id= "Fors:bottom" border= "0" cellpadding= "0" cellspacing= "0" width= "100%" >
<tbody>
<tr>
&LT;TD class= "Left" >

</td>
&LT;TD class= "Bottommiddle" > </td>
&LT;TD class= "Right" >

</td>
</tr>
</tbody>
</table>
<input type= "hidden" name= "fors" value= "Fors"/>
</form>
</body>

Copy Code code as follows:

<%--
----------------------------------1----------------------------------\
<HTML>
<HEAD>
<TITLE> Export to Excel</title>
<meta name= "Generator" c>
<meta name= "Author" c>
<meta name= "Keywords" c>
<meta name= "Description" c>
</HEAD>
<script language= "JavaScript" >
function Exportexcel (tableid) {
if (typeof (Export_object)!= "OBJECT") {
Document.body.insertAdjacentHTML ("Afterbegin", "<object style= ' display:none ' classid= ') clsid:0 002e510-0000-0000-c000-000000000046 ' id= ' export_object ' ></Object> ');
}
With (Export_object) {
DataType = "HTMLData";
HTMLData =tableid.outerhtml;
try {
Activesheet.export ("d:\\ table. xls", 0);
Alert (' Export Excel table successfully! ');
}
catch (e) {
Alert (' Export Excel table failed, make sure Excel2000 (or later) is installed, and the XLS file with the same name is not open ');
}
}
}
</script>
<BODY>
<table id= "TableID" border= "1" >
<tr>
<td> first Cell </td>
<td> Second Cell </td>
<td> Third Cell </td>
</tr>
<tr>
<td> Fourth Cell </td>
<td> Fifth Cell </td>
<td> Sixth Cell </td>
</tr>
</table>
<input type= "button" value= "Export Excel Table" >
</BODY>
</HTML>
----------------------------------2----------------------------------\
<!--exported to office2000 version of Word or Excel-->
<script language= "JavaScript" >
function Tabletoexcel () {
Window.clipboardData.setData ("Text", document.all (' theobjtable '). outerHTML);
try {
var Exapp = new ActiveXObject ("Excel.Application");
var exwbk = ExApp.workbooks.add ();
var exwsh = exwbk.worksheets (1);
Exapp.displayalerts = false;
Exapp.visible = true;
}
catch (e) {
Alert ("Your computer does not have Microsoft Excel software installed!") ");
return false;
}
Exwbk.worksheets (1). Paste;
}
function Tabletoword () {
var OWD = new ActiveXObject ("Word.Application");
var ODC = OWD.Documents.Add ("", 0, 1);
var oRange =odc.range (0,1);
var sel = Document.body.createTextRange ();
Sel.movetoelementtext (theobjtable);
Sel.select ();
Sel.execcommand ("Copy");
Orange.paste ();
OWD.Application.Visible = true;
}
</script>
<table id= "Theobjtable" >
<tr>
<td> in HTML page </td>
<td> put the table in the page </td>
<td> content Import to </td>
</td> in <td>word
<td> can also be imported to excel</td>
</tr>
</table>
<input type= "button" value= "Import to Excel" >
<input type= "button" value= "Import to Word" >
----------------------------------3----------------------------------\
--%>

******************************************************************************************************
5. About the use of JS to achieve the content of the Web page directly into Excel method
******************************************************************************************************
Copy Code code as follows:

xmlns:x= "Urn:schemas-microsoft-com:office:excel"
xmlns= "HTTP://WWW.W3.ORG/TR/REC-HTML40" >
<meta Http-equiv=content-type c>
<script>
function out () {
try{
var eltable = document.getElementById ("Out");
var orangeref = Document.body.createTextRange ();
Orangeref.movetoelementtext (eltable);
Orangeref.execcommand ("Copy");
var oXL = new ActiveXObject ("Excel.Application")
var owb = OXL.Workbooks.Add;
var osheet = Owb.activesheet;
Osheet.paste ();
oSheet.Cells.NumberFormatLocal = "@";
Osheet.columns ("D:d"). Select
OXL.Selection.ColumnWidth = 20
Osheet.columns ("A:a"). Select
Osheet.columns ("A"). Width = 1000;
oXL.Visible = true;
osheet = null;
OWB = null;
Appexcel = null;
}catch (e) {alert (e.description)}
}
</script>
<body>
<table id = ' Out ' >
<tr>
&LT;TD x:str bgcolor= "#FF0000" >00001</td>
&LT;TD x:str>0002</td>
</tr>
<tr>
&LT;TD height=18 align=right style= ' height:13.5pt ' x:str>00001</td>
&LT;TD Align=right x:str>000002</td>
</tr>
<tr>
&LT;TD x:str>0003</td>
&LT;TD x:str>00003</td>
</tr>
</table>
<input type= "button" value= "button" Id=button1 name=button1 >
</body>

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.