net导出word、excel等文件操作类

来源:互联网 发布:金星舞蹈水平知乎 编辑:程序博客网 时间:2024/06/11 20:13

在做.NET项目时,经常会遇到要导出文件的问题,如将DataGrid中的数据导出到word、excel文件等。         下面是自己写的一个.net导出文件操作类,现把代码贴出来,以供参考,如有不当之处望予指正!

[csharp] view plaincopy
  1.   1using System;  
  2.   
  3.   2using System.Web;  
  4.   
  5.   3namespace FLX.ComplexQuery  
  6.   
  7.   4{  
  8.   
  9.   5    /**//// <summary>  
  10.   
  11.   6    /// 彭建军  
  12.   
  13.   7    /// 导出报表数据存入word或execl文件  
  14.   
  15.   8    /// 2008-06-19  
  16.   
  17.   9    /// </summary>  
  18.   
  19.  10    public class ExportData  
  20.   
  21.  11    {  
  22.   
  23.  12        构造函数#region 构造函数  
  24.   
  25.  13        public ExportData()  
  26.   
  27.  14        {  
  28.   
  29.  15            //  
  30.   
  31.  16            // TODO: 在此处添加构造函数逻辑  
  32.   
  33.  17            //  
  34.   
  35.  18        }  
  36.   
  37.  19        #endregion  
  38.   
  39.  20  
  40.   
  41.  21        导出页面或web控件方法#region 导出页面或web控件方法  
  42.   
  43.  22        /**//// <summary>  
  44.   
  45.  23        /// 将Web控件或页面信息导出(不带文件名参数)  
  46.   
  47.  24        /// </summary>  
  48.   
  49.  25        /// <param name="source">控件实例</param>          
  50.   
  51.  26        /// <param name="DocumentType">导出类型:Excel或Word</param>  
  52.   
  53.  27        public void ExportControl(System.Web.UI.Control source, string DocumentType)  
  54.   
  55.  28        {  
  56.   
  57.  29            //设置Http的头信息,编码格式  
  58.   
  59.  30            if (DocumentType == "Excel")  
  60.   
  61.  31            {  
  62.   
  63.  32                //Excel              
  64.   
  65.  33                HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+ HttpUtility.UrlEncode("下载文件.xls",System.Text.Encoding.UTF8));  
  66.   
  67.  34                HttpContext.Current.Response.ContentType = "application/ms-excel";  
  68.   
  69.  35            }  
  70.   
  71.  36  
  72.   
  73.  37            else if (DocumentType == "Word")  
  74.   
  75.  38            {  
  76.   
  77.  39                //Word  
  78.   
  79.  40                HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+ HttpUtility.UrlEncode("下载文件.doc",System.Text.Encoding.UTF8));  
  80.   
  81.  41                HttpContext.Current.Response.ContentType = "application/ms-word";  
  82.   
  83.  42            }  
  84.   
  85.  43  
  86.   
  87.  44            HttpContext.Current.Response.Charset = "UTF-8";     
  88.   
  89.  45            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;   
  90.   
  91.  46  
  92.   
  93.  47            //关闭控件的视图状态  
  94.   
  95.  48            source.Page.EnableViewState =false;      
  96.   
  97.  49  
  98.   
  99.  50            //初始化HtmlWriter  
  100.   
  101.  51            System.IO.StringWriter writer = new System.IO.StringWriter() ;  
  102.   
  103.  52            System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);  
  104.   
  105.  53            source.RenderControl(htmlWriter);   
  106.   
  107.  54  
  108.   
  109.  55            //输出  
  110.   
  111.  56            HttpContext.Current.Response.Write(writer.ToString());  
  112.   
  113.  57            HttpContext.Current.Response.End();  
  114.   
  115.  58        }  
  116.   
  117.  59  
  118.   
  119.  60        /**//// <summary>  
  120.   
  121.  61        /// 将Web控件或页面信息导出(带文件名参数)  
  122.   
  123.  62        /// </summary>  
  124.   
  125.  63        /// <param name="source">控件实例</param>          
  126.   
  127.  64        /// <param name="DocumentType">导出类型:Excel或Word</param>  
  128.   
  129.  65        /// <param name="filename">保存文件名</param>  
  130.   
  131.  66        public void ExportControl(System.Web.UI.Control source, string DocumentType, string filename)  
  132.   
  133.  67        {  
  134.   
  135.  68            //设置Http的头信息,编码格式  
  136.   
  137.  69            if (DocumentType == "Excel")  
  138.   
  139.  70            {  
  140.   
  141.  71                //Excel              
  142.   
  143.  72                HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+ HttpUtility.UrlEncode(filename+".xls",System.Text.Encoding.UTF8));  
  144.   
  145.  73                HttpContext.Current.Response.ContentType = "application/ms-excel";              
  146.   
  147.  74            }  
  148.   
  149.  75  
  150.   
  151.  76            else if (DocumentType == "Word")  
  152.   
  153.  77            {  
  154.   
  155.  78                //Word  
  156.   
  157.  79                HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+ HttpUtility.UrlEncode(filename+".doc",System.Text.Encoding.UTF8));  
  158.   
  159.  80                HttpContext.Current.Response.ContentType = "application/ms-word";  
  160.   
  161.  81            }  
  162.   
  163.  82  
  164.   
  165.  83            HttpContext.Current.Response.Charset = "UTF-8";     
  166.   
  167.  84            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;   
  168.   
  169.  85  
  170.   
  171.  86            //关闭控件的视图状态  
  172.   
  173.  87            source.Page.EnableViewState =false;      
  174.   
  175.  88  
  176.   
  177.  89            //初始化HtmlWriter  
  178.   
  179.  90            System.IO.StringWriter writer = new System.IO.StringWriter() ;  
  180.   
  181.  91            System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);  
  182.   
  183.  92            source.RenderControl(htmlWriter);   
  184.   
  185.  93  
  186.   
  187.  94            //输出  
  188.   
  189.  95            HttpContext.Current.Response.Write(writer.ToString());  
  190.   
  191.  96            HttpContext.Current.Response.End();  
  192.   
  193.  97        }  
  194.   
  195.  98        #endregion  
  196.   
  197.  99  
  198.   
  199. 100        调用说明#region 调用说明  
  200.   
  201. 101        //方法ExportControl(System.Web.UI.Control source, string DocumentType,string filename)中  
  202.   
  203. 102        //第一个参数source表示导出的页面或控件名,当为datagrid或dataList控件时,在导出Excel/word文件时,必须把控件的分页、排序等属性去除并重新绑定,  
  204.   
  205. 103        //第二个参数DocumentType表示导出的文件类型word或excel  
  206.   
  207. 104        //第三个参数filename表示需要导出的文件所取的文件名  
  208.   
  209. 105        //调用方法:  
  210.   
  211. 106        //ExportData export=new ExportData();  
  212.   
  213. 107        //export.ExportControl(this, "Word","testfilename");//当为this时表示当前页面  
  214.   
  215. 108        //这是将整个页面导出为Word,并命名为testfilename  
  216.   
  217. 109        #endregion  
  218.   
  219. 110    }  
  220.   
  221. 111}  
  222.   
  223. 112  
  224. 说明一下,类中的<font face="Verdana">HttpContext.Current.Response.ContentType表示要导出文件的类型,  
  225.     下面是对<font face="Verdana">Response.ContentType类型的汇总</font></font>  
[csharp] view plaincopy
  1. <pre class="csharp" name="code">在ASP.NET中使用Response.ContentType="类型名";来确定输出格式   
  2.   
  3. 'ez' => 'application/andrew-inset',    
  4.   
  5. 'hqx' => 'application/mac-binhex40',    
  6.   
  7. 'cpt' => 'application/mac-compactpro',    
  8.   
  9. 'doc' => 'application/msword',    
  10.   
  11. 'bin' => 'application/octet-stream',    
  12.   
  13. 'dms' => 'application/octet-stream',    
  14.   
  15. 'lha' => 'application/octet-stream',    
  16.   
  17. 'lzh' => 'application/octet-stream',    
  18.   
  19. 'exe' => 'application/octet-stream',    
  20.   
  21. 'class' => 'application/octet-stream',    
  22.   
  23. 'so' => 'application/octet-stream',    
  24.   
  25. 'dll' => 'application/octet-stream',    
  26.   
  27. 'oda' => 'application/oda',    
  28.   
  29. 'pdf' => 'application/pdf',    
  30.   
  31. 'ai' => 'application/postscript',    
  32.   
  33. 'eps' => 'application/postscript',    
  34.   
  35. 'ps' => 'application/postscript',    
  36.   
  37. 'smi' => 'application/smil',    
  38.   
  39. 'smil' => 'application/smil',    
  40.   
  41. 'mif' => 'application/vnd.mif',    
  42.   
  43. 'xls' => 'application/vnd.ms-excel',    
  44.   
  45. 'ppt' => 'application/vnd.ms-powerpoint',    
  46.   
  47. 'wbxml' => 'application/vnd.wap.wbxml',    
  48.   
  49. 'wmlc' => 'application/vnd.wap.wmlc',    
  50.   
  51. 'wmlsc' => 'application/vnd.wap.wmlscriptc',    
  52.   
  53. 'bcpio' => 'application/x-bcpio',    
  54.   
  55. 'vcd' => 'application/x-cdlink',    
  56.   
  57. 'pgn' => 'application/x-chess-pgn',    
  58.   
  59. 'cpio' => 'application/x-cpio',    
  60.   
  61. 'csh' => 'application/x-csh',    
  62.   
  63. 'dcr' => 'application/x-director',    
  64.   
  65. 'dir' => 'application/x-director',    
  66.   
  67. 'dxr' => 'application/x-director',    
  68.   
  69. 'dvi' => 'application/x-dvi',    
  70.   
  71. 'spl' => 'application/x-futuresplash',    
  72.   
  73. 'gtar' => 'application/x-gtar',    
  74.   
  75. 'hdf' => 'application/x-hdf',    
  76.   
  77. 'js' => 'application/x-javascript',    
  78.   
  79. 'skp' => 'application/x-koan',    
  80.   
  81. 'skd' => 'application/x-koan',    
  82.   
  83. 'skt' => 'application/x-koan',    
  84.   
  85. 'skm' => 'application/x-koan',    
  86.   
  87. 'latex' => 'application/x-latex',    
  88.   
  89. 'nc' => 'application/x-netcdf',    
  90.   
  91. 'cdf' => 'application/x-netcdf',    
  92.   
  93. 'sh' => 'application/x-sh',    
  94.   
  95. 'shar' => 'application/x-shar',    
  96.   
  97. 'swf' => 'application/x-shockwave-flash',    
  98.   
  99. 'sit' => 'application/x-stuffit',    
  100.   
  101. 'sv4cpio' => 'application/x-sv4cpio',    
  102.   
  103. 'sv4crc' => 'application/x-sv4crc',    
  104.   
  105. 'tar' => 'application/x-tar',    
  106.   
  107. 'tcl' => 'application/x-tcl',    
  108.   
  109. 'tex' => 'application/x-tex',    
  110.   
  111. 'texinfo' => 'application/x-texinfo',    
  112.   
  113. 'texi' => 'application/x-texinfo',    
  114.   
  115. 't' => 'application/x-troff',    
  116.   
  117. 'tr' => 'application/x-troff',    
  118.   
  119. 'roff' => 'application/x-troff',    
  120.   
  121. 'man' => 'application/x-troff-man',    
  122.   
  123. 'me' => 'application/x-troff-me',    
  124.   
  125. 'ms' => 'application/x-troff-ms',    
  126.   
  127. 'ustar' => 'application/x-ustar',    
  128.   
  129. 'src' => 'application/x-wais-source',    
  130.   
  131. 'xhtml' => 'application/xhtml+xml',    
  132.   
  133. 'xht' => 'application/xhtml+xml',    
  134.   
  135. 'zip' => 'application/zip',    
  136.   
  137. 'au' => 'audio/basic',    
  138.   
  139. 'snd' => 'audio/basic',    
  140.   
  141. 'mid' => 'audio/midi',    
  142.   
  143. 'midi' => 'audio/midi',    
  144.   
  145. 'kar' => 'audio/midi',    
  146.   
  147. 'mpga' => 'audio/mpeg',    
  148.   
  149. 'mp2' => 'audio/mpeg',    
  150.   
  151. 'mp3' => 'audio/mpeg',    
  152.   
  153. 'aif' => 'audio/x-aiff',    
  154.   
  155. 'aiff' => 'audio/x-aiff',    
  156.   
  157. 'aifc' => 'audio/x-aiff',    
  158.   
  159. 'm3u' => 'audio/x-mpegurl',    
  160.   
  161. 'ram' => 'audio/x-pn-realaudio',    
  162.   
  163. 'rm' => 'audio/x-pn-realaudio',    
  164.   
  165. 'rpm' => 'audio/x-pn-realaudio-plugin',    
  166.   
  167. 'ra' => 'audio/x-realaudio',    
  168.   
  169. 'wav' => 'audio/x-wav',    
  170.   
  171. 'pdb' => 'chemical/x-pdb',    
  172.   
  173. 'xyz' => 'chemical/x-xyz',    
  174.   
  175. 'bmp' => 'image/bmp',    
  176.   
  177. 'gif' => 'image/gif',    
  178.   
  179. 'ief' => 'image/ief',    
  180.   
  181. 'jpeg' => 'image/jpeg',    
  182.   
  183. 'jpg' => 'image/jpeg',    
  184.   
  185. 'jpe' => 'image/jpeg',    
  186.   
  187. 'png' => 'image/png',    
  188.   
  189. 'tiff' => 'image/tiff',    
  190.   
  191. 'tif' => 'image/tiff',    
  192.   
  193. 'djvu' => 'image/vnd.djvu',    
  194.   
  195. 'djv' => 'image/vnd.djvu',    
  196.   
  197. 'wbmp' => 'image/vnd.wap.wbmp',    
  198.   
  199. 'ras' => 'image/x-cmu-raster',    
  200.   
  201. 'pnm' => 'image/x-portable-anymap',    
  202.   
  203. 'pbm' => 'image/x-portable-bitmap',    
  204.   
  205. 'pgm' => 'image/x-portable-graymap',    
  206.   
  207. 'ppm' => 'image/x-portable-pixmap',    
  208.   
  209. 'rgb' => 'image/x-rgb',    
  210.   
  211. 'xbm' => 'image/x-xbitmap',    
  212.   
  213. 'xpm' => 'image/x-xpixmap',    
  214.   
  215. 'xwd' => 'image/x-xwindowdump',    
  216.   
  217. 'igs' => 'model/iges',    
  218.   
  219. 'iges' => 'model/iges',    
  220.   
  221. 'msh' => 'model/mesh',    
  222.   
  223. 'mesh' => 'model/mesh',    
  224.   
  225. 'silo' => 'model/mesh',    
  226.   
  227. 'wrl' => 'model/vrml',    
  228.   
  229. 'vrml' => 'model/vrml',    
  230.   
  231. 'css' => 'text/css',    
  232.   
  233. 'html' => 'text/html',    
  234.   
  235. 'htm' => 'text/html',    
  236.   
  237. 'asc' => 'text/plain',    
  238.   
  239. 'txt' => 'text/plain',    
  240.   
  241. 'rtx' => 'text/richtext',    
  242.   
  243. 'rtf' => 'text/rtf',    
  244.   
  245. 'sgml' => 'text/sgml',    
  246.   
  247. 'sgm' => 'text/sgml',    
  248.   
  249. 'tsv' => 'text/tab-separated-values',    
  250.   
  251. 'wml' => 'text/vnd.wap.wml',    
  252.   
  253. 'wmls' => 'text/vnd.wap.wmlscript',    
  254.   
  255. 'etx' => 'text/x-setext',    
  256.   
  257. 'xsl' => 'text/xml',    
  258.   
  259. 'xml' => 'text/xml',    
  260.   
  261. 'mpeg' => 'video/mpeg',    
  262.   
  263. 'mpg' => 'video/mpeg',    
  264.   
  265. 'mpe' => 'video/mpeg',    
  266.   
  267. 'qt' => 'video/quicktime',    
  268.   
  269. 'mov' => 'video/quicktime',    
  270.   
  271. 'mxu' => 'video/vnd.mpegurl',    
  272.   
  273. 'avi' => 'video/x-msvideo',    
  274.   
  275. 'movie' => 'video/x-sgi-movie',    
  276.   
  277. 'ice' => 'x-conference/x-cooltalk'  </pre>  
原创粉丝点击