下面是我用於導出單個表數據的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
預先感謝您。
我按照我上面的代碼需要,如果更多鈔票。 @Krishna P S – zahed
@Zahed恐怕,您需要使用上述鏈接中提供的Excel Converter來獲取正確的Excel文件。將網格數據寫入響應也無助於這種情況。 –
我們可以使用存儲過程,我們直接調用上面的鏈接na。 @Krishna P S – zahed