.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. ...
随机推荐
- Locust的官网及安装
Locust官网: https://docs.locust.io/en/latest/installation.html for Python 3: $ python3 -m pip install ...
- HTML入门1—HTML基础学习
html文档结构 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- 前端高质量知识(一)-JS内存空间详细图解
变量对象与堆内存 var a = 20; var b = 'abc'; var c = true; var d = { m: 20 } 因为JavaScript具有自动垃圾回收机制,所 ...
- python 3+djanjo 2.0.7简单学习(四)--Django视图
1.概念 Django 中的视图的概念是「一类具有相同功能和模板的网页的集合」.比如,在一个博客应用中,你可能会创建如下几个视图: 博客首页——展示最近的几项内容. 内容“详情”页——详细展示某项内容 ...
- Spring Boot的Maven插件Spring Boot Maven plugin详解
Spring Boot的Maven插件(Spring Boot Maven plugin)能够以Maven的方式为应用提供Spring Boot的支持,即为Spring Boot应用提供了执行Mave ...
- 用MXnet实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别
用MXnet实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别 http://phunter.farbox.com/post/mxnet-tutorial1 用MXnet实战深度学 ...
- nuget打包
[1.创建.nuspec文件] 在.csproj目录下运行命令 nuget spec [2.生成包nupkg] 在.csproj目录下运行命令 nuget pack xxxx.csproj [推送命令 ...
- redux详解
redux介绍 学习文档:英文文档,中文文档,Github redux是什么 redux是一个独立专门用于做状态管理的JS库(不是react插件库),它可以用在react, angular, vue等 ...
- python :编写装饰器
简单装饰器 def log_time(func): # 此函数的作用时接受被修饰的函数的引用test,然后被内部函数使用 def make_decorater(): print('现在开始装饰') f ...
- LeetCode(Two Sum)
一.题目要求 Given an array of integers, return indices of the two numbers such that they add up to a spec ...