简体   繁体   中英

how to export grid view excel data using save File Dialog whithout using datasource

I really don't know guys if U can understand my problem but I will try to make it clear as possible, I have a DataGrid view and I don't use Entity framework to fill it with data just I've displayed direct data from my database to fill it like:

        private void load()
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["UR2k_CS.Properties.Settings.StoreConnectionString"].ConnectionString);
            SqlDataAdapter data = new SqlDataAdapter("Select * FROM [dbo].[missingItems]", con);
            DataTable table = new DataTable();
            data.Fill(table);
            dataGridView1.Rows.Clear();
            foreach (DataRow item in table.Rows)
            {
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[0].Value = item["RFID"].ToString();
                dataGridView1.Rows[n].Cells[1].Value = item["name"].ToString();
                 dataGridView1.Rows[n].Cells[2].Value = item["model"].ToString();
                 dataGridView1.Rows[n].Cells[3].Value = item["category"].ToString();
                 dataGridView1.Rows[n].Cells[4].Value = item["prix"].ToString();
                 dataGridView1.Rows[n].Cells[5].Value = item["ref"].ToString();

            }

and I want to export its data into.csv file using Save File Dialog ( to make user select save place) and I found that code on the net:

private void btnExportToExcel_Click(object sender, EventArgs e)
{
    var dia = new System.Windows.Forms.SaveFileDialog();
    dia.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    dia.Filter = "Excel Worksheets (*.xlsx)|*.xlsx|xls file (*.xls)|*.xls|All files (*.*)|*.*";
    if(dia.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
    {
        DataTable data = null;// use the DataSource of the DataGridView here
        var excel = new OfficeOpenXml.ExcelPackage();
        var ws = excel.Workbook.Worksheets.Add("worksheet-name");
        // you can also use LoadFromCollection with an `IEnumerable<SomeType>`
        ws.Cells["A1"].LoadFromDataTable(data, true, OfficeOpenXml.Table.TableStyles.Light1);
        ws.Cells[ws.Dimension.Address.ToString()].AutoFitColumns();

        using(var file = File.Create(dia.FileName))
            excel.SaveAs(file);
    }
}

but As U see Guys I don't have data source so how to fill it with my grid view data.

You have a datatable...

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["UR2k_CS.Properties.Settings.StoreConnectionString"].ConnectionString);
SqlDataAdapter data = new SqlDataAdapter("Select * FROM [dbo].[missingItems]", con);
DataTable table = new DataTable();
data.Fill(table);

Why dont you use just this table to export to excel? If your datable doesnt match the columns you want to export, you still would have to manage column mapping...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM