史上最简单将数据信息导入wrod文档方案(C# for word)
1 /// <summary>
2 /// 设置Word模板,word表格样式在此设置
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("<html xmlns='http://www.w3.org/1999/xhtml'>");
12 html.Append("<html><head><title>测试word</title></head>");
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("</html>");
19
20 return html.ToString();
21 }
22
23 /// <summary>
24 /// 保存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 /// 导入
53 /// </summary>
54 /// <param name="sender"></param>
55 /// <param name="e"></param>
56 private void button1_Click(object sender, EventArgs e)
57 {
58 //保存路径
59 string path = string.Format("{0}{1}", Application.StartupPath, "保存物理路径");
60
61 //多个表 这里的dataset需要换成从数据库查询出来的dataset
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>表名字</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("生成成功!");
83 }
84
85
86
87 嘿嘿!大功告成啦!这种方法快捷,但是不能设置word其他的分页功能等等,不过应对于从数据库中把所有表导入到wrod是绰绰有余啦!~
88
89 俺乃小小小小鸟儿,如过哪位大大大大鸟有更好的方案好的Code请多多指教哦!~Thanks
90
2 /// 设置Word模板,word表格样式在此设置
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("<html xmlns='http://www.w3.org/1999/xhtml'>");
12 html.Append("<html><head><title>测试word</title></head>");
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("</html>");
19
20 return html.ToString();
21 }
22
23 /// <summary>
24 /// 保存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 /// 导入
53 /// </summary>
54 /// <param name="sender"></param>
55 /// <param name="e"></param>
56 private void button1_Click(object sender, EventArgs e)
57 {
58 //保存路径
59 string path = string.Format("{0}{1}", Application.StartupPath, "保存物理路径");
60
61 //多个表 这里的dataset需要换成从数据库查询出来的dataset
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>表名字</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("生成成功!");
83 }
84
85
86
87 嘿嘿!大功告成啦!这种方法快捷,但是不能设置word其他的分页功能等等,不过应对于从数据库中把所有表导入到wrod是绰绰有余啦!~
88
89 俺乃小小小小鸟儿,如过哪位大大大大鸟有更好的方案好的Code请多多指教哦!~Thanks
90