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. 94. Binary Tree Inorder Traversal(inorder ) ***(to be continue)easy

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  2. 154. Find Minimum in Rotated Sorted Array II(Binary search)

    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/ -- leetcode follo ...

  3. 广搜,深搜,单源最短路径,POJ(1130),ZOJ(1085)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=85 http://poj.org/problem?id=1130 这 ...

  4. NYOJ(680),摘枇杷,(暴力,或者二分搜索)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=680 很巧妙的一个题目,就是看你的逆向思维,result 一定是max(a[i])~sum ...

  5. 计算多项式Poj(1996)

    题目链接:http://poj.org/problem?id=1996 思路: 刚开始打了个二维表,调了一个小时,爆内存了. #include <stdio.h> #include < ...

  6. 2017.9.15 HTML学习总结---表格table

    2.7 表格<table>的属性: 属性  用途 width         表格宽度 height        表格高度 align         表格水平对齐 border     ...

  7. fluent Python

    1.1 Python风格的纸牌 Python collections模块中的内置模块:namedtuple https://www.liaoxuefeng.com/wiki/0013747381250 ...

  8. 自己编写shave函数

    import numpy def shave(I,border=None): I = I[border[0]:I.shape[0]-border[0],border[1]:I.shape[1]-bor ...

  9. git 学习笔记 window操作系统

    一.准备工作 1.设置好操作者和邮箱 $ git config --global user.name "Your Name" $ git config --global user. ...

  10. 12、SpringBoot------activeMq的简单使用

    开发工具:STS 前言: What is ActiveMq? ActiveMq:实现了Jms规范的一款Java 消息中间件. 消息中间件: 处理消息的一个消息机制,负责接收消息与转发. 用途: (1) ...