如何将多个数据表导出到单个Excel文件中的多个工作表

时间:2016-01-21 05:31:17

标签: c# asp.net

下面是导出单表数据的C#代码,但我需要将3个表数据导出到一个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

先谢谢你。

2 个答案:

答案 0 :(得分:1)

您可以查看此Codeproject链接,该链接描述了将数据集导出到多个Excel表格

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

SO问题

中提供了上述链接

请查看有关如何使用C#

创建Excel文件的SO问题

答案 1 :(得分:0)

使用数据集存储两个数据表,

<?php
$Server="YOUR HOSTINGERS DATABASE SERVER NAME";
$unameDB ="YOUR HOSTINGERS DATABASE USER NAME";
$passDB  ="YOUR HOSTINGERS DATABASE PASSWORD";
$dbName="u749031689_enrol";
$sqlCon = mysql_connect($Server, $unameDB, $passDB) or die (mysql_error(). "Not connected");
mysql_select_db($sqlCon, $dbName);
?>

并使用

 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;
        }