【原创内容,转载请注明作者】
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
using
System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Web;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8
9 namespace FileManageDll
10 {
11 /// <summary>
12 /// 需要在调用的页面中必须重写VerifyRenderingInServerForm方法,否则将出现错误
13 /// public override void VerifyRenderingInServerForm( Control control )
14 /// {
15 /// }
16 ///
17 /// </summary>
18 public class HtmlDataToDoc
19 {
20 private Page _InvokePage;
21 /// <summary>
22 /// 调用的页面
23 /// </summary>
24 public Page InvokePage
25 {
26 get { return _InvokePage; }
27 set { _InvokePage = value; }
28 }
29
30 /// <summary>
31 /// 构造函数
32 /// </summary>
33 /// <param name="invoke_page"> 调用的页面,参数传this.Page </param>
34 public HtmlDataToDoc(Page invoke_page)
35 {
36 _InvokePage = invoke_page;
37 }
38
39 /// <summary>
40 /// Table导出excel
41 /// </summary>
42 /// <param name="tab"></param>
43 /// <param name="fileName"></param>
44 public void TableDataToExcel(System.Web.UI.HtmlControls.HtmlTable tab, string fileName)
45 {
46
47 if (fileName.ToLower().IndexOf( " .xls " ) == - 1 )
48 {
49 fileName = fileName + " .xls " ;
50 }
51
52 TableExport(tab, fileName, " application/ms-excel " );
53 }
54
55 /// <summary>
56 /// Table导出Word
57 /// </summary>
58 /// <param name="tab"></param>
59 /// <param name="fileName"></param>
60 public void TableDataToWord(System.Web.UI.HtmlControls.HtmlTable tab, string fileName)
61 {
62
63 if (fileName.ToLower().IndexOf( " .doc " ) == - 1 )
64 {
65 fileName = fileName + " .doc " ;
66 }
67
68 TableExport(tab, fileName, " application/ms-word " );
69 }
70
71 /// <summary>
72 /// 导入数据,保存文档
73 /// </summary>
74 /// <param name="tab"></param>
75 /// <param name="fileName"></param>
76 /// <param name="typeName"></param>
77 private void TableExport(System.Web.UI.HtmlControls.HtmlTable tab, string fileName, string typeName)
78 {
79 System.Web.HttpResponse httpResponse = _InvokePage.Response;
80 httpResponse.Clear();
81 httpResponse.Buffer = true ;
82 httpResponse.Charset = " GB2312 " ;
83 // Response.Charset = "UTF-8";
84 httpResponse.Write( " <meta http-equiv=Content-Type content=text/html;charset=GB2312> " );
85 httpResponse.AppendHeader( " Content-Disposition " , " attachment;filename= " + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8).ToString());
86 httpResponse.ContentEncoding = System.Text.Encoding.GetEncoding( " GB2312 " );
87 httpResponse.ContentType = typeName;
88 _InvokePage.EnableViewState = false ;
89 StringWriter tw = new StringWriter();
90 System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
91 tab.RenderControl(hw);
92 httpResponse.Write(tw.ToString());
93 httpResponse.End();
94 }
95 }
96
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Web;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8
9 namespace FileManageDll
10 {
11 /// <summary>
12 /// 需要在调用的页面中必须重写VerifyRenderingInServerForm方法,否则将出现错误
13 /// public override void VerifyRenderingInServerForm( Control control )
14 /// {
15 /// }
16 ///
17 /// </summary>
18 public class HtmlDataToDoc
19 {
20 private Page _InvokePage;
21 /// <summary>
22 /// 调用的页面
23 /// </summary>
24 public Page InvokePage
25 {
26 get { return _InvokePage; }
27 set { _InvokePage = value; }
28 }
29
30 /// <summary>
31 /// 构造函数
32 /// </summary>
33 /// <param name="invoke_page"> 调用的页面,参数传this.Page </param>
34 public HtmlDataToDoc(Page invoke_page)
35 {
36 _InvokePage = invoke_page;
37 }
38
39 /// <summary>
40 /// Table导出excel
41 /// </summary>
42 /// <param name="tab"></param>
43 /// <param name="fileName"></param>
44 public void TableDataToExcel(System.Web.UI.HtmlControls.HtmlTable tab, string fileName)
45 {
46
47 if (fileName.ToLower().IndexOf( " .xls " ) == - 1 )
48 {
49 fileName = fileName + " .xls " ;
50 }
51
52 TableExport(tab, fileName, " application/ms-excel " );
53 }
54
55 /// <summary>
56 /// Table导出Word
57 /// </summary>
58 /// <param name="tab"></param>
59 /// <param name="fileName"></param>
60 public void TableDataToWord(System.Web.UI.HtmlControls.HtmlTable tab, string fileName)
61 {
62
63 if (fileName.ToLower().IndexOf( " .doc " ) == - 1 )
64 {
65 fileName = fileName + " .doc " ;
66 }
67
68 TableExport(tab, fileName, " application/ms-word " );
69 }
70
71 /// <summary>
72 /// 导入数据,保存文档
73 /// </summary>
74 /// <param name="tab"></param>
75 /// <param name="fileName"></param>
76 /// <param name="typeName"></param>
77 private void TableExport(System.Web.UI.HtmlControls.HtmlTable tab, string fileName, string typeName)
78 {
79 System.Web.HttpResponse httpResponse = _InvokePage.Response;
80 httpResponse.Clear();
81 httpResponse.Buffer = true ;
82 httpResponse.Charset = " GB2312 " ;
83 // Response.Charset = "UTF-8";
84 httpResponse.Write( " <meta http-equiv=Content-Type content=text/html;charset=GB2312> " );
85 httpResponse.AppendHeader( " Content-Disposition " , " attachment;filename= " + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8).ToString());
86 httpResponse.ContentEncoding = System.Text.Encoding.GetEncoding( " GB2312 " );
87 httpResponse.ContentType = typeName;
88 _InvokePage.EnableViewState = false ;
89 StringWriter tw = new StringWriter();
90 System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
91 tab.RenderControl(hw);
92 httpResponse.Write(tw.ToString());
93 httpResponse.End();
94 }
95 }
96
调用时:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
protected
void
btnToWord_Click(
object
sender, EventArgs e)
{
string fileName = " fileName.doc " ;
FileManageDll.HtmlDataToDoc hdtd = new FileManageDll.HtmlDataToDoc( this .Page);
hdtd.TableDataToWord( this .TableId, fileName);
}
{
string fileName = " fileName.doc " ;
FileManageDll.HtmlDataToDoc hdtd = new FileManageDll.HtmlDataToDoc( this .Page);
hdtd.TableDataToWord( this .TableId, fileName);
}