30-11-15, 01:43 AM
ولو ان الكود VB وليس CS لكن منشان خطرك عدلتلك اياه
كود :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As New Excel.Application
Try
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
'~~> Opens Source Workbook. Change path and filename as applicable
xlWorkBook = xlApp.Workbooks.Open("C:\Print.xlsx")
'~~> Display Excel
xlApp.Visible = False
'~~> Set the source worksheet
xlWorkSheet = xlWorkBook.Sheets("sheets")
Dim Sa = TextBox1.Text
'~~> Printing the Excel Sheet
xlWorkSheet.PrintOut(From:=1, To:=7, Copies:=Sa, Collate:=True)
xlWorkSheet = Nothing
xlWorkBook = Nothing ' غيرته
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
xlApp.Quit()
xlApp = Nothing ' زيادة - هاد مهم منشان الامر التالي
GC.Collect() ' زيادة - وهاد هو المهم منشان يحذفه من التاسك بعد ما ينهي مهمته
End Sub