UltraChart导出到Excel
来源:互联网 发布:手机淘宝5.2.7 编辑:程序博客网 时间:2024/06/03 09:41
- 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.
- 1: Public Class Export
- 2:
- 3: Private Sub ChartToExcel(ByVal chart As Infragistics.Win.UltraWinChart.UltraChart)
- 4: Try
- 5:
- 6: Dim xl As New Excel.Application
- 7: Dim wb As Excel.Workbook = xl.Workbooks.Add
- 8: Dim sheet As Excel.Worksheet = wb.Worksheets(1)
- 9: sheet.Visible = Excel.XlSheetVisibility.xlSheetVisible
- 10:
- 11:
- 12: sheet.Range("A1").Value = chart.TitleTop.Text
- 13: sheet.Range("A1").Font.Bold = True
- 14: sheet.Range("A1").Font.Size = 14
- 15:
- 16: Dim dt As DataTable = CType(chart.DataSource, DataSet).Tables("Data")
- 17:
- 18:
- 19: Dim intCol As Integer = 1
- 20: For Each col As DataColumn In dt.Columns
- 21: sheet.Cells(2, intCol) = col.ColumnName
- 22: intCol += 1
- 23: Next
- 24:
- 25:
- 26: sheet.Range("A2:" & Chr(64 + intCol) & "2").Font.Bold = True
- 27: sheet.Range("A2:" & Chr(64 + intCol) & "2").Font.Size = 12
- 28:
- 29:
- 30: Dim intRow As Integer = 3
- 31: For Each row As DataRow In dt.Rows
- 32: intCol = 1
- 33: For Each col As DataColumn In dt.Columns
- 34: sheet.Cells(intRow, intCol) = row.Item(col.ColumnName)
- 35: intCol += 1
- 36: Next
- 37: intRow += 1
- 38: Next
- 39:
- 40: wb.Application.Visible = True
- 41:
- 42: Dim oChart As Excel.Chart
- 43: Dim xlsAxisCategory, xlsAxisValue As Excel.Axes
- 44: Dim charts As Excel.ChartObjects = sheet.ChartObjects(Type.Missing)
- 45:
- 46:
- 47: Dim chartObj As Excel.ChartObject = charts.Add(0, 0, 400, 300)
- 48: oChart = chartObj.Chart
- 49:
- 50: Dim xlsSerie As Excel.SeriesCollection = oChart.SeriesCollection
- 51: oChart.ChartType = Excel.XlChartType.xlLine
- 52:
- 53:
- 54: Dim chartRange As Excel.Range = sheet.Range("A1", Chr((intCol - 1) + 64) & intRow)
- 55: oChart.SetSourceData(chartRange, Type.Missing)
- 56:
- 57: wb.Application.Visible = True
- 58:
- 59: Catch ex As Exception
- 60: Console.WriteLine("Doh!")
- 61: End Try
- 62:
- 63: End Sub
- 64: End Class