.Net导出Word和Excel
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- public class ExportOffice
- {
- //导出页面或web控件方法#region 导出页面或web控件方法
- /**/
- /// <summary>
- /// 将Web控件或页面信息导出(不带文件名参数)
- /// </summary>
- /// <param name="source">控件实例</param>
- /// <param name="DocumentType">导出类型:Excel或Word</param>
- public void ExportControl(System.Web.UI.Control source, string DocumentType)
- {
- //设置Http的头信息,编码格式
- if (DocumentType == "Excel")
- {
- //Excel
- HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("下载文件.xls", System.Text.Encoding.UTF8));
- HttpContext.Current.Response.ContentType = "application/ms-excel";
- }
- else if (DocumentType == "Word")
- {
- //Word
- HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("下载文件.doc", System.Text.Encoding.UTF8));
- HttpContext.Current.Response.ContentType = "application/ms-word";
- }
- HttpContext.Current.Response.Charset = "UTF-8";
- HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
- //关闭控件的视图状态
- source.Page.EnableViewState = false;
- //初始化HtmlWriter
- System.IO.StringWriter writer = new System.IO.StringWriter();
- System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
- source.RenderControl(htmlWriter);
- //输出
- HttpContext.Current.Response.Write(writer.ToString());
- HttpContext.Current.Response.End();
- }
- /**/
- /// <summary>
- /// 将Web控件或页面信息导出(带文件名参数)
- /// </summary>
- /// <param name="source">控件实例</param>
- /// <param name="DocumentType">导出类型:Excel或Word</param>
- /// <param name="filename">保存文件名</param>
- public void ExportControl(System.Web.UI.Control source, string DocumentType, string filename)
- {
- //设置Http的头信息,编码格式
- if (DocumentType == "Excel")
- {
- //Excel
- HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".xls", System.Text.Encoding.UTF8));
- HttpContext.Current.Response.ContentType = "application/ms-excel";
- }
- else if (DocumentType == "Word")
- {
- //Word
- HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".doc", System.Text.Encoding.UTF8));
- HttpContext.Current.Response.ContentType = "application/ms-word";
- }
- HttpContext.Current.Response.Charset = "UTF-8";
- HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
- //关闭控件的视图状态
- source.Page.EnableViewState = false;
- //初始化HtmlWriter
- System.IO.StringWriter writer = new System.IO.StringWriter();
- System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
- source.RenderControl(htmlWriter);
- //输出
- HttpContext.Current.Response.Write(writer.ToString());
- HttpContext.Current.Response.End();
- }
- #region 调用说明
- //方法ExportControl(System.Web.UI.Control source, string DocumentType,string filename)中
- //第一个参数source表示导出的页面或控件名,当为datagrid或dataList控件时,在导出Excel/word文件时,必须把控件的分页、排序等属性去除并重新绑定,
- //第二个参数DocumentType表示导出的文件类型word或excel
- //第三个参数filename表示需要导出的文件所取的文件名
- //调用方法:
- //ExportData export=new ExportData();
- //export.ExportControl(this, "Word","testfilename");//当为this时表示当前页面
- //这是将整个页面导出为Word,并命名为testfilename
- #endregion
- }
- <!DOCTYPE html>
- <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'>
- <head runat="server">
- <!--[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]-->
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title></title>
- <style>
- table{ border: solid 1px black;border-collapse: collapse;}
- td{width: 200px;border: solid 1px black;word-wrap: break-word; word-break: break-all;}
- body,tr,td{font-size:14px;}
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <table style="margin:0px auto;" border="1" >
- <caption style="font:20px/30px Arail;"><asp:Literal runat="server" ID="lbl_Caption"></asp:Literal></caption>
- <tr>
- <td><asp:Literal runat="server" ID="lbl_username_title"></asp:Literal></td>
- <td><asp:Literal runat="server" ID="lbl_username_value"></asp:Literal></td>
- <td><asp:Literal runat="server" ID="lbl_userCode_title"></asp:Literal></td>
- <td><asp:Literal runat="server" ID="lbl_userCode_value"></asp:Literal></td>
- </tr>
- <tr>
- <td><asp:Literal runat="server" ID="lbl_sex_title"></asp:Literal></td>
- <td><asp:Literal runat="server" ID="lbl_sex_value"></asp:Literal></td>
- <td><asp:Literal runat="server" ID="lbl_birthday_title"></asp:Literal></td>
- <td><asp:Literal runat="server" ID="lbl_birthday_value"></asp:Literal></td>
- </tr>
- <tr>
- <td><asp:Literal runat="server" ID="lbl_nationality_title"></asp:Literal></td>
- <td><asp:Literal runat="server" ID="lbl_nationality_value"></asp:Literal></td>
- <td><asp:Literal runat="server" ID="lbl_company_title"></asp:Literal></td>
- <td><asp:Literal runat="server" ID="lbl_company_value"></asp:Literal></td>
- </tr>
- <tr>
- <td><asp:Literal runat="server" ID="lbl_hospitalName_title"></asp:Literal></td>
- <td><asp:Literal runat="server" ID="lbl_hospitalName_value"></asp:Literal></td>
- <td><asp:Literal runat="server" ID="lbl_tjDate_title"></asp:Literal></td>
- <td><asp:Literal runat="server" ID="lbl_tjDate_value"></asp:Literal></td>
- </tr>
- <asp:Literal runat="server" ID="lbl_data"> </asp:Literal>
- </table>
- </form>
- </body>
- </html>
.Net导出Word和Excel的更多相关文章
- 通过swagger json一键解析为html页面、导出word和excel的解析算法分享
写在前面: 完全通过Spring Boot工程 Java代码,将swagger json 一键解析为html页面.导出word和execel的解析算法,不需要任何网上那些类似于“SwaggerMark ...
- 在Java中导出word、excel格式文件时JSP页面头的设置
我们在JSP中往往会把一些表格里的东西需要导出到本地,一般都是导成word.excel格式的文件.这只需要在JSP页面头设置及在<head></head>标签中添加下面的代码: ...
- 【C#点滴记录】ASP.NET 使用C# 导出Word 和Excel
原文摘自 慧优米网,链接地址:http://huiyoumi.wang/upload/forum.php?mod=viewthread&tid=797&extra= 好了正文来了 今天 ...
- C#导出Word或Excel文件总显示Html标记
原因:Word或Excel文件包含的GridView没有查询到数据.
- poi导出word、excel
在实际的项目开发中,经常会有一些涉及到导入导出的文档的功能.apache开源项目之一poi对此有很好的支持,对之前的使用做一些简要的总结. 1,导入jar 为了保证对格式的兼容性,在项目的pom.xm ...
- JS019. 原生JS使用new Blob()实现带格式导出Word、Excel(提供无编程基础将页面上表格导出到本地的方法)
导出效果 代码实现 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- Java使用freemarker导出word和excel
www.linxiaosheng.com/post/2013-12-05/40060346181 https://github.com/upyun/java-sdk
- .net core mvc部署到IIS导出Word 提示80070005拒绝访问
项目中相信大家经常会遇到导出Word.Excel等需求,在实际开发环境中,一般不会出现什么问题,但当发布到IIS上后可能会遇到各种各样的问题,一般都是权限的问题.前几天把公司项目发布后,出现Word导 ...
- 导出Excel And 导出word
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx. ...
随机推荐
- selenium安装及官方文档
selenium-python官方文档: https://selenium-python.readthedocs.io/ python3.5已安装的情况下,安装示意图如下 命令行输入 pip3 ins ...
- 【JavaScript 封装库】BETA 5.0 测试版发布!
JavaScript 前端框架(封装库) BETA 5.0 已于10月10日正式发布,今天开始提供 BETA 5.0 的 API 参考文献.相较于之前 5 个版本的发布都是草草的提供源代码,并没有很多 ...
- DFS+BFS(POJ3083)
题目链接:http://poj.org/problem?id=3083 解题报告:这个题目,搜最短路,没有什么问题.优先走左边,走右边,有很多说法,思路大概都相同,都是记录当前朝向,根据数学公式(i+ ...
- P1823 Patrik 音乐会的等待
题目描述 N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟进行谈笑风生.队列中任意两个人A和B,如果他们是相邻或他们之间没有人比A或B高,那么他们是可以互相看 ...
- wordpress问题集锦
1.内容不自动换行 找到对应的样式,添加如下代码,width根据具体情况修改. width:640px;white-space:normal;word-break:break-all;word-wra ...
- git中.gitignore 文件
现在项目的根目录放了 .gitignore 文件,并且git远程仓库的项目根目录已经有了 logs文件夹. 由于每次本地运行项目,都会生成新的log文件,但是我并不想提交logs文件夹里面的内容,所以 ...
- Mybatis异常:java.lang.NumberFormatException: For input string: "S"
MyBatis异常日志如下: Caused by: java.lang.NumberFormatException: For input string: "S" at sun.mi ...
- C#语言概述
C#语言概述 一..NET Framework .NET Framework是Windows的一个不可或缺的组件,它包括公共语言运行库(CLR)和类库两部分. CLR是Microsoft的公共语言基础 ...
- 开发必看 | iOS开发常用设计模式!
ios开发学习中,经常弄不清楚ios的开发模式,今天我们就来进行简单的总结和探讨~(一)代理模式 应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则: ...
- CSS3一些特殊属性
(一)-webkit-tap-highlight-color 这个属性只用于iOS (iPhone和iPad).当你点击一个链接或者通过Javascript定义的可点击元素的时候,它就 ...