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
|
Imports OXL = Microsoft.Office.Interop.Excel
Public Class Form1
Dim cheminFichier As String = ""
Dim appExcel As OXL.Application
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim openDLG As New OpenFileDialog
openDLG.Filter = "Fichiers Excel(*.xls)|*.xls"
If openDLG.ShowDialog = System.Windows.Forms.DialogResult.OK Then
cheminFichier = openDLG.FileName
If Len(cheminFichier) = 0 Then
Exit Sub
End If
End If
'si il ya une application excel en cours la fermer
If appExcel IsNot Nothing Then
appExcel.Quit()
appExcel = Nothing
End If
appExcel = New OXL.Application
Dim oBook As OXL.Workbook
Dim oSheet As OXL.Worksheet
oBook = appExcel.Workbooks.Open(cheminFichier)
oSheet = oBook.Worksheets(1)
'Affiche AppExcel
appExcel.Visible = True
'Initialise l'interface Range svp
Dim Rng1 As OXL.Range = Nothing
'CAS D'UNE SEULE LIGNE
Rng1 = oSheet.Cells(1, 1) ' Selectionner Cellule Initiale ligne
Rng1.EntireRow.Select() ' Etend Selection à toute la ligne
Rng1.EntireRow.Interior.Color = ColorTranslator.ToWin32(Color.Aquamarine)
'Delete entire row selected
Rng1.EntireRow.Delete()
'CAS DE PLUSIEURS LIGNES
Dim Rng2 As OXL.Range = Nothing
Rng2 = oSheet.Range(oSheet.Cells(4, 4), oSheet.Cells(6, 6)) ' Selectionner Cellules Initiales de lignes
Rng2.EntireRow.Select() ' Etend Selection à toutes les lignes
Rng2.EntireRow.Interior.Color = ColorTranslator.ToWin32(Color.Chocolate)
oBook.Save()
appExcel.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel)
oBook = Nothing
appExcel = Nothing
End Sub
End Class |
Partager