将网格视图数据导出到Excel

时间:2012-10-17 09:41:08

标签: c#-4.0 export-to-excel

将Visual Studio 2010与Server Management Studio 2008一起使用我已经创建了一个功能,教师可以通过该功能查看所有年份所有学生的成绩:

通过下拉选择,它在网格视图中显示数据,并带有一个按钮,供教师在Excel工作表中查看选择。对于某些选择,有很多数据,因此需要时间。

现在我的客户不希望在页面中显示网格视图,而是根据下拉列表中的选择直接导出到Excel。

任何人都可以帮我这样做吗?它会让我的页面加载速度更快吗?

1 个答案:

答案 0 :(得分:1)

首先在ssms中编写存储过程以检索数据。 它应该像

   CREATE PROCEDURE [Retrieveresult] (@selectedfield nvarchar(50))
   AS  
   BEGIN  
   select * from students where condition=@selectfield
   END  

然后在visualstudio中

   oConn = new SqlConnection();
   oConn.ConnectionString = "your connection string"
   SqlCommand command = new SqlCommand("Retrieveresult");
    command.CommandType = System.Data.CommandType.StoredProcedure;
     oConn.Open();
   command.Parameters.Add(new SqlParameter("@selectfield", System.Data.SqlDbType.nvarchar(50))); 
    command.Parameters["@selectfield"].Value="selected value from gridview"
   IDataReader oDr;
    oDr=command.executereader();
  while(oDr.read())
     {
       Get the corresponding values in the objects 
      }