为了加速将datagridview中保存的数据导出为excel,我在本论坛中使用了一个建议的方法,其中datagridview的内容被复制到剪贴板,然后按顺序粘贴到excel电子表格中减少应用程序与excel通信的次数。
虽然这很好,但是一个缺点是,如果用户在datagridview中选择了特定的单元格 - 代码将导致此选择丢失,因为它使用datagridview.SelectAll()
方法。
我希望找到一个简单的解决方案来重新选择导出到excel的用户原始选择。我尝试过以下方法:
Dim mySelection As DataGridViewSelectedCellCollection
mySelection = myDataGridView.SelectedCells
ExportToExcel(myDataGridView, "Exported Data")
myDataGridView.SelectedCells = mySelection
我怀疑DataGridViewSelectedCellCollection
在这里使用不正确,因为它似乎与选择中保留的数据有关,而不是所选单元格的位置。
替代方法是有一种方法可以将所有datagridview放入剪贴板而无需使用SelectAll()
吗?
如果需要,导出到Excel代码在此处:
Private Sub ExportToExcel(myDataGridView As DataGridView, myWorksheetName As String)
' Creating a Excel object.
Dim excel As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
Dim workbook As Microsoft.Office.Interop.Excel._Workbook = excel.Workbooks.Add(Type.Missing)
Dim xlWorkSheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing
Try
xlWorkSheet = workbook.ActiveSheet
xlWorkSheet.Name = myWorksheetName
'Data transfer from grid to Excel.
With xlWorkSheet
.Range("1:1").EntireRow.Font.Bold = True
'Set Clipboard Copy Mode
myDataGridView.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
myDataGridView.SelectAll()
'Get the content from Grid for Clipboard
Dim str As String = TryCast(myDataGridView.GetClipboardContent().GetData(DataFormats.UnicodeText), String)
'Set the content to Clipboard
Clipboard.SetText(str, TextDataFormat.UnicodeText)
'Identify and select the range of cells in Excel to paste the clipboard data.
.Range("A1").Select()
'Paste the clipboard data
.Paste()
Clipboard.Clear()
End With
'Getting the location and file name of the excel to save from user.
Dim saveDialog As New SaveFileDialog()
saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx"
saveDialog.FilterIndex = 2
If saveDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
workbook.SaveAs(saveDialog.FileName)
MessageBox.Show("Export Successful")
End If
Catch ex As System.Exception
MessageBox.Show(ex.Message)
Finally
excel.Quit()
workbook = Nothing
excel = Nothing
End Try
End Sub
答案 0 :(得分:1)
由于DGV.SelectedCells是只读的,我认为您可能只需要将每个单元格的selected属性设置为true(除非您正在进行完整的行/列选择)。您可以遍历您选择的单元格集合,并重新选择每一个:
myDataGridView.ClearSelection()
If mySelection IsNot Nothing Then
For Each dgvCell As DataGridViewCell In mySelection
dgvCell.Selected = True
Next
mySelection = Nothing
End If
答案 1 :(得分:1)
您可以复制DataGridView
的内容而不更改选择。通过组合制表符分隔的unicode String并将其推入ClipBoard
:
Sub CopyDataGridViewToClipboard(dgv As DataGridView, includeHeader As Boolean)
Dim sbl As New System.Text.StringBuilder
If includeHeader Then
For intCol As Integer = 0 To dgv.Columns.Count - 1
Dim dgvc As DataGridViewColumn = dgv.Columns(intCol)
If intCol > 0 Then sbl.Append(vbTab)
sbl.Append(dgvc.HeaderText)
Next intCol
sbl.AppendLine()
End If
For intRow As Integer = 0 To dgv.Rows.Count - 1
Dim dgvr As DataGridViewRow = dgv.Rows(intRow)
For intCol As Integer = 0 To dgv.Columns.Count - 1
If intCol > 0 Then sbl.Append(vbTab)
sbl.Append(dgvr.Cells(intCol).Value)
Next intCol
sbl.AppendLine()
Next intRow
Clipboard.SetText(sbl.ToString, TextDataFormat.UnicodeText)
End Sub