home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_AutoExcel_vb________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2001-03-02  |  3.8 KB  |  131 lines

  1. option strict off
  2. imports System
  3. imports System.Reflection ' For Missing.Value and BindingFlags
  4. imports System.Runtime.InteropServices ' For COMException
  5. imports Excel
  6.  
  7. class AutoExcel
  8.     public shared sub Main()    
  9.     
  10.         Console.WriteLine ("Creating new Excel.Application")
  11.         dim app as Application 
  12.         app = new Application()
  13.         if app is nothing then
  14.             Console.WriteLine("ERROR: EXCEL couldn't be started!")
  15.             environment.exitcode = 0
  16.             exit sub
  17.         end if
  18.         
  19.         Console.WriteLine ("Making application visible")
  20.         app.Visible = true
  21.         
  22.         Console.WriteLine ("Getting the workbooks collection")
  23.         dim workbooks as Workbooks
  24.         workbooks = app.Workbooks
  25.  
  26.         Console.WriteLine ("Adding a new workbook")
  27.         ' The following line is the temporary workaround for the LCID problem
  28.         dim workbook as _Workbook 
  29.         workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet)
  30.  
  31.         Console.WriteLine ("Getting the worksheets collection")
  32.         dim sheets as Sheets 
  33.         sheets = workbook.Worksheets
  34.  
  35.         dim worksheet as _Worksheet 
  36.         worksheet = sheets.Item(1)
  37.         if worksheet is nothing then
  38.             Console.WriteLine ("ERROR: worksheet == null")
  39.         end if
  40.         
  41.         Console.WriteLine ("Setting the value for cell")
  42.         dim range as Object 'Range 
  43.         range = worksheet.Range("A10", Missing.Value)
  44.         if range is nothing then
  45.             Console.WriteLine ("ERROR: range == null")
  46.         end if
  47.         range.Value2 = 5
  48.         
  49.         ' This paragraph sends single dimension array to Excel
  50.         Dim range2 as Object
  51.         range2 = worksheet.Range("A1", "E1")
  52.         Dim array2(5) as Integer
  53.         Dim i As Integer
  54.         For i = 0 To 4
  55.             array2(i) = i+1
  56.         Next i
  57.         range2.Value2 = array2
  58.         
  59.         ' This paragraph sends two dimension array to Excel
  60.         Dim range3 as Object
  61.         range3 = worksheet.Range("A2", "E3")
  62.         Dim array3(2, 5) as Integer
  63.         Dim j As Integer
  64.         For i = 0 To 1
  65.             For j = 0 To 4
  66.                 array3(i, j) = i*10+j
  67.             Next j
  68.         Next i
  69.         range3.Value2 = array3
  70.         
  71.         ' This paragraph reads two dimension array from Excel
  72.         Dim range4 as Object 
  73.         range4 = worksheet.Range("A2", "E3")
  74.         Dim array4(,) as Object
  75.         array4 = range4.Value2
  76.         
  77.         For i = array4.GetLowerBound(0) To array4.GetUpperBound(0)
  78.             For j = array4.GetLowerBound(1) To array4.GetUpperBound(1)
  79.                 if CDbl(array4(i, j)) <> array3(i-1, j-1) then 
  80.                 environment.exitcode = 0
  81.                 Console.WriteLine ("ERROR: Comparison FAILED!")
  82.                 exit sub
  83.                 end if
  84.             Next j
  85.         Next i
  86.         
  87.         ' This paragraph sends two dimension array to Excel
  88.         Dim range5 as Object 
  89.         range5 = worksheet.Range("A5", "J6")
  90.         Dim array5(2, 10) as Double
  91.         For j = 0 To array5.GetUpperBound(1)
  92.             Dim arg as Double
  93.             arg = Math.PI/array5.GetLength(1) * j
  94.             array5(0, j) = Math.Sin(arg)
  95.             array5(1, j) = Math.Cos(arg)
  96.         Next j
  97.         range5.Value2 = array5
  98.  
  99.         ' The following code draws the chart
  100.         
  101.         range5.Select
  102.         
  103.         Dim chartobjects as Object
  104.         chartobjects = worksheet.ChartObjects(Missing.Value)
  105.  
  106.         Dim chartobject as Object
  107.         chartobject = chartobjects.Add(10, 100, 450, 250)
  108.         
  109.         Dim chart as Object
  110.         chart = chartobject.Chart
  111.         
  112.         chart.ChartWizard(range5, XlChartType.xl3DColumn, Missing.Value, XlRowCol.xlRows, 0, 0, true, "Sample Chart", _ 
  113.         "Sample Category Type", "Sample Value Type")
  114.  
  115.         Console.WriteLine ("Press ENTER to finish the sample:")
  116.         Console.ReadLine()
  117.         
  118.         Try
  119.             ' If user interacted with Excel it will not close when the app object is destroyed, so we close it explicitely
  120.             workbook.Saved = true
  121.             app.UserControl = false
  122.             app.Quit()
  123.         Catch Outer As COMException    
  124.             Console.WriteLine ("User closed Excel manually, so we don't have to do that")
  125.         End Try
  126.  
  127.         environment.exitcode = 100
  128.         Console.WriteLine ("Sample successfully finished!")
  129. end sub
  130. end class
  131.