تصدير بيانات DataGridView1 الى ملف Excel - mr_hso - 11-01-21
السلام عليكم
كل عام وانتم بخير
اتمني ان تكونوا بخير حال وافضل صحة
لدي مشروع فيجوال بيسك مرتبط بقاعدة بيانات Access
لدي Form به DataGridView اريد تصدير البيانات التي به الى ملف اكسيل عن طريق مربع الحوار حفظ الى Save as
لتحديد مكان الحفظ
انا استخدم هذا الكود
كود :
Dim MsExcel = CreateObject("Excel.Application")
MsExcel.Workbooks.Add()
For i As Integer = 0 To 3
Next
MsExcel.Cells(0 + 1).Value = DataGridView1.Columns(0).HeaderText
MsExcel.Cells(1 + 1).Value = DataGridView1.Columns(2).HeaderText
MsExcel.Cells(2 + 1).Value = DataGridView1.Columns(3).HeaderText
MsExcel.Cells(3 + 1).Value = DataGridView1.Columns(4).HeaderText
MsExcel.Cells(4 + 1).Value = DataGridView1.Columns(5).HeaderText
MsExcel.Cells(5 + 1).Value = DataGridView1.Columns(10).HeaderText
MsExcel.Cells(6 + 1).Value = DataGridView1.Columns(12).HeaderText
MsExcel.Cells(7 + 1).Value = DataGridView1.Columns(37).HeaderText
For j As Integer = 0 To DataGridView1.Rows.Count - 1
MsExcel.Columns.Font.Name = "Times New Roman"
MsExcel.Cells(j + 2, 1).Value = DataGridView1.Rows(j).Cells(0).Value
MsExcel.Cells(j + 2, 2).Value = DataGridView1.Rows(j).Cells(2).Value
MsExcel.Cells(j + 2, 3).Value = DataGridView1.Rows(j).Cells(3).Value
MsExcel.Cells(j + 2, 4).Value = DataGridView1.Rows(j).Cells(4).Value
MsExcel.Cells(j + 2, 5).Value = DataGridView1.Rows(j).Cells(5).Value
MsExcel.Cells(j + 2, 6).Value = DataGridView1.Rows(j).Cells(10).Value
MsExcel.Cells(j + 2, 7).Value = DataGridView1.Rows(j).Cells(12).Value
MsExcel.Cells(j + 2, 8).Value = DataGridView1.Rows(j).Cells(37).Value
Next
MsExcel.Visible = True
وكذلك اريد ربط عملية التصدير هذه بــ ProgressBar
ارجوا ان اجد ضالتي لديكم
كفانا الله واياكم شر الوباء
سبحان الله وبحمده سبحان الله العظيم
RE: تصدير بيانات DataGridView1 الى ملف Excel - mr_hso - 17-01-21
الحمد لله وجدت الحل
اولاً ساقوم بادراج SaveFileDialog1 الى الفورم ثم اضيف BackgroundWorker1
عند حدث الضغط على الزر الذي سينفذ عملية الاستخراج اكتب الكود التالي
كود :
If BackgroundWorker1.IsBusy Then
MsgBox("جاري استخراج ملف الان انتظر حتى تنتهي عملية الاستخراج ", Title:="تنبيه")
Else
SaveFileDialog1.Title = "Save Excel File"
SaveFileDialog1.Filter = "Excel files (*.xlsx)|*.xlsx"
SaveFileDialog1.ShowDialog()
If SaveFileDialog1.FileName = "" Then
Exit Sub
End If
BackgroundWorker1.RunWorkerAsync()
End If
وفي حدث BackgroundWorker1_DoWork
كود :
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'create an Excel WorkBook
Dim MsExcel As New Excel.Application
Dim sheet As Excel.Worksheet
MsExcel.Workbooks.Add()
sheet = MsExcel.ActiveWorkbook.ActiveSheet
For i As Integer = 0 To 3
Next
MsExcel.Cells(0 + 1).Value = DataGridView1.Columns(0).HeaderText
MsExcel.Cells(1 + 1).Value = DataGridView1.Columns(2).HeaderText
MsExcel.Cells(2 + 1).Value = DataGridView1.Columns(3).HeaderText
MsExcel.Cells(3 + 1).Value = DataGridView1.Columns(4).HeaderText
MsExcel.Cells(4 + 1).Value = DataGridView1.Columns(5).HeaderText
MsExcel.Cells(5 + 1).Value = DataGridView1.Columns(10).HeaderText
MsExcel.Cells(6 + 1).Value = DataGridView1.Columns(12).HeaderText
MsExcel.Cells(7 + 1).Value = DataGridView1.Columns(37).HeaderText
For j As Integer = 0 To DataGridView1.Rows.Count - 1
MsExcel.Columns.Font.Name = "Times New Roman"
MsExcel.Cells(j + 2, 1).Value = DataGridView1.Rows(j).Cells(0).Value
MsExcel.Cells(j + 2, 2).Value = DataGridView1.Rows(j).Cells(2).Value
MsExcel.Cells(j + 2, 3).Value = DataGridView1.Rows(j).Cells(3).Value
MsExcel.Cells(j + 2, 4).Value = DataGridView1.Rows(j).Cells(4).Value
MsExcel.Cells(j + 2, 5).Value = DataGridView1.Rows(j).Cells(5).Value
MsExcel.Cells(j + 2, 6).Value = DataGridView1.Rows(j).Cells(10).Value
MsExcel.Cells(j + 2, 7).Value = DataGridView1.Rows(j).Cells(12).Value
MsExcel.Cells(j + 2, 8).Value = DataGridView1.Rows(j).Cells(37).Value
BackgroundWorker1.ReportProgress(j)
Next
MsExcel.ActiveWorkbook.SaveAs(SaveFileDialog1.FileName)
MsExcel.Workbooks.Close()
MsExcel.Quit()
End Sub
وفي حدث BackgroundWorker1_ProgressChanged اضع الكود التالي
كود :
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
Label2.Text = e.ProgressPercentage + 1
End Sub
وفي حدث BackgroundWorker1_RunWorkerCompleted اضع الكود التالي
كود :
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
MsgBox("تمت العملية بنجاح", Title:="تهنئة")
End Sub
وتاكد من ان قيمة worker report progress = true وهذا فى خصائص ال BackgroundWorker1
والله الموفق والمستعان
RE: تصدير بيانات DataGridView1 الى ملف Excel - mr_hso - 18-01-21
نسيت ان اخبركم بوضع ProgressBar1 في الفورم ليتم التقدم مع عملية استخراج ملف الاكسيل
RE: تصدير بيانات DataGridView1 الى ملف Excel - atefkhalf2004 - 20-01-21
http://vb4arb.com/vb/member.php?action=profile&uid=36040
السيد الفاضل
mr_hso
ممكن تضع المثال كامل
حاولت اطبق المثال
لم اصل
RE: تصدير بيانات DataGridView1 الى ملف Excel - atefkhalf2004 - 20-01-21
السيد الفاضل اين المثال
RE: تصدير بيانات DataGridView1 الى ملف Excel - mr_hso - 24-01-21
(20-01-21, 10:15 PM)atefkhalf2004 كتب : السيد الفاضل اين المثال
اسف اخي الكريم انشغلت ولم اقراء تعليقك الا الان
ارجو ان تتقبل اعتذاري
المثال مرفق لتعم الافادة للجميع ان شاء الله
RE: تصدير بيانات DataGridView1 الى ملف Excel - إليسار - 22-07-21
بطل يعطيك العافيه
RE: تصدير بيانات DataGridView1 الى ملف Excel - atefkhalf2004 - 22-07-21
شكرا كثيرا بتعدي علي الواحد رغما عنه
مرة اخري شكرا
|