1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
Imports System
Imports System.Windows.Forms
Imports OXL = Microsoft.Office.Interop
Public Class frmVersExcel
Inherits Form
Dim appExcel As OXL.Excel.Application
Dim wbk As OXL.Excel.Workbook
Dim wks As OXL.Excel.Worksheet
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
Me.DGV.AllowUserToAddRows = False
Me.DGV.Dock = DockStyle.Fill
Me.btnCollerCellules.Text = "Coller Cellules Selectionnees"
Me.btnCollerCellules.Dock = DockStyle.Fill
Me.btnOuvreClasseur.Text = "Classeur Excel..."
Me.btnOuvreClasseur.Dock = DockStyle.Fill
Me.btnSaveClasseur.Text = "Enreg.Classeur..."
Me.btnSaveClasseur.Dock = DockStyle.Fill
Me.Text = "DataGridView Clipboard "
End Sub
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
' Initialise le DGV.
Me.DGV.ColumnCount = 5
Me.DGV.Rows.Add(New String() {"A", "B", "C", "D", "E"})
Me.DGV.Rows.Add(New String() {"F", "G", "H", "I", "J"})
Me.DGV.Rows.Add(New String() {"K", "L", "M", "N", "O"})
Me.DGV.Rows.Add(New String() {"P", "Q", "R", "S", "T"})
Me.DGV.Rows.Add(New String() {"U", "V", "W", "X", "Y"})
Me.DGV.AutoResizeColumns()
'active mode copier sans headers
Me.DGV.ClipboardCopyMode = _
DataGridViewClipboardCopyMode.EnableWithoutHeaderText
End Sub
Private Sub collerBouton_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnCollerCellules.Click
'si nbre de cellules selectionnes dans DataGridView1 n'est pas nul
If Me.DGV.GetCellCount( _
DataGridViewElementStates.Selected) > 0 Then
Try
' Ajoute la selection DGV au clipboard.
Clipboard.SetDataObject( _
Me.DGV.GetClipboardContent())
If wks IsNot Nothing Then
'copie dans le range selectionne (cellule de depart)
wks.Paste()
End If
Catch ex As System.Runtime.InteropServices.ExternalException
MessageBox.Show( _
"Le Clipboard ne peut pas etre accede. Reessayer ...")
End Try
End If
End Sub
Private Sub btnOuvreClasseur_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOuvreClasseur.Click
Dim openDlg As OpenFileDialog = New OpenFileDialog
openDlg.Filter = "Fichiers Excel(*.xls)|*.xls"
Dim strCheminFichier As String = ""
If openDlg.ShowDialog Then
strCheminFichier = openDlg.FileName
If Len(strCheminFichier) = 0 Then
MessageBox.Show("Entrer un nom de fichier.svp...")
Exit Sub
End If
End If
If appExcel IsNot Nothing Then
appExcel.Quit()
appExcel = Nothing
End If
appExcel = New OXL.Excel.Application
'ouvre classeur
wbk = appExcel.Workbooks.Open(strCheminFichier)
'selectionne feuille 1
wks = wbk.Worksheets(1)
appExcel.Visible = True
End Sub
Private Sub btnSaveClasseur_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveClasseur.Click
If appExcel IsNot Nothing Then
wbk.Save()
wbk = Nothing
wks = Nothing
appExcel.Quit()
appExcel = Nothing
End If
End Sub
End Class |
Partager