将2个网格视图导出到Excel中的单个工作表

时间:2017-01-30 22:19:08

标签: c# excel datatable export

目前,我有2个数据表正在导出到2个单独的工作表中。如何将这两个数据表导出到一个工作表中?任何人?我一直很难解决这个难题。

private DataTable SkyvisionMachineData()
        {
            string constr = ConfigurationManager.ConnectionStrings["Media_OperationsConnectionString9"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT CorporateName, Region FROM MACHINE"))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter())
                    {
                        DataTable dt = new DataTable();
                        cmd.CommandType = CommandType.Text;
                        cmd.Connection = con;
                        da.SelectCommand = cmd;
                        da.Fill(dt);
                        dt.TableName = "Region";
                        return dt;
                    }
                }
            }
        }
        private DataTable SkyvisionZoneData()
        {
            string constr = ConfigurationManager.ConnectionStrings["Media_OperationsConnectionString9"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))

            {
                using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT CorporateName, Spare, Region FROM  dbo.Machine WHERE CorporateName LIKE @CorporateName OR Region like @Region"))

                {
                    using (SqlDataAdapter da = new SqlDataAdapter())
                    {
                        Label1.Text = TextBox1.Text;
                        DataTable dt = new DataTable();
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.Add("@CorporateName", SqlDbType.VarChar, 50).Value = Label1.Text;
                        cmd.Parameters.Add("@Region", SqlDbType.VarChar, 20).Value = Label1.Text;
                        cmd.Connection = con;
                        da.SelectCommand = cmd;
                        da.Fill(dt);
                        dt.TableName = "Machine";
                        return dt;
                    }
                }
            }
        }
        public DataSet getDataSetExportToExcel()
        {
            DataSet ds = new DataSet();

            DataTable Machine = new DataTable("Machine Data");
            Machine = SkyvisionMachineData();

            DataTable Region = new DataTable("Skyvision Zone Data");
            Region = SkyvisionZoneData();

            ds.Tables.Add(Machine);
            ds.Tables.Add(Region);
            return ds;

        }
        protected void ExportToExcel_Click(object sender, EventArgs e)
        {
            DataSet ds = getDataSetExportToExcel();
            using (XLWorkbook wb = new XLWorkbook())
            {
                wb.Worksheets.Add(ds);
                wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                wb.Style.Font.Bold = true;
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "";
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;filename= DBSearchReport.xlsx

    ");
                using (MemoryStream MyMemoryStream = new MemoryStream())
                {
                    wb.SaveAs(MyMemoryStream);
                    MyMemoryStream.WriteTo(Response.OutputStream);
                    Response.Flush();
                    Response.End();
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

Merge方法获取第二个表中的值并将它们与第一个表合并,因此第一个表现在将保存两个值。

如果要保留两个原始表,可以先复制原始表,然后合并:

dtAll = new DataTable();
...
dtAll.Merge(dtOne);
dtAll.Merge(dtTwo);
dtAll.Merge(dtThree);

此技术在您想要迭代合并数据表的循环中很有用:

DataTable dtAllCountries = new DataTable();

foreach(String strCountry in listCountries)
{
    DataTable dtCountry = getData(strCountry); //Some function that returns a data table
    dtAllCountries.Merge(dtCountry);
}