Как экспортировать выбранные строки из datagridview в excel на языке C#
Я работал с несколькими методами но все они экспортируют datagridview мне просто нужна строка которая будет выбрана
Заранее спасибо
Что я уже пробовал:
я попробовал этот код, но он позволяет экспортировать все строки datagridview, но я хочу экспортировать только ту строку, которая будет выбрана
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CSharpCornerDemo { public partial class Form3 : Form { public Form3() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { myDataGridView.Columns.Clear(); myDataGridView.ColumnCount = 2; myDataGridView.Columns[0].Name = "User ID"; myDataGridView.Columns[1].Name = "Password"; string[] row = new string[] { "abc", "abc" }; myDataGridView.Rows.Add(row); row = new string[] { "pqr", "pqr" }; myDataGridView.Rows.Add(row); row = new string[] { "ghanashyam", "ghanashyam" }; myDataGridView.Rows.Add(row); row = new string[] { "jignesh", "jignesh" }; myDataGridView.Rows.Add(row); DataGridViewLinkColumn dgvLink = new DataGridViewLinkColumn(); dgvLink.UseColumnTextForLinkValue = true; dgvLink.LinkBehavior = LinkBehavior.SystemDefault; dgvLink.HeaderText = "Link Data"; dgvLink.Name = "SiteName"; dgvLink.LinkColor = Color.Blue; dgvLink.TrackVisitedState = true; dgvLink.Text = "http://www.c-sharpcorner.com"; dgvLink.UseColumnTextForLinkValue = true; myDataGridView.Columns.Add(dgvLink); } private void myDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { string UserId = myDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString(); string Password = myDataGridView.Rows[e.RowIndex].Cells[1].Value.ToString(); // creating Excel Application Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); // creating new WorkBook within Excel application Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); // creating new Excelsheet in workbook Microsoft.Office.Interop.Excel._Worksheet worksheet = null; // see the excel sheet behind the program app.Visible = true; // get the reference of first sheet. By default its name is Sheet1. // store its reference to worksheet worksheet = workbook.Sheets["Sheet1"]; worksheet = workbook.ActiveSheet; // changing the name of active sheet worksheet.Name = "Exported from gridview"; // storing header part in Excel for (int i = 1; i < myDataGridView.Columns.Count + 1; i++) { worksheet.Cells[1, i] = myDataGridView.Columns[i - 1].HeaderText; } // storing Each row and column value to excel sheet for (int j = 0; j < myDataGridView.Columns.Count; j++) { worksheet.Cells[0, j + 1] = myDataGridView.Rows[0].Cells[j].Value.ToString(); } // save the application workbook.SaveAs("c:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // Exit from the application app.Quit(); } } }