【问题标题】:Epplus Export Error like this : 'System.Data.DataSet' the object type 'System.Data.DataTable' not taken in kind.Epplus 导出错误如下:'System.Data.DataSet' 对象类型'System.Data.DataTable' 不是实物。
【发布时间】:2016-10-23 04:52:36
【问题描述】:

我想将 gridview 转换为 .xls,但它会引发错误,当我单击确定时,它会给我“无法导出到 excel。原始错误:'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);
            }
        }

我搜索然后当我点击导出按钮时它给我错误。

【问题讨论】:

  • 您在哪一行收到错误,能否请您发布完整的异常详细信息。
  • 不排队。它在程序运行时给我。 Click to see error@Venky

标签: c# excel datatable export-to-excel epplus-4


【解决方案1】:

没错,我是从 DataSet 转换为 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);
            }
        }

【讨论】:

    【解决方案2】:

    我认为从DataSetDataTable 的转换是错误的。您不能将DataSet 直接转换为DataTable

    使用下面的代码来做到这一点。

    BindingSource bs = (BindingSource)dataGridView1.DataSource; 
    DataTable dt= (DataTable) bs.DataSource;
    
     ws.Cells["A1"].LoadFromDataTable(dt, true);
    

    【讨论】:

    • 类似错误; 'System.Data.DataSet' 对象类型'System.Windows.Forms.BindingSource' 不是实物。
    • 您应该在按钮单击事件中的某处放置一个breakpoint,然后逐行开始调试,然后您将知道发生在哪一行,并为您提供更详细的异常消息。这是您找到解决方案的唯一方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 2022-01-05
    • 2021-08-05
    • 2018-02-14
    • 1970-01-01
    • 2015-01-25
    相关资源
    最近更新 更多