【最简单的】.net导出word、excel文件操作类
在做.NET项目时,经常会遇到要导出文件的问题,如将DataGrid中的数据导出到word、excel文件等。
下面是自己写的一个.net导出文件操作类,现把代码贴出来,以供参考,如有不当之处望予指正!
1using System;
2using System.Web;
3namespace FLX.ComplexQuery
4{
5 /// <summary>
7 /// 导出报表数据存入word或execl文件
9 /// </summary>
10 public class ExportData
11 {
12 #region 构造函数
13 public ExportData()
14 {
15 //
16 // TODO: 在此处添加构造函数逻辑
17 //
18 }
19 #endregion
20
21 #region 导出页面或web控件方法
22 /// <summary>
23 /// 将Web控件或页面信息导出(不带文件名参数)
24 /// </summary>
25 /// <param name="source">控件实例</param>
26 /// <param name="DocumentType">导出类型:Excel或Word</param>
27 public void ExportControl(System.Web.UI.Control source, string DocumentType)
28 {
29 //设置Http的头信息,编码格式
30 if (DocumentType == "Excel")
31 {
32 //Excel
33 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+ HttpUtility.UrlEncode("下载文件.xls",System.Text.Encoding.UTF8));
34 HttpContext.Current.Response.ContentType = "application/ms-excel";
35 }
36
37 else if (DocumentType == "Word")
38 {
39 //Word
40 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+ HttpUtility.UrlEncode("下载文件.doc",System.Text.Encoding.UTF8));
41 HttpContext.Current.Response.ContentType = "application/ms-word";
42 }
43
44 HttpContext.Current.Response.Charset = "UTF-8";
45 HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
46
47 //关闭控件的视图状态
48 source.Page.EnableViewState =false;
49
50 //初始化HtmlWriter
51 System.IO.StringWriter writer = new System.IO.StringWriter() ;
52 System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
53 source.RenderControl(htmlWriter);
54
55 //输出
56 HttpContext.Current.Response.Write(writer.ToString());
57 HttpContext.Current.Response.End();
58 }
59
60 /// <summary>
61 /// 将Web控件或页面信息导出(带文件名参数)
62 /// </summary>
63 /// <param name="source">控件实例</param>
64 /// <param name="DocumentType">导出类型:Excel或Word</param>
65 /// <param name="filename">保存文件名</param>
66 public void ExportControl(System.Web.UI.Control source, string DocumentType, string filename)
67 {
68 //设置Http的头信息,编码格式
69 if (DocumentType == "Excel")
70 {
71 //Excel
72 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+ HttpUtility.UrlEncode(filename+".xls",System.Text.Encoding.UTF8));
73 HttpContext.Current.Response.ContentType = "application/ms-excel";
74 }
75
76 else if (DocumentType == "Word")
77 {
78 //Word
79 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+ HttpUtility.UrlEncode(filename+".doc",System.Text.Encoding.UTF8));
80 HttpContext.Current.Response.ContentType = "application/ms-word";
81 }
82
83 HttpContext.Current.Response.Charset = "UTF-8";
84 HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
85
86 //关闭控件的视图状态
87 source.Page.EnableViewState =false;
88
89 //初始化HtmlWriter
90 System.IO.StringWriter writer = new System.IO.StringWriter() ;
91 System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
92 source.RenderControl(htmlWriter);
93
94 //输出
95 HttpContext.Current.Response.Write(writer.ToString());
96 HttpContext.Current.Response.End();
97 }
98 #endregion
99
100 #region 调用说明
101 //方法ExportControl(System.Web.UI.Control source, string DocumentType,string filename)中
102 //第一个参数source表示导出的页面或控件名,当为datagrid或dataList控件时,在导出Excel/word文件时,必须把控件的分页、排序等属性去除并重新绑定,
103 //第二个参数DocumentType表示导出的文件类型word或excel
104 //第三个参数filename表示需要导出的文件所取的文件名
105 //调用方法:
106 //ExportData export=new ExportData();
107 //export.ExportControl(this, "Word","testfilename");//当为this时表示当前页面
108 //这是将整个页面导出为Word,并命名为testfilename
109 #endregion
110 }
111}
112
说明一下,类中的HttpContext.Current.Response.ContentType表示要导出文件的类型,
下面是对Response.ContentType类型的汇总
在ASP.NET中使用Response.ContentType="类型名";来确定输出格式
'ez' => 'application/andrew-inset',
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'doc' => 'application/msword',
'bin' => 'application/octet-stream',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'so' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'bcpio' => 'application/x-bcpio',
'vcd' => 'application/x-cdlink',
'pgn' => 'application/x-chess-pgn',
'cpio' => 'application/x-cpio',
'csh' => 'application/x-csh',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'spl' => 'application/x-futuresplash',
'gtar' => 'application/x-gtar',
'hdf' => 'application/x-hdf',
'js' => 'application/x-javascript',
'skp' => 'application/x-koan',
'skd' => 'application/x-koan',
'skt' => 'application/x-koan',
'skm' => 'application/x-koan',
'latex' => 'application/x-latex',
'nc' => 'application/x-netcdf',
'cdf' => 'application/x-netcdf',
'sh' => 'application/x-sh',
'shar' => 'application/x-shar',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
'tar' => 'application/x-tar',
'tcl' => 'application/x-tcl',
'tex' => 'application/x-tex',
'texinfo' => 'application/x-texinfo',
'texi' => 'application/x-texinfo',
't' => 'application/x-troff',
'tr' => 'application/x-troff',
'roff' => 'application/x-troff',
'man' => 'application/x-troff-man',
'me' => 'application/x-troff-me',
'ms' => 'application/x-troff-ms',
'ustar' => 'application/x-ustar',
'src' => 'application/x-wais-source',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/zip',
'au' => 'audio/basic',
'snd' => 'audio/basic',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'kar' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'm3u' => 'audio/x-mpegurl',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'wav' => 'audio/x-wav',
'pdb' => 'chemical/x-pdb',
'xyz' => 'chemical/x-xyz',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'ief' => 'image/ief',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'djvu' => 'image/vnd.djvu',
'djv' => 'image/vnd.djvu',
'wbmp' => 'image/vnd.wap.wbmp',
'ras' => 'image/x-cmu-raster',
'pnm' => 'image/x-portable-anymap',
'pbm' => 'image/x-portable-bitmap',
'pgm' => 'image/x-portable-graymap',
'ppm' => 'image/x-portable-pixmap',
'rgb' => 'image/x-rgb',
'xbm' => 'image/x-xbitmap',
'xpm' => 'image/x-xpixmap',
'xwd' => 'image/x-xwindowdump',
'igs' => 'model/iges',
'iges' => 'model/iges',
'msh' => 'model/mesh',
'mesh' => 'model/mesh',
'silo' => 'model/mesh',
'wrl' => 'model/vrml',
'vrml' => 'model/vrml',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'asc' => 'text/plain',
'txt' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'sgml' => 'text/sgml',
'sgm' => 'text/sgml',
'tsv' => 'text/tab-separated-values',
'wml' => 'text/vnd.wap.wml',
'wmls' => 'text/vnd.wap.wmlscript',
'etx' => 'text/x-setext',
'xsl' => 'text/xml',
'xml' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mxu' => 'video/vnd.mpegurl',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'ice' => 'x-conference/x-cooltalk'