Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用EPPlus将过滤后的DataGridView数据保存到Excel中?_C#_Excel_Datagridview_Epplus - Fatal编程技术网

C# 如何使用EPPlus将过滤后的DataGridView数据保存到Excel中?

C# 如何使用EPPlus将过滤后的DataGridView数据保存到Excel中?,c#,excel,datagridview,epplus,C#,Excel,Datagridview,Epplus,如何使用EPPlus将过滤后的数据从DataGridView导入Excel? 我不知道从哪里开始,我没有发现任何类似我的问题 这是“我的保存”按钮的代码: SaveFileDialog saveFileDialog1 = new SaveFileDialog(); using (MySqlConnection con = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand(

如何使用EPPlus将过滤后的数据从
DataGridView
导入Excel? 我不知道从哪里开始,我没有发现任何类似我的问题

这是“我的保存”按钮的代码:

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
using (MySqlConnection con = new MySqlConnection(connectionString))
{
    using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM statusrouted.routed", con))
    {
        cmd.CommandType = CommandType.Text;
        using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
        {
            using (DataTable dt = new DataTable())
            {
                using (ExcelPackage pck = new ExcelPackage())
                {
                    sda.Fill(dt);


                    ExcelWorksheet ws = pck.Workbook.Worksheets.Add(DateTime.Today.ToString("MMMM-yyyy"));

                    ws.Cells["A2"].LoadFromDataTable( dt, true);


                    saveFileDialog1.Title = "Save as Excel";
                    saveFileDialog1.FileName = "";
                    saveFileDialog1.Filter = "Excel files(2007)|*.xlsx";

                    if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
                    {
                        try
                        {
                            pck.SaveAs(new FileInfo(@"" + saveFileDialog1.FileName));
                            recentsToolStripMenuItem1.AddRecentItem(@"" + saveFileDialog1.FileName);
                        }
                        catch (Exception)
                        {
                            DialogResult reminder = MessageBox.Show("Cannot save file, file opened in another program.\nClose it first! ", "Save Failed", MessageBoxButtons.OK);
                        }
                    }
                }
            }
        }
    }
}
这是
textbox\u textchanged
事件中我的过滤器的代码: 我不知道这是否重要

DataView DV = new DataView(dt);
string oks;
if (comboBox1.SelectedIndex == 0)
{
    oks = "ffrom";
}
else if (comboBox1.SelectedIndex == 1)
{
    oks = "office";
}
else if (comboBox1.SelectedIndex == 2)
{
    oks = "code";
}

else
{
    if (comboBox1.SelectedIndex == 3)
    {
        oks = "datein";
    }
    else
    {
        oks = "dateout";
    }
}

DV.RowFilter = string.Format( oks+ " LIKE '%{0}%'", textBox5.Text);
this.dataGridView1.DataSource = DV;
dataGridView1.ClearSelection();

找到了!我刚刚改变了这个:

ws.Cells["A2"].LoadFromDataTable(dt, true);
为此:

ws.Cells["A2"].LoadFromDataTable((this.maindgv.DataSource as DataTable).DefaultView.ToTable(), true);

我不确定它是如何工作的,但我认为这是因为
dt
是加载到
DataGridView
的数据,而
(this.maindgv.DataSource为DataTable)。DefaultView.ToTable()
是当前显示在
DataGridView
上的数据。但我不确定。

发现:试图将其转换为适合我的代码,但不起作用。您对数据的假设是正确的。这就是我回答的要点,传递
DataTable
会将所有数据导出到Excel,同时传递
DataView.ToTable()
会导出所有后期过滤的数据。如果在使用
ClosedXML
而不是
ExcelPackage
的所有额外详细信息中丢失了这一点,那么很抱歉。