2016-06-21 18 views
0

グリッドビューを.xlsに変換したいのですが、エラーがスローされ、「OK」をクリックすると「Excelにエクスポートできませんでした。元のエラー: 'System.Data.DataSet'」 オブジェクトタイプ 'System.Data.DataTable'私のコードはここにあります。Epplusエクスポートエラー: 'System.Data.DataSet'オブジェクトの種類 'System.Data.DataTable'が親切ではありません。

マイサーチボタン

 groupBox2.Visible = true; 
     SqlConnection baglanti = new SqlConnection("Data Source=.; Initial Catalog=database; Trusted_Connection=yes; MultipleActiveResultSets=True"); 
     SqlDataAdapter da = new SqlDataAdapter(); 
     SqlCommand cmd = new SqlCommand(); 
     DataSet ds = new DataSet(); 
     baglanti.Open(); 
     cmd.CommandText = "SELECT * FROM hostes_tablo WHERE ayak_no=" + comboBox7.Text + ""; 

     da.SelectCommand = cmd; 
     cmd.Connection = baglanti; 
     da.Fill(ds, "hostes_tablo"); 

     dataGridView1.DataSource = ds; 
     dataGridView1.DataMember = "hostes_tablo"; 
     baglanti.Close(); 

私のエクスポート]ボタン私はその私を与えるエラーエクスポートボタンをクリックしていたとき、私はその後、検索

var saveFileDialog1 = new SaveFileDialog(); 
     saveFileDialog1.Filter = "Excel File (*.xlsx)|*.xlsx"; 
     saveFileDialog1.FilterIndex = 1; 
     if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      try 
      { 
       FileInfo file = new FileInfo(saveFileDialog1.FileName); 
       if (file.Exists) 
       { 
        file.Delete(); 
       } 

       using (ExcelPackage pck = new ExcelPackage(file)) 
       { 
        ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Sheet1"); 
        ws.Cells["A1"].LoadFromDataTable(((DataTable)dataGridView1.DataSource), true); 
        ws.Cells.AutoFitColumns(); 

        using (ExcelRange rng = ws.Cells[1, 1, 1, dataGridView1.Columns.Count]) 
        { 
         rng.Style.Font.Bold = true; 
         rng.Style.Fill.PatternType = ExcelFillStyle.Solid; 
         rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(79, 129, 189)); 
         rng.Style.Font.Color.SetColor(System.Drawing.Color.White); 
        } 

        pck.Save(); 
        pck.Dispose(); 

       } 

       MessageBox.Show(string.Format("Excel file \"{0}\" generated successfully.", file.Name)); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Failed to export to Excel. Original error: " + ex.Message); 
      } 
     } 

+0

へのDataSetからキャストされました。 – Venky

+0

は行しません。プログラムが実行されている間私に与えてください。 [クリックしてエラーを表示](http://i.hizliresim.com/z4MkVD.png)@Venky –

答えて

0

キャストは、DataSetからDataTableまでの誤差が生じていると思います。 DataSetDataTableに直接キャストすることはできません。

以下のコードを使用してください。

BindingSource bs = (BindingSource)dataGridView1.DataSource; 
DataTable dt= (DataTable) bs.DataSource; 

ws.Cells["A1"].LoadFromDataTable(dt, true); 
+0

simular mistake; 'System.Data.DataSet'オブジェクト型 'System.Windows.Forms.BindingSource'が親切ではありません。 –

+0

ボタンクリックイベントのどこかに「ブレークポイント」を置き、1行ずつデバッグを開始すると、どのラインで例外が発生しているかが分かり、より詳細な例外メッセージが得られます。これが解決策を見つける唯一の方法です。 – Venky

1

あなたは右の私は、あなたがエラーを取得していて、完全な例外の詳細を投稿してくださいすることができますどのラインでのDataTable

var saveFileDialog1 = new SaveFileDialog(); 
     saveFileDialog1.Filter = "Excel Dosyası (*.xlsx)|*.xlsx"; 
     saveFileDialog1.FilterIndex = 1; 
     if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      try 
      { 
       DataSet ds = dataGridView1.DataSource as DataSet; 
       if (ds != null) 
       { 
        DataTable tbl = ds.Tables["hostes_tablo"]; 

        FileInfo file = new FileInfo(saveFileDialog1.FileName); 
        if (file.Exists) 
        { 
         file.Delete(); 
        } 

        using (ExcelPackage pck = new ExcelPackage(file)) 
        { 
         ExcelWorksheet ws = pck.Workbook.Worksheets.Add("AjansRed Sorgu Sonuç"); 
         ws.Cells["A1"].LoadFromDataTable(tbl, true); 
         ws.Cells.AutoFitColumns(); 
         ws.Cells[1,dataGridView1.Columns.Count+2].Value = label81.Text.ToString(); 
         using (ExcelRange rng = ws.Cells[1, 1, 1, dataGridView1.Columns.Count+1])//ws.cells[from row, from column, to row, to column]. sayıların anlamı 
         { 
          rng.Style.Font.Bold = true; 
          rng.Style.Fill.PatternType = ExcelFillStyle.Solid; 
          rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(79, 129, 189)); 
          rng.Style.Font.Color.SetColor(System.Drawing.Color.White); 
         } 

         pck.Save(); 
         pck.Dispose(); 
        } 

        MessageBox.Show(string.Format(@"Sorgu Sonucunuzu İçeren ""{0}"" Başarıyla Dışarıya Aktarıldı!", file.Name)); 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Hata! Hata! Hata! Excel Dışarı Aktarma Esnasında Sorun Oluştu. Original error: " + ex.Message); 
      } 
     } 
+0

ニース... :-) ... – Venky

関連する問題