2016-06-21 36 views
0

我想將一個gridview轉換爲.xls,但它會拋出錯誤,當我點擊確定它給我「無法導出到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

在哪一行,你所得到的錯誤,你可以請張貼完整的異常詳細信息。 – Venky

+0

不行。 İt的程序正在運行時給我。 [點擊查看錯誤](http://i.hizliresim.com/z4MkVD.png)@Venky –

回答

0

我認爲鑄造從DataSetDataTable是給出錯誤。你不能直接投DataSetDataTable

使用下面的代碼來做到這一點。

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

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

模擬錯誤; 'System.Data.DataSet'對象類型'System.Windows.Forms.BindingSource'不是實物。 –

+0

你應該在按鈕點擊事件的某個地方放置一個「斷點」,然後逐行開始調試,然後你就會知道異常發生在哪一行,它會給你更詳細的異常消息。這是您找到解決方案的唯一途徑。 – 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

相關問題