下面介绍一种ASP.net中导出Excel的简单方法

先上代码:前台代码如下(这是自己项目里面写的一点代码先贴出来吧)

<div id="export" runat="server" style="width: 700px; margin-left: auto; margin-right: auto;">
<!--startprint-->

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" style="word-break: break-all;">

<tr>

<td align="center" colspan="7" valign="middle" style="font-size: 14px; font-family: 宋体; font-weight: bold;

height: 30px">

<span id="spanYEAR" runat="server"></span>年<span id="spanMONTH" runat="server"></span>月分公司月度需求生产任务汇总

</td>

</tr>

<tr>

<td align="right" valign="middle" colspan="7">

<table width="100%">

<tr>

<td colspan="2" align="left">

&nbsp;数量单位:支

</td>

<%-- <td width="10%" align="center" style="font-weight: bold;">

&nbsp;

</td>--%>

<td width="20%" align="center" style="font-weight: bold;">

</td>

<td width="25%" colspan="2" align="center">

</td>

<td width="40%" colspan="2" align="center">

</td>

</tr>

</table>

</td>

</tr>

</table>

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" style="font-size: 12px;

font-family: 宋体; word-break: break-all;">

<tr>

<td colspan="4" align="left" valign="top">

<div id="div" runat="server">

<table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" style="font-family: 宋体;

word-break: break-all;">

<tr>

<td width="8%" align="center" style="font-weight: bold; height: 25px">

序号

</td>

<td width="20%" align="center" style="font-weight: bold;">

硒鼓型号

</td>

<td width="12%" align="center" style="font-weight: bold;">

硒鼓类别

</td>

<td width="15%" align="center" style="font-weight: bold;">

第一批次

</td>

<td width="15%" align="center" style="font-weight: bold;">

第二批次

</td>

<td width="15%" align="center" style="font-weight: bold;">

第三批次

</td>

<td width="15%" align="center" style="font-weight: bold;">

第四批次

</td>

</tr>

<asp:Repeater ID="rptXQPC" runat="server">

<ItemTemplate>

<tr>

<td width="8%" align="center" height="25px">

<%#Eval("ROWID")%>

</td>

<td width="20%" align="center">

<%#Eval("xgxh")%>

</td>

<td width="12%" align="center">

<%#Eval("XGLB")%>

</td>

<td width="15%" align="center">

<%#Eval("First")%>

</td>

<td width="15%" align="center">

<%#Eval("Second")%>

</td>

<td width="15%" align="center">

<%#Eval("Third")%>

</td>

<td width="15%" align="center">

<%#Eval("Fourth")%>

</td>

</tr>

</ItemTemplate>

</asp:Repeater>

<tr>

<td colspan="3" width="40%" align="right" style="font-weight: bold; height: 25px">

批次合计:

</td>

<td width="15%" align="center" style="font-weight: bold;">

<div id="divFirst" runat="server"></div>

</td>

<td width="15%" align="center" style="font-weight: bold;">

<div id="divSecond" runat="server"></div>

</td>

<td width="15%" align="center" style="font-weight: bold;">

<div id="divThird" runat="server"></div>

</td>

<td width="15%" align="center" style="font-weight: bold;">

<div id="divFourth" runat="server"></div>

</td>

</tr>

</table>

</div>

</td>

</tr>

</table>

<!--endprint-->

</div>

后台代码如下 :

绑定Repeater数据这里接就不多做介绍了(用的Repeater嵌套)

下面贴出后台导出Excel的方法:

/// <summary>

/// 将数据导出到excel,与下面的函数同时使用才能正常工作

/// </summary>

/// <param name="ctl"></param>

public void ToExcel(System.Web.UI.Control ctl)

{

HttpContext.Current.Response.Clear();

HttpContext.Current.Response.Charset = "";

string filename = "Report" + System.DateTime.Now.ToString("_yyyyMMddHHmm");

HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" +

System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");

HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword

ctl.Page.EnableViewState = false;

System.IO.StringWriter tw = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

ctl.RenderControl(hw);

HttpContext.Current.Response.Write(tw.ToString());

HttpContext.Current.Response.End();

}

后台导出Excel时直接调用为 ToExcel(this.export); 这里的export是加了runat="server"的div名称,

这句代码的意思是调用ToExcel的方法导出export里面的页面数据(页面上的数据是怎么展示的,导出来以后的Excel数据会以同样的方式展示)

ASP.net中导出Excel的简单方法介绍的更多相关文章

  1. asp.net中导出excel数据的方法汇总

    1.由dataset生成 代码如下 复制代码 public void CreateExcel(DataSet ds,string typeid,string FileName)    {    Htt ...

  2. asp.net中导出Excel的方法

    一.asp.net中导出Excel的方法: 本文转载 在asp.net中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出 ...

  3. C# asp.net中导出Excel表时总出现"只能在执行 Render() 的过程中调用 RegisterForEventValidation

    C# asp.net中导出Excel表时总出现"只能在执行 Render() 的过程中调用 RegisterForEventValidation 后台添加以下方法:/// <summa ...

  4. Asp.net中导出Excel文档(Gridview)

    主要思路,通过GridView来导出文档. 新建一个Aspx页面,页面创建GridView控件,后台绑定好数据源.然后load中直接打印即可导出 前台的GridView <asp:GridVie ...

  5. 导出excel的简单方法

    excel的操作,最常用的就是导出和导入,废话不多说上代码. 本例使用NPOI实现的,不喜勿喷哈.... /// <summary> /// 导出Excel /// </summar ...

  6. spring mvc项目中导出excel表格简单实现

    查阅了一些资料,才整理出spring mvc 项目导出excel表格的实现,其实很是简单,小计一下,方便以后查阅,也希望帮助有需要的朋友. 1.导入所需要依赖(Jar包).我使用的是maven,所以坐 ...

  7. ASP.Net的导出Excel的快速方法,DataTable导出Excel(亲测,非原创)

    //使用方法 ExcelHelper.dataTableToCsv(dt,@"D:\1212.xls");System.Diagnostics.Process.Start(@&qu ...

  8. Asp.net 中高亮显示搜索关键字简单方法

    今天用到搜索时的高亮显示,百度了一下,如下面: 1.替换关键字,对字体变色.         public static string ReplaceRed(string strtitle, stri ...

  9. asp.net中导出Execl的方法

    一.asp.net中导出Execl的方法: 在 asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址 输出在浏览器上:一种是将文件直接将文件输出流写给 ...

随机推荐

  1. ActionScript 3 中的强制类型转换

    以前AS中是这样进行强制类型转换的:假设有一个类叫做Class1,我们声明了一个它的对象 c1,如果想要将它转换成Class2类型,只要这样写: Class2(c1); 在AS3中你依然可以这样写,但 ...

  2. sizeof求字节以及与strlen的区别

    例子一: /* *根据以下条件进行计算: *1. 结构体的大小等于结构体内最大成员大小的整数倍 *2. 结构体内的成员的首地址相对于结构体首地址的偏移量是其类型大小的整数倍,比如说double型成员相 ...

  3. linux自带抓包工具tcpdump使用说明

    tcpdump是个强大的网络分析工具,有很多细致的规则可以定义. 参考:http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html ...

  4. rpm命令使用说明

    RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种 ...

  5. wince 位图的使用

    操作位图的基本步骤: *创建位图句柄 *加载位图对象 *创建内存设备描述对象,将位图选入内存设备描述对象 *使用绘图函数进行图形绘制 *删除位图句柄 创建位图句柄并且加载位图对象: *位图句柄 HBI ...

  6. SuperMapDeskTop中去除面图层边框

    0.项目中有个功能需要在超图DeskTop中修改面图层的符号,将要素的边框去掉.搞来搞去这个功能并不像ArcGisDeskTop一样直接有无边框的符号,通过请教,通过修改面图层的线样式的RGB颜色管理 ...

  7. EWM Matrai B2B管理平台

    该应用是一款企业管理的app,可以通过“分享”.“工作分派”.“审批”.“业务”.“工作计划”.“日程”等功能得到有效的管控.该项目主要分为5大模块,分别是近期动态,任务,日程,我,在线聊天.   

  8. ps扩大、缩小选区

    用"套索工具""魔棒工具"或者等工具将选区选出来,创建出一个需要处理的选区.   点击ps菜单栏中的"选择",在下拉菜单中选择"修 ...

  9. Java Concurrency - 取消线程执行器中的线程

    When you work with an executor, you don't have to manage threads. You only implement the Runnable or ...

  10. IntelliJ IDEA 13.x 下使用Hibernate + Spring MVC + JBoss 7.1.1

    从2004年开始做.NET到现在.直到最近要做一些JAVA的项目,如果说100个人写一篇关于.NET的文章,估计这10个人写的内容都是一样.但是如果说10个人写Java的文章,那真的是10个人10种写 ...