1 /// <summary>
2 // set the Word template. The word table style is set here.
3 /// </summary>
4 /// <param name = "dsTr"> </param>
5 /// <returns> </returns>
6 protected string SetWordTemplate (string dsTr)
7 {
8 StringBuilder html = new StringBuilder ();
9
10 html. Append ("<! DOCTYPE html PUBLIC '-// W3C // dtd xhtml 1.0 Transitional // en' 'HTTP: // www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> ");
11 html. Append ("12 html. Append ("13 html. Append ("<body> ");
14 // html. Append ("<table cellpadding = '0' cellspacing = '0' border = '1'> ");
15 html. Append (dsTr );
16 // html. Append ("</table> ");
17 html. Append ("</body> ");
18 html. Append ("19
20 return html. ToString ();
21}
22
23 /// <summary>
24 // Save the Word
25 /// </summary>
26 /// <param name = "savePath"> </param>
27 /// <param name = "data"> </param>
28 /// <returns> </returns>
29 protected bool SaveWord (string savePath, string data)
30 {
31 try
32 {
33 using (StreamWriter sw = new StreamWriter (savePath, true, Encoding. Default ))
34 {
35 sw. WriteLine (data );
36
37 sw. Close ();
38 sw. Dispose ();
39}
40
41 return true;
42}
43 catch
44 {
45 return false;
46}
47
48 return false;
49}
50
51 // <summary>
52 // Import
53 /// </summary>
54 /// <param name = "sender"> </param>
55 // <param name = "e"> </param>
56 private void button#click (object sender, EventArgs e)
57 {
58 // save path
59 string path = string. Format ("{0} {1}", Application. StartupPath, "save physical path ");
60
61 // multiple tables the dataset here needs to be replaced by the dataset queried from the database
62 foreach (DataTable dt in new DataSet (). Tables)
63 {
64 StringBuilder _ tables = new StringBuilder ();
65 for (int I = 0; I <dt. Rows. Count; I ++)
66 {
67 if (I = 0)
68 {
69 _ tables. Append ("<table cellpadding = '0' cellspacing = '0' border = '1'> ");
70 _ tables. Append ("<tr> <td> table name </td> </tr> ");
71}
72
73 _ tables. append ("<tr> <td>" + I + "</td> <td>" + I + "</td> <td>" + I + "</ td> </tr> ");
74
75 if (I = (dt. Rows. Count-1 ))
76 _ tables. Append ("</table> ");
77}
78
79 SaveWord (path, this. SetWordTemplate (_ tables. ToString ()));
80}
81
82 MessageBox. Show ("generated successfully! ");
83}
84
85
86
87 hey! Success! This method is quick, but other paging functions of word cannot be set. However, it is more than enough to import all tables from the database to wrod !~
88
89 I am a little bird. If you have any good Code, please advise me !~ Thanks
90