易截截图软件、单文件、免安装、纯绿色、仅160KB

ASP.NET(C#) DataSet数据导出到Excel

今天给客户做了个功能需要把网格数据(Gridview)导出成Execl,所以我去网上找了个代码 private void Export(string FileType, string FileName)
...{
try
...{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
grdSearch.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
catch (Exception err)
...{
DOMUtility.PopAlertWindow("导出Excel出错!错误原因:" + err.Message);
//return false;
}
}
但是这种方法的话只是把当前Gridview页导出到Excel,而客户需要把当前所以分页的数据导出到Excel,并且在客户端打开,所以想了想有几种方案:
1)将execl文件导出到服务器上,再下载。这样的话服务器中有多余的文件了。不通过
2)将DataSet中的数据导出Execl
private void DataTable2Excel(System.Data.DataTable dtData)
...{
System.Web.UI.WebControls.DataGrid dgExport = null;
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;

if (dtData != null)
...{
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
curContext.Response.Charset = "";

strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

dgExport = new System.Web.UI.WebControls.DataGrid();
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind();

dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
}
先把datatable转换成DataGrid数据,然后通过DataGrid对象RenderControl的Response把数据返回给客户端,


相关文档:

C#: 提取网页中的javascript代码

public static void Main()
        {
            WebRequest req = WebRequest.Create("http://blog.csdn.net/xiaofengsheng");
            try
  & ......

Java调用C#的WebService实例三(实际应用)

/*
   本段代码在公司项目中实际远程调用第三方公司提供的C#开发WebService的示例
*/
/**
  * 登录游戏
  *
  * @param paramPN
  * @param paramTerraceID
  * @param paramSvrID
  */
 private String loginGame(String paramPN, HttpServletRequest req){
&n ......

Asp.net页面传值方法及实用技巧

一. 使用QueryString变量
    QueryString是一种非常简单也是使用比较多的一种传值方式,但是它将传递的值显示在浏览器的地址栏中,如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法。
         Response.Redirect( "target.aspx?param1=hello& ......

学习asp.net C#

 1、DateTime   数字型  
System.DateTime   currentTime=new   System.DateTime();  
  1.1   取当前年月日时分秒  
currentTime=System.DateTime.Now;  
  1.2   取当前年  
int   年=currentTime.Year;  
  1.3   取当前月 &n ......

ASP.NET中GridView分页

方法一:
新建一个页面然后在页面拖一个GridView控件在属性框中设置GridView控件的AllowPaing属性为True 然后再设置GridView的PageSize属性,源文件如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames ="classId" CellPadding="5" OnRowDeleting="GridView1_RowDeleting" All ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号