PDA

View Full Version : چگونه یک فایل ECXEL را در vb.net باز کنم و..



Afshin160
پنج شنبه 08 شهریور 1386, 01:28 صبح
سلا م دوستان عزیز ..
1) چگونه یک فایل EXCEL را در vb.net باز کنم و مانندVBA در آن برتامه نویسی کنم ....

2) وچرا هیچ یک از کد هایی که گرفتم و در رابطه با باز کردن و ویرایش کردن EXCEL در vb.net است جواب نمی دهد....؟

3) لطفان چند تا کد ساده به من بدهید ....

متشکرم

omidmehraban
پنج شنبه 08 شهریور 1386, 02:36 صبح
با سلام و احترام:
1-از خود componentهای office استفاده کنید
2-یا سی دی Microsoft Visual Studio Tools for the Microsoft Office System که تو پک VS2005 من سی دی پنجم هست را نصب کن،بعد نصب یک سری پروژه به پروژه های جدید شما اضافه می شود که می توانید پروژه اکسل رو انتخاب کنید.
در این نوع پروژه ها می توانید به VB.Net2005 برنامه بنویسید.sample و تصویر نمونه در ضمیمه

aliila
پنج شنبه 08 شهریور 1386, 07:17 صبح
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oldCI As System.Globalization.CultureInfo = _
System.Threading.Thread.CurrentThread.CurrentCultu re
System.Threading.Thread.CurrentThread.CurrentCultu re = _
New System.Globalization.CultureInfo("en-US")
Dim excel_app As Excel.Application
Dim the_date As Date
Dim stop_date As Date
Dim r As Short
Dim new_chart As Excel.Chart
Dim new_book As Excel.Workbook
Dim active_sheet As Excel.Worksheet

' Create the Excel application.
excel_app = CreateObject("Excel.Application")

' Comment this line to hide Excel.
' excel_app.ActiveCell()
' Create a new spreadsheet.
new_book = excel_app.Workbooks.Add()

' Generate random values for the dates in January 2006.
active_sheet = new_book.Sheets(1)
the_date = CDate("2006/1/8")
stop_date = CDate("2006/3/5")
r = 1
Do While the_date < stop_date
'UPGRADE_WARNING: Couldn't resolve default property of object active_sheet.Cells(). Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"'
active_sheet.Cells._Default(r, 1) = the_date
'UPGRADE_WARNING: Couldn't resolve default property of object active_sheet.Cells(). Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"'
active_sheet.Cells._Default(r, 2) = Int(Rnd() * 90) + 10

the_date = DateAdd(Microsoft.VisualBasic.DateInterval.Day, 1, the_date)
r = r + 1
Loop
' Make a chart that uses the data.
new_chart = excel_app.Charts.Add()


With new_chart
.ChartType = Excel.XlChartType.xlLineMarkers
.SetSourceData(Source:=active_sheet.Range("A1:B" & VB6.Format(r - 1)), PlotBy:=Excel.XlRowCol.xlColumns)
.Location(Where:=Excel.XlChartLocation.xlLocationA sObject, Name:=active_sheet.Name)
End With

active_sheet.Shapes._Default(active_sheet.Shapes.C ount).Top = 10
active_sheet.Shapes._Default(active_sheet.Shapes.C ount).Left = 100
active_sheet.Shapes._Default(active_sheet.Shapes.C ount).Width = 600
active_sheet.Shapes._Default(active_sheet.Shapes.C ount).Height = 400

excel_app.ActiveChart.ChartArea.Select()
With excel_app.ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "February Values"
'UPGRADE_WARNING: Couldn't resolve default property of object ActiveChart.Axes().HasTitle. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"'
.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary).HasTitle = True
'UPGRADE_WARNING: Couldn't resolve default property of object ActiveChart.Axes().AxisTitle. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"'
.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary).AxisTitle.Characters. Text = "Date"
'UPGRADE_WARNING: Couldn't resolve default property of object ActiveChart.Axes().HasTitle. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"'
.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary).HasTitle = True
'UPGRADE_WARNING: Couldn't resolve default property of object ActiveChart.Axes().AxisTitle. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"'
.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary).AxisTitle.Characters. Text = "Value"
End With
excel_app.Visible = True
Dim new_sheet, old_sheet As Excel.Worksheet
Dim a, b As Object
excel_app = CreateObject("Excel.Application")
new_book = excel_app.Workbooks.Add()
new_sheet = new_book.ActiveSheet
new_sheet.Cells.Select()
new_sheet.Cells.ColumnWidth = 1
new_sheet.Cells.RowHeight = 10

End Sub

Afshin160
پنج شنبه 08 شهریور 1386, 21:43 عصر
سلام aliila
کد بالا error :
Name 'VB6' is not declared.
را میدهد ...
declared مربطش چیه...؟