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的更多相关文章

  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. MySQL数据库实验六:存储过程建立与调用

    实验六  存储过程建立与调用 一.实验目的 理解存储过程的概念.建立和调用方法. 二.实验环境 三.实验示例 1.定义一个函数,按性别计算所有学生的平均年龄. CREATE FUNCTION aver ...

  2. Cmake 01

    1. sdsf(single direction single file) 1.1  The directory tree /* ./template | +--- build | +---main. ...

  3. IOS 拦截所有push进来的子控制器

    /** * 能拦截所有push进来的子控制器 */ - (void)pushViewController:(UIViewController *)viewController animated:(BO ...

  4. EF分组后把查询的字段具体映射到指定类里面的写法

    //先做基本查询 var querySql = from l in _logClinicDataOperationRepository.Table select new LogClinicDataOp ...

  5. POJ-1990 MooFest---两个树状数组

    题目链接: https://vjudge.net/problem/POJ-1990 题目大意: 一群牛参加完牛的节日后都有了不同程度的耳聋,第i头牛听见别人的讲话,别人的音量必须大于v[i],当两头牛 ...

  6. firewalld 使用简介

    学习apache安装的时候需要打开80端口,由于centos 7版本以后默认使用firewalld后,网上关于iptables的设置方法已经不管用了,想着反正iptable也不会用,索性直接搬官方文档 ...

  7. python web应用--web框架(三)

    了解了WSGI框架,我们发现:其实一个Web App,就是写一个WSGI的处理函数,针对每个HTTP请求进行响应. 但是如何处理HTTP请求不是问题,问题是如何处理100个不同的URL. 每一个URL ...

  8. SpringBoot集成Quartz(解决@Autowired空指针Null问题即依赖注入的属性为null)

    使用spring-boot作为基础框架,其理念为零配置文件,所有的配置都是基于注解和暴露bean的方式. Quartz的4个核心概念: 1.Job表示一个工作,要执行的具体内容.此接口中只有一个方法v ...

  9. mask r-cnn

    mask R-cnn, kaiming he的新作.可以同时完成object detection和segmentation,还可以做pose estimation,简直就是功能多多啊.在coco上测试 ...

  10. 在ubuntu中docker的简单使用(一)

    >>docker version 当运行docker version 命令出现Cannot connect to Docker daemon. Is the docker daemon r ...