// storing header part in Excel for (var i = 0; i < dataGridView1.Columns.Count; i++) worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;// storing Each row and column value to excel sheet for (var i = 0; i < dataGridView1.SelectedRows.Count; i++){ for (var j = 0; j < dataGridView1.Columns.Count; j++) { if (dataGridView1.Rows[i].Cells[j].Value == null) continue; worksheet.Cells[2 + i, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString(); }} 顺便说一句 - 我不会把它放在一个广义的CellContentClick事件中 - 这意味着你很难选择不止一排。如果您打算使用一个显示导出此行的按钮,则需要检查您是否正在点击正确的列,然后再直接创建电子表格。As an aside - I wouldn't have put this in a generalised CellContentClick event - it means that you will struggle to select more than one row. If you intend to have a button that says "export this row" then you need to check that you are clicking in the correct column before diving straight into creating a spreadsheet. 这篇关于如何将所选行从datagridview导出到C#中的excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-21 15:06