View Full Version : خواندن فایلهای excel
sahele_sheni
یک شنبه 22 فروردین 1389, 01:17 صبح
سلام . با استفاده از کلاس Microsoft.Office.Interop.Excel یه فایل excel روبخونم ؟؟؟؟
Shahram_Shobeiri
یک شنبه 22 فروردین 1389, 10:01 صبح
در ابتدا کلاس کتابخانه excel رو به پروژه اضافه کنید :
Project->Add Reference->COM->Microsoft Excel 12.0 Object Library->OK
سپس دو خط زیر را بالای برنامه درج کنید :
Imports Excel = Microsoft.Office.Interop.Excel
Imports Office = Microsoft.Office.Core
این هم یک مثال کامل :
Project->Add Reference->COM->Microsoft Excel 12.0 Object Library->OK
' Now import classes from reference
Imports Excel = Microsoft.Office.Interop.Excel
Imports Office = Microsoft.Office.Core
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Local variables
Dim xlsApp As Excel.Application
Dim xlsWorkBook As Excel.Workbook
Dim xlsWorkSheet As Excel.Worksheet
Dim xlsCell As Excel.Range
' Initialise Excel Object
xlsApp = New Excel.Application
' Open test Excel spreadsheet
xlsWorkBook = xlsApp.Workbooks.Open("c:\test.xls")
' Open worksheet (can open by number or name)
xlsWorkSheet = xlsWorkBook.Worksheets(1) ' You could also do it by name: "sheet1"
' Read the first cell
xlsCell = xlsWorkSheet.Range("A1")
' Display the first cell
MsgBox(xlsCell.Text)
End Sub
End Class
Shahram_Shobeiri
یک شنبه 22 فروردین 1389, 10:13 صبح
از طریق oledb هم میشه به فایل های excel متصل شد و مثل دیتابیس با اونها کار کرد. هر شیت میشه معادل یک تیبل!
sahele_sheni
یک شنبه 22 فروردین 1389, 13:41 عصر
ممنون دوست عزیز . کد شما رو که اجرا کردم این error رو میده ....
Exception from HRESULT: 0x800A03EC
...مشکل چیه ؟؟؟؟
از روش oledb میتونم فایل رو لود کنم ولی یه سری ستون های زیادی هم به grid اضافه میکنه ....
Shahram_Shobeiri
یک شنبه 22 فروردین 1389, 14:06 عصر
http://www.made4dotnet.com/Default.aspx?tabid=141&aid=15
این لینک احتمالاً مشکل شما رو حل کنه.
sahele_sheni
دوشنبه 23 فروردین 1389, 11:07 صبح
سلام . من از این کد استفاده میکنم ولی چیزی توی grid نشون نمیده .....
Imports Excel = Microsoft.Office.Interop.Excel
Imports Office = Microsoft.Office.Core
Imports System.Globalization.CultureInfo
Public Class Form1
Dim oldCI As Globalization.CultureInfo
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
SetNewCurrentCulture()
Dim xlsApp As Excel.Application
Dim xlsWorkBook As Excel.Workbook
Dim xlsWorkSheet As Excel.Worksheet
Dim xlsCell As Excel.Range
' Initialise Excel Object
xlsApp = New Excel.Application
' Open test Excel spreadsheet
xlsWorkBook = xlsApp.Workbooks.Open("d:\test.xls")
' Open worksheet (can open by number or name)
xlsWorkSheet = xlsWorkBook.Worksheets(1) ' You could also do it by name: "sheet1"
' Read the first cell
xlsCell = xlsWorkSheet.Range("Sheet1")
' Display the first cell
DataGridView1.DataSource = xlsWorkBook
DataGridView1.Update()
ResetCurrentCulture()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub SetNewCurrentCulture()
oldCI = System.Threading.Thread.CurrentThread.CurrentCultu re
System.Threading.Thread.CurrentThread.CurrentCultu re = New System.Globalization.CultureInfo("en-US")
End Sub
Private Sub ResetCurrentCulture()
System.Threading.Thread.CurrentThread.CurrentCultu re = oldCI
End Sub
End Class
اگه ممکنه این کد رو تست کنید ... ممنون
vBulletin® v4.2.5, Copyright ©2000-1403, Jelsoft Enterprises Ltd.