سبق أن خبرتك مالي في هذا المجال منشان هيك خبرت صديق لي بيعرف VB وأرسلت له طلبك مع رابط مساعد
فعملك تشكيلة دوال التعامل مع ملف الاكسل إن شاء الله بتعجبك وتلبي متطلبات مشروعك
1: طباعة مجال محدد من شيت معين من ملف اكسل PrintExcelSheetRange
2: تحويل مجال محدد من شيت معين من ملف اكسل إلى بي دي اف ExportExcelRangeToPdf
3: تحويل شيت معين من ملف اكسل إلى بي دي اف ExportExcelSheetToPdf
4: تحويل كامل ملف اكسل إلى بي دي اف ExportExcelWorkbookToPdf
PrintExcelSheetRange
كود :
''' <summary>طباعة مجال محدد من شيت معين من ملف اكسل</summary>
''' <param name="excelFile">إسم ملف الاكسل</param>
''' <param name="sheetName">إسم الشيت</param>
''' <param name="rangeAddress">A1:G50 عنوان المجال مثل</param>
''' <param name="copies">إختياري: عدد نسخ الطباعة</param>
Shared Sub PrintExcelSheetRange(ByVal excelFile As String, ByVal sheetName As String, ByVal rangeAddress As String, Optional ByVal copies As Integer = 1)
If Not IO.File.Exists(excelFile) Then
MsgBox("لم أجد ملف الاكسل بإسم" & vbCrLf & excelFile)
Return
End If
Dim excelApplication As New Excel.Application
Dim excelWorkbook As Excel.Workbook = Nothing
Dim excelSheet As Excel.Worksheet = Nothing
Try
excelApplication.Visible = False
excelWorkbook = excelApplication.Workbooks.Open(excelFile)
excelSheet = excelWorkbook.Sheets(sheetName)
excelSheet.PageSetup.PrintArea = rangeAddress
excelSheet.PrintOut(From:=1, To:=7, Copies:=copies, Collate:=True)
Catch ex As Exception
' Respond to the error.
End Try
' Close the workbook object.
If Not excelWorkbook Is Nothing Then
excelWorkbook.Close(False)
excelWorkbook = Nothing
End If
' Quit Excel and release the ApplicationClass object.
If Not excelApplication Is Nothing Then
excelApplication.Quit()
excelApplication = Nothing
End If
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
ExportExcelRangeToPdf
كود :
''' <summary>تحويل مجال محدد من شيت معين من ملف اكسل إلى بي دي اف</summary>
''' <param name="excelFile">إسم ملف الاكسل</param>
''' <param name="sheetName">إسم الشيت</param>
''' <param name="rangeAddress">A1:G50 عنوان المجال مثل</param>
''' <param name="pdtFile">إسم ملف بي دي اف</param>
Shared Sub ExportExcelRangeToPdf(ByVal excelFile As String, ByVal sheetName As String, ByVal rangeAddress As String, ByVal pdtFile As String)
If Not IO.File.Exists(excelFile) Then
MsgBox("لم أجد ملف الاكسل بإسم" & vbCrLf & excelFile)
Return
End If
Dim excelApplication As New Excel.Application
Dim excelWorkbook As Excel.Workbook = Nothing
Dim excelSheet As Excel.Worksheet = Nothing
Dim excelRange As Excel.Range = Nothing
Try
' Open the source workbook.
excelWorkbook = excelApplication.Workbooks.Open(excelFile)
If excelWorkbook.Sheets.Cast(Of Object).Any(Function(x) x.Name.Equals(sheetName)) Then
excelSheet = excelWorkbook.Sheets(sheetName)
Dim paramExportFormat As Excel.XlFixedFormatType = Excel.XlFixedFormatType.xlTypePDF
Dim paramExportQuality As Excel.XlFixedFormatQuality = Excel.XlFixedFormatQuality.xlQualityStandard
Dim paramOpenAfterPublish As Boolean = False
Dim paramIncludeDocProps As Boolean = True
Dim paramIgnorePrintAreas As Boolean = True
Dim paramFromPage As Object = Type.Missing
Dim paramToPage As Object = Type.Missing
excelRange = excelSheet.Range(rangeAddress)
' Save it in the target format.
If Not excelRange Is Nothing Then
excelRange.ExportAsFixedFormat(
paramExportFormat, _
pdtFile, paramExportQuality, _
paramIncludeDocProps, paramIgnorePrintAreas, _
paramFromPage, paramToPage, paramOpenAfterPublish)
End If
Else
MsgBox("لم أجد شيت بإسم" & vbCrLf & sheetName)
End If
Catch ex As Exception
' Respond to the error.
End Try
' Close the workbook object.
If Not excelWorkbook Is Nothing Then
excelWorkbook.Close(False)
excelWorkbook = Nothing
End If
' Quit Excel and release the ApplicationClass object.
If Not excelApplication Is Nothing Then
excelApplication.Quit()
excelApplication = Nothing
End If
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
ExportExcelSheetToPdf
كود :
''' <summary>تحويل شيت معين من ملف اكسل إلى بي دي اف</summary>
''' <param name="excelFile">إسم ملف الاكسل</param>
''' <param name="sheetName">إسم الشيت</param>
''' <param name="pdtFile">إسم ملف بي دي اف</param>
Shared Sub ExportExcelSheetToPdf(ByVal excelFile As String, ByVal sheetName As String, ByVal pdtFile As String)
If Not IO.File.Exists(excelFile) Then
MsgBox("لم أجد ملف الاكسل بإسم" & vbCrLf & excelFile)
Return
End If
Dim excelApplication As New Excel.Application
Dim excelWorkbook As Excel.Workbook = Nothing
Dim excelSheet As Excel.Worksheet = Nothing
Try
' Open the source workbook.
excelWorkbook = excelApplication.Workbooks.Open(excelFile)
If excelWorkbook.Sheets.Cast(Of Object).Any(Function(x) x.Name.Equals(sheetName)) Then
excelSheet = excelWorkbook.Sheets(sheetName)
Dim paramExportFormat As Excel.XlFixedFormatType = Excel.XlFixedFormatType.xlTypePDF
Dim paramExportQuality As Excel.XlFixedFormatQuality = Excel.XlFixedFormatQuality.xlQualityStandard
Dim paramOpenAfterPublish As Boolean = False
Dim paramIncludeDocProps As Boolean = True
Dim paramIgnorePrintAreas As Boolean = True
Dim paramFromPage As Object = Type.Missing
Dim paramToPage As Object = Type.Missing
' Save it in the target format.
If Not excelSheet Is Nothing Then
excelSheet.ExportAsFixedFormat(
paramExportFormat, _
pdtFile, paramExportQuality, _
paramIncludeDocProps, paramIgnorePrintAreas, _
paramFromPage, paramToPage, paramOpenAfterPublish)
End If
Else
MsgBox("لم أجد شيت بإسم" & vbCrLf & sheetName)
End If
Catch ex As Exception
' Respond to the error.
End Try
' Close the workbook object.
If Not excelWorkbook Is Nothing Then
excelWorkbook.Close(False)
excelWorkbook = Nothing
End If
' Quit Excel and release the ApplicationClass object.
If Not excelApplication Is Nothing Then
excelApplication.Quit()
excelApplication = Nothing
End If
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
ExportExcelWorkbookToPdf
كود :
''' <summary>تحويل كامل ملف اكسل إلى بي دي اف</summary>
''' <param name="excelFile">إسم ملف الاكسل</param>
''' <param name="pdtFile">إسم ملف بي دي اف</param>
Shared Sub ExportExcelWorkbookToPdf(ByVal excelFile As String, ByVal pdtFile As String)
If Not IO.File.Exists(excelFile) Then
MsgBox("لم أجد ملف الاكسل بإسم" & vbCrLf & excelFile)
Return
End If
Dim excelApplication As New Excel.Application
Dim excelWorkbook As Excel.Workbook = Nothing
Try
Dim paramExportFormat As Excel.XlFixedFormatType = Excel.XlFixedFormatType.xlTypePDF
Dim paramExportQuality As Excel.XlFixedFormatQuality = Excel.XlFixedFormatQuality.xlQualityStandard
Dim paramOpenAfterPublish As Boolean = False
Dim paramIncludeDocProps As Boolean = True
Dim paramIgnorePrintAreas As Boolean = True
Dim paramFromPage As Object = Type.Missing
Dim paramToPage As Object = Type.Missing
' Open the source workbook.
excelWorkbook = excelApplication.Workbooks.Open(excelFile)
' Save it in the target format.
If Not excelWorkbook Is Nothing Then
excelWorkbook.ExportAsFixedFormat(
paramExportFormat, _
pdtFile, paramExportQuality, _
paramIncludeDocProps, paramIgnorePrintAreas, _
paramFromPage, paramToPage, paramOpenAfterPublish)
End If
Catch ex As Exception
' Respond to the error.
End Try
' Close the workbook object.
If Not excelWorkbook Is Nothing Then
excelWorkbook.Close(False)
excelWorkbook = Nothing
End If
' Quit Excel and release the ApplicationClass object.
If Not excelApplication Is Nothing Then
excelApplication.Quit()
excelApplication = Nothing
End If
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub