2017-01-30 4 views
-1

現在、2つのワークシートにエクスポートされる2つのデータテーブルがあります。これら2つのデータテーブルを1つのワークシートにエクスポートするにはどうすればよいですか?誰でも?私はこのパズルを解決するのに苦労しました。2つのグリッドビューをExcelの単一のワークシートにエクスポート

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(); 
       } 
      } 
     } 
+0

DataTableを使用してマージする方法を理解していますか?これをオンラインで行う方法の例がたくさんあります。 – MethodMan

+0

残念ながらそうではありません。自分のシナリオでどのように実行されたかを私に見せていただければ幸いです。 –

+0

私はちょうどあなたが何をすべきか教えてくれました。データテーブルをどのようにマージするかについて簡単なgoogle検索を行います。問題がある場合は、GoogleにUNION文を作成する方法をGoogleに伝えたくありません。またストアドプロシージャ – MethodMan

答えて

1

マージ方法は、第2のテーブルから値を取得し、最初のテーブルでそれらをマージし、第1は、現在の両方の値を保持します。

あなたは、元のテーブルの両方を保持したい場合は、マージ、その後、オリジナルの最初のコピーができます:

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); 
} 
関連する問題