.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. ...
随机推荐
- 神奇的暴力数据结构——ODT
前言 \(ODT\),即珂朵莉树,又称老司机树(\(Old\ Driver\ Tree\)). 它是一个十分暴力的数据结构,可以用于各种乱搞,也非常的实用. 当然,这全要基于一个基本条件:数据随机. ...
- P1089 津津的储蓄计划
题目描述 津津的零花钱一直都是自己管理.每个月的月初妈妈给津津300300元钱,津津会预算这个月的花销,并且总能做到实际花销和预算的相同. 为了让津津学习如何储蓄,妈妈提出,津津可以随时把整百的钱存在 ...
- 2017.10.26 JavaWeb----第五章 JavaBean技术
JavaWeb----第五章 JavaBean技术 (1)JavaBean技术 JavaBean技术是javaweb程序的重要组成部分,是一个可重复使用的软件组件,是用Java语言编写的.遵循一定的标 ...
- 2017.10.21 Java中的数据源与连接池技术
1.数据源技术就是预先建立好一定的数量的数据库连接,并将这些连接保存在连接池中,有连接池负责对这些数据库连接管理,当访问数据库时,只需要从连接池中取出有空闲状态的数据库连接:当程序访问数据库结束时,释 ...
- P1266 速度限制
P1266 速度限制 第一次接触这种分层spfa 类似于dp 个人理解 #include<cstdio> #include<iostream> #include<algo ...
- 【洛谷P1063】NOIP2006能量项链
能量项链 https://www.luogu.org/problemnew/show/P1063 好像比合并石子更水.. 区间动规,f[l][r]表示合并区间l~r的最大能量 按区间长度dp 枚举中间 ...
- bootstarp v3 学习简记
1.快速设置浮动通过这两个class让页面元素左右浮动. !important被用来避免某些问题. <div class="pull-left">...</div ...
- C#接口定义
C#接口定义 C#不支持多重继承,但是客观世界出现多重继承的情况又比较多.为了避免传统的多重继承给程序带来的复杂性等问题,C# 提出了接口的概念.通过接口可以实现多重继承的功能. 继承该接口的类或结 ...
- l1,l2norm
http://www.chioka.in/differences-between-l1-and-l2-as-loss-function-and-regularization/ 这里分别对l1 loss ...
- 分布式系统session一致性问题
一.引言 1.什么是session Session 是服务器用来保存用户操作的一系列会话信息,由Web容器进行管理.最常见的,会把用户的登录信息.用户信息存储在 session 中,以保持登录状态. ...