How to export Web page content to Word and Excel using JavaScript

Source: Internet
Author: User

How to export Web page content to Word and Excel using JavaScript

This article mainly introduces how JavaScript exports Web page content to Word and Excel. It involves the use of javascript to operate the ActiveXObject control and has some reference value. For more information, see

 

 

This document describes how to export the content of a Web page to Word and Excel by using JavaScript. Share it with you for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

<HTML>

<HEAD>

<Title> How to export an EXCEL document from a WEB page

</Title>

</HEAD>

<Body>

<BR>

<Table id = "PrintA" width = "100%" border = "1" cellspacing = "0"

Cellpadding = "0" bgcolor = "#61FF13">

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

<TD> cell A </TD>

<TD> cell A </TD>

<TD> cell A </TD>

<TD> cell A </TD>

</TR>

<TR>

<TD colSpan = 4 style = "text-align: center;">

<Font color = "BLUE" face = "Verdana"> cell merge Row A </FONT>

</TD>

</TR>

</TABLE>

<BR>

<Table id = "PrintB" width = "100%" border = "1" cellspacing = "0"

Cellpadding = "0">

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

<TD> cell B </TD>

<TD> cell B </TD>

<TD> cell B </TD>

<TD> cell B </TD>

</TR>

<TR>

<TD colSpan = 4 style = "text-align: center;"> cell merge Row B </TD>

</TR>

</TABLE>

<Br>

<Input type = "button" onclick = "javascript: AllAreaWord ();"

Value = "export the content of the specified area to Word">

<Input type = "button" onclick = "javascript: AllAreaExcel ();"

Value = "export the content of a specified area to Excel">

<Input type = "button" onclick = "javascript: CellAreaExcel ();"

Value = "export the content of a form cell to Excel">

<Script language = "javascript">

// Import the content of the specified page area to Excel

Function AllAreaExcel ()

{

Var oXL = new ActiveXObject ("Excel. Application ");

Var oWB = oXL. Workbooks. Add ();

Var oSheet = oWB. ActiveSheet;

Var sel = document. body. createTextRange ();

Sel. moveToElementText (PrintA );

Sel. select ();

Sel.exe cCommand ("Copy ");

OSheet. Paste ();

OXL. Visible = true;

}

// Specify the "cell" content in the page area to import to Excel

Function CellAreaExcel ()

{

Var oXL = new ActiveXObject ("Excel. Application ");

Var oWB = oXL. Workbooks. Add ();

Var oSheet = oWB. ActiveSheet;

Var Lenr = PrintA. rows. length;

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

{

Var Lenc = PrintA. rows (I). cells. length;

For (j = 0; j <Lenc; j ++)

{

OSheet. Cells (I + 1, j + 1). value = PrintA. rows (I). cells (j). innerText;

}

}

OXL. Visible = true;

}

 

// Import the content of the specified page area to the Word

Function AllAreaWord ()

{

Var oWD = new ActiveXObject ("Word. Application ");

Var oDC = oWD. Documents. Add );

Var oRange = oDC. Range (0, 1 );

Var sel = document. body. createTextRange ();

Sel. moveToElementText (PrintA );

Sel. select ();

Sel.exe cCommand ("Copy ");

ORange. Paste ();

OWD. Application. Visible = true;

// Window. close ();

}

</SCRIPT>

</Body>

</Html>

I hope this article will help you design javascript programs.

Related Article

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.