2016-01-21 70 views
2

下面是我用於導出單個表數據的C#代碼,但我需要將3個表數據導出到一個Excel文件中。我需要將多個數據表導出到單個Excel文件中的多個工作表。如何將多個數據表導出到單個Excel文件中的多個工作表中

protected void lnkbtn_Submit_Click(object sender, EventArgs e) 
    { 
    DateTime Fromdate = DateTime.ParseExact(txt_FromDate.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture); 
    DateTime Todate = DateTime.ParseExact(txt_ToDate.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture); 

    bo.Dateused = Fromdate; 
    bo.Dateused2 = Todate; 

    DataTable dt = bl.Get_Registrationdetailsbydate(bo); 
    gv_Regdetails.DataSource = dt; 
    gv_Regdetails.DataBind(); 
    Session["Fromdate"] = txt_FromDate.Text; 
    Session["Todate"] = txt_ToDate.Text; 
    if (gv_Regdetails.Rows.Count > 0) 
    { 
     Session["registrationtable_date"] = dt; 
     btnExport.Visible = true; 
    } 

    else 
    { 
     Session["registrationtable_date"] = null; 
     btnExport.Visible = false; 
    } 
} 
protected void Button1_Click(object sender, ImageClickEventArgs e) 
{ 
    DataGrid dg = new DataGrid(); 
    if (Session["registrationtable_date"] != null) 
    { 
     dg.DataSource = (DataTable)Session["registrationtable_date"]; 
     dg.DataBind(); 
     System.Web.HttpContext.Current.Response.Clear(); 
     System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=TotalRegistrationDetails.xls"); 
     System.Web.HttpContext.Current.Response.Charset = ""; 
     System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     System.Web.HttpContext.Current.Response.ContentType = "application/vnd.xls"; 
     System.IO.StringWriter stringwrite = new System.IO.StringWriter(); 
     System.Web.UI.HtmlTextWriter htmlwrite = new HtmlTextWriter(stringwrite); 
     dg.RenderControl(htmlwrite); 
     System.Web.HttpContext.Current.Response.Write(stringwrite.ToString()); 
     System.Web.HttpContext.Current.Response.End(); 
     Session.Remove("registrationtable_date"); 

     } 
     } 
    } 

我的存儲過程腳本:

ALTER PROCEDURE [dbo].[Get_Registrationdetailsbydate] 
(
    @Fromdate datetime, 
    @Todate datetime 
) 
AS 
BEGIN 

select Name,Email,Mobile,SubmissionDate from Registration WHERE (CONVERT(varchar(50), SubmissionDate, 101) BETWEEN @Fromdate AND @Todate) 
END 

預先感謝您。

回答

1

你可以看看這個CodeProject上的鏈接,它描述了出口數據集到多個Excel表

http://www.codeproject.com/Articles/31516/Export-DataSet-to-Multiple-Excel-Sheets

上面的鏈接在此SO問題是提供

請找到SO問題關於如何使用C#創建Excel文件

+0

我按照我上面的代碼需要,如果更多鈔票。 @Krishna P S – zahed

+0

@Zahed恐怕,您需要使用上述鏈接中提供的Excel Converter來獲取正確的Excel文件。將網格數據寫入響應也無助於這種情況。 –

+0

我們可以使用存儲過程,我們直接調用上面的鏈接na。 @Krishna P S – zahed

0

使用數據集來存儲兩個數據表,

public DataSet getDataSetExportToExcel() 
     { 
      DataSet ds = new DataSet(); 
      DataTable dtEmp = new DataTable("Employee"); 
      dtEmp = getAllEmployeesList(); 

      DataTable dtEmpOrder = new DataTable("Order List"); 
      dtEmpOrder = getAllEmployeesOrderList(); 
      ds.Tables.Add(dtEmp); 
      ds.Tables.Add(dtEmpOrder); 
      return ds; 
     } 

,並使用

dg.DataSource = (DataSet)Session["registrationtable_date"]; 
     dg.DataBind(); 
+0

我正在使用存儲過程可以根據上述方法更新答案,我另一種存儲過程的方法是bl.Get_educationdetails。 @Kutty Rajesh Valangai – zahed

相關問題