1. Save the html page to word
Copy codeThe Code is 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>
<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: \ mw.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
Copy codeThe Code is 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 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.
Copy codeThe Code is as follows:
<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
Copy codeThe Code is 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 );
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>
**************************************** **************************************** ******************
4. excel operations
**************************************** **************************************** ******************
Copy codeThe Code is as follows:
<% @ Page c %>
<H1> content <Html>
<Head>
<Script language = "javascript" type = "text/javascript">
Function MakeExcel (){
Var I, j, n;
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 (2, 6). Value = "Service Project ";
Xlsheet. Cells (2, 7). Value = "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 ++ ){
// 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"/>
</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>
Copy codeThe Code is 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: 0002E510-0000-0000-C000-000000000046 'id = 'export _ object'> </OBJECT> ");
}
With (EXPORT_OBJECT ){
DataType = "HTMLData ";
HTMLData = tableid. outerHTML;
Try {
ActiveSheet. Export ("d: \ table format .xls", 0 );
Alert ('excel sheet exported! ');
}
Catch (e ){
Alert ('failed to export the EXCEL table. Make sure that Excel2000 (or later) is installed and the xls file of the same name' is not opened ');
}
}
}
</Script>
<BODY>
<Table id = "tableid" border = "1">
<Tr>
<Td> the first cell </td>
<Td> the second cell </td>
<Td> the third cell </td>
</Tr>
<Tr>
<Td> cell 4 </td>
<Td> cell 5 </td>
<Td> cell 6 </td>
</Tr>
</Table>
<Input type = "button" value = "Export EXCEL table">
</BODY>
</HTML>
// ------------------------------------ 2 ----------------------------------\\
<! -- Export to word or excel of office2000 -->
<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 ("Microsoft Excel software is not installed on your computer! ");
Return false;
}
ExWBk. worksheets (1). Paste;
}
Function tableToWord (){
Var oWD = new ActiveXObject ("Word. Application ");
Var oDC = oWD. Documents. Add );
Var oRange = oDC. Range (0, 1 );
Var sel = document. body. createTextRange ();
Sel. moveToElementText (theObjTable );
Sel. select ();
Sel.exe cCommand ("Copy ");
ORange. Paste ();
OWD. Application. Visible = true;
}
</Script>
<Table id = "theObjTable">
<Tr>
<Td> On the html page </td>
<Td> change the table on the webpage </td>
<Td> content imported to </td>
<Td> word </td>
<Td> You can also import data to an excel file </td>
</Tr>
</Table>
<Input type = "button" value = "import to excel">
<Input type = "button" value = "import to word">
// ------------------------------------ 3 ----------------------------------\\
-- %>
**************************************** **************************************** **********************
5. How to Use js to directly convert content in webpages to excel
**************************************** **************************************** **********************
Copy codeThe Code is as follows:
<Html xmlns: o = "urn: schemas-microsoft-com: office"
Xmlns: x = "urn: schemas-microsoft-com: office: excel"
Xmlns = "http://www.w3.org/TR/REC-html40">
<Head>
<Meta http-equiv = Content-Type c>
<Script>
Function out (){
Try {
Var elTable = document. getElementById ("out ");
Var oRangeRef = document. body. createTextRange ();
ORangeRef. moveToElementText (elTable );
ORangeRef.exe cCommand ("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>
</Head>
<Body>
<Table id = 'out'>
<Tr>
<Td x: str bgcolor = "# ff00000"> 00001 </td>
<Td x: str> 0002 </td>
</Tr>
<Tr>
<Td height = 18 align = right style = 'height: 13.5pt 'x: str> 00001 </td>
<Td align = right x: str> 000002 </td>
</Tr>
<Tr>
<Td x: str> 0003 </td>
<Td x: str> 00003 </td>
</Tr>
</Table>
<INPUT type = "button" value = "Button" id = button1 name = button1>
</Body>
</Html>