23-07-24, 04:53 AM
(22-07-24, 10:29 AM)تركي الحلواني كتب : جرب التعديل هذا
أولا
في نافذة "Package Manager Console"، اكتب الأوامر التالية:
PHP كود :
Install-Package ExcelDataReader
Install-Package ExcelDataReader.DataSet
ثانيا جرب الكود التالي
PHP كود :
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports ExcelDataReader
Public Class Form3
Private Sub ButtonImport_Click(sender As Object, e As EventArgs) Handles ButtonImport.Click
Dim openFileDialog As New OpenFileDialog()
openFileDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm"
If openFileDialog.ShowDialog() = DialogResult.OK Then
ImportExcelToDataGridView(openFileDialog.FileName)
End If
End Sub
Private Sub ImportExcelToDataGridView(ByVal filePath As String)
Try
Using stream = File.Open(filePath, FileMode.Open, FileAccess.Read)
Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateReader(stream)
Dim result As DataSet = excelReader.AsDataSet(New ExcelDataSetConfiguration() With {
.ConfigureDataTable = Function(__) New ExcelDataTableConfiguration() With {
.UseHeaderRow = True
}
})
Dim dataTable As DataTable = result.Tables(0)
DataGridView1.DataSource = dataTable
excelReader.Close()
End Using
Catch ex As Exception
MessageBox.Show("Error importing Excel file: " & ex.Message)
End Try
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
SaveDataGridViewToDatabase()
End Sub
Private Sub ButtonLoad_Click(sender As Object, e As EventArgs) Handles ButtonLoad.Click
LoadDataFromDatabase()
End Sub
Private Sub ButtonExport_Click(sender As Object, e As EventArgs) Handles ButtonExport.Click
ExportDataGridViewToExcel()
End Sub
Private Sub SaveDataGridViewToDatabase()
Dim connectionString As String = "Data Source=.;Initial Catalog=DATABASE;Integrated Security=True"
Using cn As New SqlConnection(connectionString)
cn.Open()
For Each row As DataGridViewRow In DataGridView1.Rows
If Not row.IsNewRow Then
Using cm As New SqlCommand("INSERT INTO Item_Tbl (ItemBarcode, ItemNameA, ItemNameE, ..., Column15) VALUES (@ItemBarcode, @ItemNameA, @ItemNameE, ..., @val15)", cn)
cm.Parameters.AddWithValue("@ItemBarcode", row.Cells(0).Value)
cm.Parameters.AddWithValue("@ItemNameA", row.Cells(1).Value)
cm.Parameters.AddWithValue("@ItemNameE", row.Cells(2).Value)
'...
cm.Parameters.AddWithValue("@val15", row.Cells(14).Value)
cm.ExecuteNonQuery()
End Using
End If
Next
End Using
MessageBox.Show("Data saved successfully!")
End Sub
Private Sub LoadDataFromDatabase()
Dim connectionString As String = "Data Source=.;Initial Catalog=DATABASE;Integrated Security=True"
Using cn As New SqlConnection(connectionString)
Dim query As String = "SELECT * FROM Item_Tbl"
Dim da As New SqlDataAdapter(query, cn)
Dim dt As New DataTable()
da.Fill(dt)
DataGridView1.DataSource = dt
End Using
End Sub
Private Sub ExportDataGridViewToExcel()
Dim excelApp As New Excel.Application()
Dim excelWorkbook As Excel.Workbook = excelApp.Workbooks.Add()
Dim excelWorksheet As Excel.Worksheet = CType(excelWorkbook.Sheets(1), Excel.Worksheet)
' إضافة عناوين الأعمدة
For col As Integer = 0 To DataGridView1.Columns.Count - 1
excelWorksheet.Cells(1, col + 1).Value = DataGridView1.Columns(col).HeaderText
Next
' إضافة البيانات
For row As Integer = 0 To DataGridView1.Rows.Count - 1
For col As Integer = 0 To DataGridView1.Columns.Count - 1
excelWorksheet.Cells(row + 2, col + 1).Value = DataGridView1.Rows(row).Cells(col).Value
Next
Next
' حفظ الملف
Dim saveFileDialog As New SaveFileDialog()
saveFileDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm"
If saveFileDialog.ShowDialog() = DialogResult.OK Then
excelWorkbook.SaveAs(saveFileDialog.FileName)
excelWorkbook.Close()
excelApp.Quit()
MessageBox.Show("Data exported successfully!")
End If
ReleaseObject(excelWorksheet)
ReleaseObject(excelWorkbook)
ReleaseObject(excelApp)
End Sub
Private Sub ReleaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class
تم التجربة ويعمل بشكل جيد وسريع
السلام عليكم
شكرا لك اخي الكريم
ومقدر تعبك معايا
وان شاء الله في ميزان حسناتك
الكود مش راضي يضبط معايا
عند استيراد الاصناف ممتاز ما في اي مشكلة .
ولكن عند عملية الحفظ تظهر لي هذه الرسالة :
