الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

قراءة ملف اكسيل في الدوت نت

مغلق
بدأه Eng.Tivoli في 23 سبتمبر 2007 · 4 رد · 2,441 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته

حاولت البحث عن طريقة لقراءة بيانات موجودة في ملف اكسيل وعرضها في داتا غريد في فيجوال بيسك دوت نت 2005 ومن ثم تخزين البيانات في الداتابيز

وجدت هذا الكود

' Let the user chose the file they want

Dim xcFileInfo As IO.FileInfo

Dim xcFileDialog As New OpenFileDialog()

xcFileDialog.Filter = "Excel Spreadsheet Files!|*.xls"

xcFileDialog.Title = "Select estimate in excel spreadsheet file!"

If xcFileDialog.ShowDialog = DialogResult.OK Then

   xcFileInfo = New IO.FileInfo(xcFileDialog.FileName)

End If



Dim myExcel As Excel.Application ' Interface to Excel 

Dim myWorkBookCollection As Excel.Workbooks ' Workbook-collection (note the 's' at the end) 

Dim myWorkBook As Excel.Workbook ' Single Workbook (spreadsheet-collection) 

Dim myWorkSheet As Excel.Worksheet ' Single spreadsheet 



' Initialize the interface to Excel.exe 

myExcel = New Excel.Application

If myExcel Is Nothing Then

   MessageBox.Show("Could not load Excel.exe")

   Exit Sub

End If

' initialise access to Excel's workbook collection 

myWorkBookCollection = myExcel.Workbooks

'open spreadsheet from disk 

myWorkBook = myWorkBookCollection.Open(xcFileInfo.FullName, , False)

'get 1st sheet from workbook 

myWorkSheet = myWorkBook.Sheets.Item(1)

'alter contents of 1st cell 

Dim myCell As Object = myWorkSheet.Range("A1", System.Reflection.Missing.Value)

myCell.Value2 = "VB.NET was here!!!"

'display the spreadsheet 

'myExcel.Visible = True

'Read Cell A2

Dim myCell2 As Object = myWorkSheet.Range("A2", System.Reflection.Missing.Value)

Me.Text = myCell.Value2

'save and get out

myWorkBook.Save()

myExcel.Quit()

مع وجوب اضافة ريفرنس أكسيل

أضفت الرفرنس وجربت الكود لكن هذا السطر

myWorkBook = myWorkBookCollection.Open(xcFileInfo.FullName, , False)

يظهر لي هذا الخطأ

NullRefrenceException was handled

object refrence not set to an instance of an object

ممكن تخبروني معنى الخطأ ،، وهل توجد طريقة أخرى لقراءة الملف

شكرا

#2
Public Function Append_ExcelSheets(ByVal WorkbookName As String, ByVal AppendRow As Integer) As Boolean

		Form.ActiveForm.Cursor = System.Windows.Forms.Cursors.WaitCursor



		Dim MyExcel As Excel.Application
		Dim MyWorkBook As Excel.Workbook
		Dim MySheet As Excel.Worksheet
		Dim MyRange As Excel.Range
		Dim SheetName As String, SheetNo As Integer

		' Test to see if there is a copy of Microsoft Excel already running.
		' Getobject function called without the first argument returns a
		' reference to an instance of the application. 
		' If the application is not running, an error occurs.
		Try
			MyExcel = CType(GetObject(, "Excel.Application"), Excel.Application)
		Catch ex As Exception
			Try
				MyExcel = CType(CreateObject("Excel.Application"), Excel.Application)
			Catch eex As Exception
				MsgBox("ُExcel is not installed")
				Exit Function
			End Try
		End Try

		'Save old Locale( do this if you dont have the arabic kit )
		Dim oldCI As System.Globalization.CultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture

		'Change Locale to English
		System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US")


		'Open New WorkBook
		'MyWorkBook = MyExcel.Workbooks.Add
		'MySheet = CType(MyWorkBook.ActiveSheet, Excel.Worksheet)
		'
		'Open an existing Workbook
		MyWorkBook = MyExcel.Workbooks.Open(WorkbookName)
		'
		'Set ProgressBar Parameters
		Dim MyMDIForm As New Form_MDI
		MyMDIForm.MDIProgressBar.Maximum = MyWorkBook.Worksheets.Count
		MyMDIForm.MDIProgressBar.Minimum = 1
		MyMDIForm.MDIProgressBar.Value = 1
		MyMDIForm.MDIProgressBar.Visible = True
		'
		'Open Selected sheet by No or by name then set it as the active sheet
		MySheet = CType(MyWorkBook.Worksheets(1), Excel.Worksheet)
		MySheet.Activate()
		'
		'Appending Sheets
		For SheetNo = 2 To MyWorkBook.Worksheets.Count
			Dim ReadSheet As Excel.Worksheet = CType(MyWorkBook.Worksheets(SheetNo), Excel.Worksheet)
			MyMDIForm.MDIProgressBar.Value += 1
			MyMDIForm.MDIStatusbarPanel.Text = "No Processing Sheet " & ReadSheet.Name
			'Start reading from row2 which is first raw after the Title Row
			Dim ReadSheetRowNo As Integer = 2
			'While the first cell in this row is not empty
			While Get_ExcelCellValue(ReadSheet, ReadSheetRowNo, 1).Length > 0
				'Start copiung in new row in the main sheet
				Copy_Row(ReadSheet, ReadSheetRowNo)
				Paste_Row(MySheet, AppendRow)
				AppendRow += 1 'Advance to next Row in Main Sheet
				ReadSheetRowNo += 1 'Advance to next Row in Read Sheet
			End While
		Next SheetNo

		'Appending sheet Cell by Cell
		'For SheetNo = 2 To MyWorkBook.Worksheets.Count
		'Dim ReadSheet As Excel.Worksheet = CType(MyWorkBook.Worksheets(SheetNo), Excel.Worksheet)
		'MyMDIForm.MDIProgressBar.Value += 1
		'MyMDIForm.MDIStatusbarPanel.Text = "No Processing Sheet " & ReadSheet.Name
		'Dim ReadSheetRowNo As Integer = 2
		'While Get_ExcelCellValue(ReadSheet, ReadSheetRowNo, 1).Length > 0
		'MainSheetLastRow += 1
		'For SheetColNo As Integer = 1 To 9
		'Dim CellValue As String = Get_ExcelCellValue(ReadSheet, ReadSheetRowNo, SheetColNo)
		'Set_ExcelCellValue(MySheet, MainSheetLastRow, SheetColNo, CellValue)
		'Next SheetColNo
		'ReadSheetRowNo += 1
		'End While
		'Next SheetNo

		'Move to the Top of the Sheet
		MySheet.Application.ActiveWindow.ScrollRow = 1
		MySheet.Application.ActiveWindow.ScrollColumn = 1

		'MyWorkbook.Save
		MyExcel.Visible = True
		'MyExcel.Parent.Windows(1).Visible = True

		'MySheet.SaveAs("C:\Crouchie1998 Example.xls")

		MyRange = Nothing
		MySheet = Nothing
		MyWorkBook = Nothing
		'objExcel.Quit()
		MyExcel = Nothing

		'Restore old Locale
		System.Threading.Thread.CurrentThread.CurrentCulture = oldCI

		Form.ActiveForm.Cursor = System.Windows.Forms.Cursors.Default

		MyMDIForm.MDIProgressBar.Visible = False

	End Function
#3

شكرا اخي الكريم على الرد والمساعده

جربت الكود ومازال يظهر لي خطأ في نفس السطر Open (كما في المثال السابق)

MyWorkBook = MyExcel.Workbooks.Open(WorkbookName)

والخطأ هو

Exception from HRESULT

0x80131019.

ارجو المساعده لو تكرمتوا

وجزاكم الله خيرا

#4

هذا الخطأ Exception from HRESULT

0x80131019 غامض نوعا ما

اولا : حاولى ان تفتحى Solution جديد واضيفى مرجع Excel وحاولى تشغيل الكود السابق .

ثانيا : ماهى نسخة vs المستعملة فانا اعمل على vs2003 ولا اواجه مثل هذا الخلل .

ثالثا : ماهو رقم مرجع الاكسل المستخدم

رابعا : هل WorkbookName يشبر الى ملف اكسل موجود وسليم فعلا على جهازك ؟

#5

شكرا اخي وليد على ردك

جربت مثال على 2003 شغال

بس المشروع اللي اشتغل عليه حاليا 2005 وخلصت جميع الشاشات بس بقى شاشة لقراءة ملف اكسيل

يمكن اعمل هذي الشاشه بالvb6 او عن طريق dll اذا لم اجد طريقة

لك مني جزيل الشكر

هذا الموضوع مغلق.

مواضيع مشابهة