1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5.  
  6. public class ExportOffice
  7. {
  8. //导出页面或web控件方法#region 导出页面或web控件方法
  9. /**/
  10. /// <summary>
  11. /// 将Web控件或页面信息导出(不带文件名参数)
  12. /// </summary>
  13. /// <param name="source">控件实例</param>
  14. /// <param name="DocumentType">导出类型:Excel或Word</param>
  15. public void ExportControl(System.Web.UI.Control source, string DocumentType)
  16. {
  17. //设置Http的头信息,编码格式
  18. if (DocumentType == "Excel")
  19. {
  20. //Excel
  21. HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("下载文件.xls", System.Text.Encoding.UTF8));
  22. HttpContext.Current.Response.ContentType = "application/ms-excel";
  23. }
  24.  
  25. else if (DocumentType == "Word")
  26. {
  27. //Word
  28. HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("下载文件.doc", System.Text.Encoding.UTF8));
  29. HttpContext.Current.Response.ContentType = "application/ms-word";
  30. }
  31.  
  32. HttpContext.Current.Response.Charset = "UTF-8";
  33. HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
  34.  
  35. //关闭控件的视图状态
  36. source.Page.EnableViewState = false;
  37.  
  38. //初始化HtmlWriter
  39. System.IO.StringWriter writer = new System.IO.StringWriter();
  40. System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
  41. source.RenderControl(htmlWriter);
  42.  
  43. //输出
  44. HttpContext.Current.Response.Write(writer.ToString());
  45. HttpContext.Current.Response.End();
  46. }
  47.  
  48. /**/
  49. /// <summary>
  50. /// 将Web控件或页面信息导出(带文件名参数)
  51. /// </summary>
  52. /// <param name="source">控件实例</param>
  53. /// <param name="DocumentType">导出类型:Excel或Word</param>
  54. /// <param name="filename">保存文件名</param>
  55. public void ExportControl(System.Web.UI.Control source, string DocumentType, string filename)
  56. {
  57. //设置Http的头信息,编码格式
  58. if (DocumentType == "Excel")
  59. {
  60. //Excel
  61. HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".xls", System.Text.Encoding.UTF8));
  62. HttpContext.Current.Response.ContentType = "application/ms-excel";
  63. }
  64.  
  65. else if (DocumentType == "Word")
  66. {
  67. //Word
  68. HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".doc", System.Text.Encoding.UTF8));
  69. HttpContext.Current.Response.ContentType = "application/ms-word";
  70. }
  71.  
  72. HttpContext.Current.Response.Charset = "UTF-8";
  73. HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
  74.  
  75. //关闭控件的视图状态
  76. source.Page.EnableViewState = false;
  77.  
  78. //初始化HtmlWriter
  79. System.IO.StringWriter writer = new System.IO.StringWriter();
  80. System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
  81. source.RenderControl(htmlWriter);
  82.  
  83. //输出
  84. HttpContext.Current.Response.Write(writer.ToString());
  85. HttpContext.Current.Response.End();
  86. }
  87. #region 调用说明
  88. //方法ExportControl(System.Web.UI.Control source, string DocumentType,string filename)中
  89. //第一个参数source表示导出的页面或控件名,当为datagrid或dataList控件时,在导出Excel/word文件时,必须把控件的分页、排序等属性去除并重新绑定,
  90. //第二个参数DocumentType表示导出的文件类型word或excel
  91. //第三个参数filename表示需要导出的文件所取的文件名
  92. //调用方法:
  93. //ExportData export=new ExportData();
  94. //export.ExportControl(this, "Word","testfilename");//当为this时表示当前页面
  95. //这是将整个页面导出为Word,并命名为testfilename
  96. #endregion
  97. }

  

  1. <!DOCTYPE html>
  2.  
  3. <html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'>
  4. <head runat="server">
  5. <!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:TrackMoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val='Cambria Math'/><m:brkBin m:val='before'/><m:brkBinSub m:val='--'/><m:smallFrac m:val='off'/><m:dispDef/><m:lMargin m:val='0'/> <m:rMargin m:val='0'/><m:defJc m:val='centerGroup'/><m:wrapIndent m:val='1440'/><m:intLim m:val='subSup'/><m:naryLim m:val='undOvr'/></m:mathPr></w:WordDocument></xml><![endif]-->
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  7. <title></title>
  8. <style>
  9. table{ border: solid 1px black;border-collapse: collapse;}
  10. td{width: 200px;border: solid 1px black;word-wrap: break-word; word-break: break-all;}
  11. body,tr,td{font-size:14px;}
  12. </style>
  13. </head>
  14. <body>
  15. <form id="form1" runat="server">
  16. <table style="margin:0px auto;" border="1" >
  17. <caption style="font:20px/30px Arail;"><asp:Literal runat="server" ID="lbl_Caption"></asp:Literal></caption>
  18. <tr>
  19. <td><asp:Literal runat="server" ID="lbl_username_title"></asp:Literal></td>
  20. <td><asp:Literal runat="server" ID="lbl_username_value"></asp:Literal></td>
  21. <td><asp:Literal runat="server" ID="lbl_userCode_title"></asp:Literal></td>
  22. <td><asp:Literal runat="server" ID="lbl_userCode_value"></asp:Literal></td>
  23. </tr>
  24. <tr>
  25. <td><asp:Literal runat="server" ID="lbl_sex_title"></asp:Literal></td>
  26. <td><asp:Literal runat="server" ID="lbl_sex_value"></asp:Literal></td>
  27. <td><asp:Literal runat="server" ID="lbl_birthday_title"></asp:Literal></td>
  28. <td><asp:Literal runat="server" ID="lbl_birthday_value"></asp:Literal></td>
  29. </tr>
  30. <tr>
  31. <td><asp:Literal runat="server" ID="lbl_nationality_title"></asp:Literal></td>
  32. <td><asp:Literal runat="server" ID="lbl_nationality_value"></asp:Literal></td>
  33. <td><asp:Literal runat="server" ID="lbl_company_title"></asp:Literal></td>
  34. <td><asp:Literal runat="server" ID="lbl_company_value"></asp:Literal></td>
  35. </tr>
  36. <tr>
  37. <td><asp:Literal runat="server" ID="lbl_hospitalName_title"></asp:Literal></td>
  38. <td><asp:Literal runat="server" ID="lbl_hospitalName_value"></asp:Literal></td>
  39. <td><asp:Literal runat="server" ID="lbl_tjDate_title"></asp:Literal></td>
  40. <td><asp:Literal runat="server" ID="lbl_tjDate_value"></asp:Literal></td>
  41. </tr>
  42. <asp:Literal runat="server" ID="lbl_data"> </asp:Literal>
  43. </table>
  44. </form>
  45. </body>
  46. </html>

  

.Net导出Word和Excel的更多相关文章

  1. 通过swagger json一键解析为html页面、导出word和excel的解析算法分享

    写在前面: 完全通过Spring Boot工程 Java代码,将swagger json 一键解析为html页面.导出word和execel的解析算法,不需要任何网上那些类似于“SwaggerMark ...

  2. 在Java中导出word、excel格式文件时JSP页面头的设置

    我们在JSP中往往会把一些表格里的东西需要导出到本地,一般都是导成word.excel格式的文件.这只需要在JSP页面头设置及在<head></head>标签中添加下面的代码: ...

  3. 【C#点滴记录】ASP.NET 使用C# 导出Word 和Excel

    原文摘自 慧优米网,链接地址:http://huiyoumi.wang/upload/forum.php?mod=viewthread&tid=797&extra= 好了正文来了 今天 ...

  4. C#导出Word或Excel文件总显示Html标记

    原因:Word或Excel文件包含的GridView没有查询到数据.

  5. poi导出word、excel

    在实际的项目开发中,经常会有一些涉及到导入导出的文档的功能.apache开源项目之一poi对此有很好的支持,对之前的使用做一些简要的总结. 1,导入jar 为了保证对格式的兼容性,在项目的pom.xm ...

  6. JS019. 原生JS使用new Blob()实现带格式导出Word、Excel(提供无编程基础将页面上表格导出到本地的方法)

    导出效果 代码实现 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  7. Java使用freemarker导出word和excel

    www.linxiaosheng.com/post/2013-12-05/40060346181 https://github.com/upyun/java-sdk

  8. .net core mvc部署到IIS导出Word 提示80070005拒绝访问

    项目中相信大家经常会遇到导出Word.Excel等需求,在实际开发环境中,一般不会出现什么问题,但当发布到IIS上后可能会遇到各种各样的问题,一般都是权限的问题.前几天把公司项目发布后,出现Word导 ...

  9. 导出Excel And 导出word

      <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx. ...

随机推荐

  1. selenium安装及官方文档

    selenium-python官方文档: https://selenium-python.readthedocs.io/ python3.5已安装的情况下,安装示意图如下 命令行输入 pip3 ins ...

  2. 【JavaScript 封装库】BETA 5.0 测试版发布!

    JavaScript 前端框架(封装库) BETA 5.0 已于10月10日正式发布,今天开始提供 BETA 5.0 的 API 参考文献.相较于之前 5 个版本的发布都是草草的提供源代码,并没有很多 ...

  3. DFS+BFS(POJ3083)

    题目链接:http://poj.org/problem?id=3083 解题报告:这个题目,搜最短路,没有什么问题.优先走左边,走右边,有很多说法,思路大概都相同,都是记录当前朝向,根据数学公式(i+ ...

  4. P1823 Patrik 音乐会的等待

    题目描述 N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟进行谈笑风生.队列中任意两个人A和B,如果他们是相邻或他们之间没有人比A或B高,那么他们是可以互相看 ...

  5. wordpress问题集锦

    1.内容不自动换行 找到对应的样式,添加如下代码,width根据具体情况修改. width:640px;white-space:normal;word-break:break-all;word-wra ...

  6. git中.gitignore 文件

    现在项目的根目录放了 .gitignore 文件,并且git远程仓库的项目根目录已经有了 logs文件夹. 由于每次本地运行项目,都会生成新的log文件,但是我并不想提交logs文件夹里面的内容,所以 ...

  7. Mybatis异常:java.lang.NumberFormatException: For input string: "S"

    MyBatis异常日志如下: Caused by: java.lang.NumberFormatException: For input string: "S" at sun.mi ...

  8. C#语言概述

    C#语言概述 一..NET Framework .NET Framework是Windows的一个不可或缺的组件,它包括公共语言运行库(CLR)和类库两部分. CLR是Microsoft的公共语言基础 ...

  9. 开发必看 | iOS开发常用设计模式!

    ios开发学习中,经常弄不清楚ios的开发模式,今天我们就来进行简单的总结和探讨~(一)代理模式 应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则: ...

  10. CSS3一些特殊属性

    (一)-webkit-tap-highlight-color         这个属性只用于iOS (iPhone和iPad).当你点击一个链接或者通过Javascript定义的可点击元素的时候,它就 ...