UltraChart导出到Excel

来源:互联网 发布:手机淘宝5.2.7 编辑:程序博客网 时间:2024/06/03 09:41
  1. Here is a simple example of exporting the UltraChart to an Excelspreadsheet.  It uses Office Interop so you need to include a referenceto the Microsoft Office 11 Object Library in your project.
  2.    1:  Public Class Export
  3.    2:  
  4.    3:      Private Sub ChartToExcel(ByVal chart As Infragistics.Win.UltraWinChart.UltraChart)
  5.    4:          Try
  6.    5:   
  7.    6:              Dim xl As New Excel.Application
  8.    7:              Dim wb As Excel.Workbook = xl.Workbooks.Add
  9.    8:              Dim sheet As Excel.Worksheet = wb.Worksheets(1)
  10.    9:              sheet.Visible = Excel.XlSheetVisibility.xlSheetVisible
  11.   10:   
  12.   11:              'add the top title to the sheet as the header
  13.   12:              sheet.Range("A1").Value = chart.TitleTop.Text
  14.   13:              sheet.Range("A1").Font.Bold = True
  15.   14:              sheet.Range("A1").Font.Size = 14
  16.   15:   
  17.   16:              Dim dt As DataTable = CType(chart.DataSource, DataSet).Tables("Data")
  18.   17:   
  19.   18:              'output the headers.
  20.   19:              Dim intCol As Integer = 1
  21.   20:              For Each col As DataColumn In dt.Columns
  22.   21:                  sheet.Cells(2, intCol) = col.ColumnName
  23.   22:                  intCol += 1
  24.   23:              Next
  25.   24:   
  26.   25:              'Bold the headers.
  27.   26:              sheet.Range("A2:" & Chr(64 + intCol) & "2").Font.Bold = True
  28.   27:              sheet.Range("A2:" & Chr(64 + intCol) & "2").Font.Size = 12
  29.   28:   
  30.   29:              'start our data on row 3 of the worksheet.
  31.   30:              Dim intRow As Integer = 3
  32.   31:              For Each row As DataRow In dt.Rows
  33.   32:                  intCol = 1
  34.   33:                  For Each col As DataColumn In dt.Columns
  35.   34:                      sheet.Cells(intRow, intCol) = row.Item(col.ColumnName)
  36.   35:                      intCol += 1
  37.   36:                  Next
  38.   37:                  intRow += 1
  39.   38:              Next
  40.   39:   
  41.   40:              wb.Application.Visible = True
  42.   41:   
  43.   42:              Dim oChart As Excel.Chart
  44.   43:              Dim xlsAxisCategory, xlsAxisValue As Excel.Axes
  45.   44:              Dim charts As Excel.ChartObjects = sheet.ChartObjects(Type.Missing)
  46.   45:   
  47.   46:              ' Adds a chart at x = 0, y = 0, 500 points wide and 300 tall.
  48.   47:              Dim chartObj As Excel.ChartObject = charts.Add(0, 0, 400, 300)
  49.   48:              oChart = chartObj.Chart
  50.   49:   
  51.   50:              Dim xlsSerie As Excel.SeriesCollection = oChart.SeriesCollection
  52.   51:              oChart.ChartType = Excel.XlChartType.xlLine
  53.   52:   
  54.   53:              'add 64 to intCol since ASCII A is 65 and then take the Chr() of it.BR>
  55.   54:              Dim chartRange As Excel.Range = sheet.Range("A1", Chr((intCol - 1) + 64) & intRow)
  56.   55:              oChart.SetSourceData(chartRange, Type.Missing)
  57.   56:   
  58.   57:              wb.Application.Visible = True
  59.   58:   
  60.   59:          Catch ex As Exception
  61.   60:              Console.WriteLine("Doh!")
  62.   61:          End Try
  63.   62:   
  64.   63:      End Sub
  65.   64:  End Class
原创粉丝点击