将GridView内数据导入到Excel

以下是导出数据的后台类:

using  System;
using  System.Data;
using  System.Data.SqlClient;
using  System.IO;

namespace  WebUtility
{
    
/// <summary>
    
/// 将Table的数据导出Excel;
    
/// 标题为Table的标题;
    
/// 使用文件操作来完成此功能:
    
/// 缺点:不能操作Excel;标题可能是英文,没有边框,没有格式;
    
/// </summary>

    public class OutExcel:DBUtility.DBHelper
    
{
        
/// <summary>
        
/// 导出EXCEL
        
/// </summary>
        
/// <param name="dt"></param>
        
/// <param name="FileName"></param>
        
/// <returns></returns>

        public int Export(DataTable dt,string FileName)
        
{
            
try
            
{
                
//文件名;创建;可写
                FileStream fs=new FileStream(FileName,FileMode.Create,FileAccess.Write); 
                StreamWriter sw
=new StreamWriter(fs,System.Text.Encoding.GetEncoding("gb2312"));
                
//标题
                string Title = "";
                
//内容
                string Line = "";
                
for (int i=0;i<dt.Columns.Count;i++)
                
{
                    Title 
= Title + dt.Columns[i].ToString() + " ";
                }

                
//去掉最后的回车符
                Title = Title.Substring(0,Title.Length -1);
                
//将标题写入流
                sw.WriteLine(Title); 
                
foreach(DataRow dr in dt.Rows) 
                

                    
for (int i=0;i<dt.Columns.Count;i++)
                    
{
                        Line 
= Line+dr[i] + " ";
                    }

                    Line 
= Line.Substring(0,Line.Length -1);
                    
//将数据写入文件流
                    sw.WriteLine(Line); 
                    Line 
= "";
                }
 
                sw.Close();
                
return 0;
            }

            
catch
            
{
                
return 1;
            }

        }

        
//===============================================================================================
    }

}

 在页面上进行调用:
protected   void  btnOut_Click( object  sender, EventArgs e)
    
{
        
public WebUtility.OutExcel myOut = new WebUtility.OutExcel();
        
//导出日志到Excel
        System.Data.DataTable dt = user.DataSetLoginLog(WebUtility.SysUser.LoginLogType.all).Tables[0];
        
string DownloadPath = Server.MapPath("."+ "//" + string.Format("登录日志备份{0}.XLS",System.DateTime.Now.ToShortDateString());
        
//导出
        this.myOut.Export(dt, DownloadPath);
        
//以下代码将 pdf 文件写入客户端浏览器。
        Response.ClearContent();
        Response.ClearHeaders();
        
//作为附件下载
        Response.AddHeader("Content-Disposition""attachment; filename=" + Server.UrlEncode(DownloadPath));
        Response.ContentType 
= "application/ms-excel";
        Response.WriteFile(DownloadPath);
        Response.Flush();
        Response.Close();
        
//从磁盘删除导出的文件
        System.IO.File.Delete(DownloadPath);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值