Not needing to have Microsoft Excel installed on the machine, The Spire.DataExport can create Excel spreadsheet. This sample demonstrates how to export data table into xls and create pie-3d chart.
private void button1_Click(object sender, EventArgs e) { System.Data.OleDb.OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); oleDbConnection1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb"; System.Data.OleDb.OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand(); oleDbCommand1.CommandText = "select * from parts"; oleDbCommand1.Connection = oleDbConnection1; System.Data.OleDb.OleDbCommand oleDbCommand2 = new System.Data.OleDb.OleDbCommand(); oleDbCommand2.CommandText = "select * from country"; oleDbCommand2.Connection = oleDbConnection1; Spire.DataExport.XLS.CellExport cellExport2 = new Spire.DataExport.XLS.CellExport(); Spire.DataExport.XLS.WorkSheet workSheet3 = new Spire.DataExport.XLS.WorkSheet(); Spire.DataExport.XLS.Chart chart1 = new Spire.DataExport.XLS.Chart(); Spire.DataExport.XLS.ChartSeries chartSeries1 = new Spire.DataExport.XLS.ChartSeries(); Spire.DataExport.XLS.StripStyle stripStyle5 = new Spire.DataExport.XLS.StripStyle(); Spire.DataExport.XLS.StripStyle stripStyle6 = new Spire.DataExport.XLS.StripStyle(); cellExport2.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView; cellExport2.AutoFitColWidth = true; cellExport2.DataFormats.CultureName = "zh-CN"; cellExport2.DataFormats.Currency = "¥#,###,##0.00"; cellExport2.DataFormats.DateTime = "yyyy-M-d H:mm"; cellExport2.DataFormats.Float = "#,###,##0.00"; cellExport2.DataFormats.Integer = "#,###,##0"; cellExport2.DataFormats.Time = "H:mm"; cellExport2.FileName = "chart1.xls"; cellExport2.SheetOptions.AggregateFormat.Font.Name = "Arial"; cellExport2.SheetOptions.CustomDataFormat.Font.Name = "Arial"; cellExport2.SheetOptions.DefaultFont.Name = "Arial"; cellExport2.SheetOptions.FooterFormat.Font.Name = "Arial"; cellExport2.SheetOptions.HeaderFormat.Font.Name = "Arial"; cellExport2.SheetOptions.HyperlinkFormat.Font.Color = Spire.DataExport.XLS.CellColor.Blue; cellExport2.SheetOptions.HyperlinkFormat.Font.Name = "Arial"; cellExport2.SheetOptions.HyperlinkFormat.Font.Underline = Spire.DataExport.XLS.XlsFontUnderline.Single; cellExport2.SheetOptions.NoteFormat.Alignment.Horizontal = Spire.DataExport.XLS.HorizontalAlignment.Left; cellExport2.SheetOptions.NoteFormat.Alignment.Vertical = Spire.DataExport.XLS.VerticalAlignment.Top; cellExport2.SheetOptions.NoteFormat.Font.Bold = true; cellExport2.SheetOptions.NoteFormat.Font.Name = "Tahoma"; cellExport2.SheetOptions.NoteFormat.Font.Size = 8F; cellExport2.SheetOptions.TitlesFormat.Font.Bold = true; cellExport2.SheetOptions.TitlesFormat.Font.Name = "Arial"; workSheet3.AutoFitColWidth = true; chart1.CategoryLabels.ColX = ((System.Byte)(1)); chart1.CategoryLabels.ColY = ((System.Byte)(1)); chart1.CategoryLabels.RowX = 1; chart1.CategoryLabels.RowY = 9; chart1.CategoryLabelsColumn = "Name"; chart1.DataRangeSheet = "charts"; chart1.Position.AutoPosition.Height = 12; chart1.Position.AutoPosition.Top = 1; chart1.Position.AutoPosition.Width = 6; chartSeries1.DataColumn = "Population"; chartSeries1.DataRangeSheet = "charts"; chartSeries1.Title = "Population"; chart1.Series.Add(chartSeries1); chart1.Style = Spire.DataExport.XLS.ChartStyle.Pie3d; chart1.Title = "Chart demo"; workSheet3.Charts.Add(chart1); workSheet3.FormatsExport.CultureName = "zh-CN"; workSheet3.FormatsExport.Currency = "¥#,###,##0.00"; workSheet3.FormatsExport.DateTime = "yyyy-M-d H:mm"; workSheet3.FormatsExport.Float = "#,###,##0.00"; workSheet3.FormatsExport.Integer = "#,###,##0"; workSheet3.FormatsExport.Time = "H:mm"; stripStyle5.Borders.Bottom.Style = Spire.DataExport.XLS.CellBorderStyle.Medium; stripStyle5.Borders.Left.Style = Spire.DataExport.XLS.CellBorderStyle.Medium; stripStyle5.Borders.Right.Style = Spire.DataExport.XLS.CellBorderStyle.Medium; stripStyle5.Borders.Top.Style = Spire.DataExport.XLS.CellBorderStyle.Medium; stripStyle5.FillStyle.Background = Spire.DataExport.XLS.CellColor.LightGreen; stripStyle5.FillStyle.Pattern = Spire.DataExport.XLS.Pattern.Solid; stripStyle5.Font.Name = "Arial"; stripStyle6.Borders.Bottom.Style = Spire.DataExport.XLS.CellBorderStyle.Medium; stripStyle6.Borders.Left.Style = Spire.DataExport.XLS.CellBorderStyle.Medium; stripStyle6.Borders.Right.Style = Spire.DataExport.XLS.CellBorderStyle.Medium; stripStyle6.Borders.Top.Style = Spire.DataExport.XLS.CellBorderStyle.Medium; stripStyle6.FillStyle.Background = Spire.DataExport.XLS.CellColor.LightTurquoise; stripStyle6.FillStyle.Pattern = Spire.DataExport.XLS.Pattern.Solid; stripStyle6.Font.Name = "Arial"; workSheet3.ItemStyles.Add(stripStyle5); workSheet3.ItemStyles.Add(stripStyle6); workSheet3.ItemType = Spire.DataExport.XLS.CellItemType.Col; workSheet3.Options.AggregateFormat.Font.Name = "Arial"; workSheet3.Options.CustomDataFormat.Font.Name = "Arial"; workSheet3.Options.DefaultFont.Name = "Arial"; workSheet3.Options.FooterFormat.Font.Name = "Arial"; workSheet3.Options.HeaderFormat.Font.Bold = true; workSheet3.Options.HeaderFormat.Font.Color = Spire.DataExport.XLS.CellColor.Blue; workSheet3.Options.HeaderFormat.Font.Name = "Arial"; workSheet3.Options.HyperlinkFormat.Font.Color = Spire.DataExport.XLS.CellColor.Blue; workSheet3.Options.HyperlinkFormat.Font.Name = "Arial"; workSheet3.Options.HyperlinkFormat.Font.Underline = Spire.DataExport.XLS.XlsFontUnderline.Single; workSheet3.Options.NoteFormat.Alignment.Horizontal = Spire.DataExport.XLS.HorizontalAlignment.Left; workSheet3.Options.NoteFormat.Alignment.Vertical = Spire.DataExport.XLS.VerticalAlignment.Top; workSheet3.Options.NoteFormat.Font.Bold = true; workSheet3.Options.NoteFormat.Font.Name = "Tahoma"; workSheet3.Options.NoteFormat.Font.Size = 8F; workSheet3.Options.TitlesFormat.Borders.Bottom.Style = Spire.DataExport.XLS.CellBorderStyle.Medium; workSheet3.Options.TitlesFormat.Borders.Left.Style = Spire.DataExport.XLS.CellBorderStyle.Medium; workSheet3.Options.TitlesFormat.Borders.Right.Style = Spire.DataExport.XLS.CellBorderStyle.Medium; workSheet3.Options.TitlesFormat.Borders.Top.Style = Spire.DataExport.XLS.CellBorderStyle.Medium; workSheet3.Options.TitlesFormat.FillStyle.Background = Spire.DataExport.XLS.CellColor.LightYellow; workSheet3.Options.TitlesFormat.FillStyle.Pattern = Spire.DataExport.XLS.Pattern.Solid; workSheet3.Options.TitlesFormat.Font.Bold = true; workSheet3.Options.TitlesFormat.Font.Name = "Arial"; workSheet3.SheetName = "charts"; workSheet3.SQLCommand = oleDbCommand2; workSheet3.StartDataCol = ((System.Byte)(0)); cellExport2.Sheets.Add(workSheet3); cellExport2.SQLCommand = oleDbCommand1; oleDbConnection1.Open(); try { cellExport2.SaveToFile(); } finally { oleDbConnection1.Close(); } }
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim oleDbConnection1 As New System.Data.OleDb.OleDbConnection() oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb" Dim oleDbCommand1 As New System.Data.OleDb.OleDbCommand() oleDbCommand1.CommandText = "select * from parts" oleDbCommand1.Connection = oleDbConnection1 Dim oleDbCommand2 As New System.Data.OleDb.OleDbCommand() oleDbCommand2.CommandText = "select * from country" oleDbCommand2.Connection = oleDbConnection1 Dim cellExport2 As New Spire.DataExport.XLS.CellExport() Dim workSheet3 As New Spire.DataExport.XLS.WorkSheet() Dim chart1 As New Spire.DataExport.XLS.Chart() Dim chartSeries1 As New Spire.DataExport.XLS.ChartSeries() Dim stripStyle5 As New Spire.DataExport.XLS.StripStyle() Dim stripStyle6 As New Spire.DataExport.XLS.StripStyle() cellExport2.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView cellExport2.AutoFitColWidth = True cellExport2.DataFormats.CultureName = "zh-CN" cellExport2.DataFormats.Currency = "¥#,###,##0.00" cellExport2.DataFormats.DateTime = "yyyy-M-d H:mm" cellExport2.DataFormats.Float = "#,###,##0.00" cellExport2.DataFormats.[Integer] = "#,###,##0" cellExport2.DataFormats.Time = "H:mm" cellExport2.FileName = "chart1.xls" cellExport2.SheetOptions.AggregateFormat.Font.Name = "Arial" cellExport2.SheetOptions.CustomDataFormat.Font.Name = "Arial" cellExport2.SheetOptions.DefaultFont.Name = "Arial" cellExport2.SheetOptions.FooterFormat.Font.Name = "Arial" cellExport2.SheetOptions.HeaderFormat.Font.Name = "Arial" cellExport2.SheetOptions.HyperlinkFormat.Font.Color = Spire.DataExport.XLS.CellColor.Blue cellExport2.SheetOptions.HyperlinkFormat.Font.Name = "Arial" cellExport2.SheetOptions.HyperlinkFormat.Font.Underline = Spire.DataExport.XLS.XlsFontUnderline.[Single] cellExport2.SheetOptions.NoteFormat.Alignment.Horizontal = Spire.DataExport.XLS.HorizontalAlignment.Left cellExport2.SheetOptions.NoteFormat.Alignment.Vertical = Spire.DataExport.XLS.VerticalAlignment.Top cellExport2.SheetOptions.NoteFormat.Font.Bold = True cellExport2.SheetOptions.NoteFormat.Font.Name = "Tahoma" cellExport2.SheetOptions.NoteFormat.Font.Size = 8.0F cellExport2.SheetOptions.TitlesFormat.Font.Bold = True cellExport2.SheetOptions.TitlesFormat.Font.Name = "Arial" workSheet3.AutoFitColWidth = True chart1.CategoryLabels.ColX = CByte(1) chart1.CategoryLabels.ColY = CByte(1) chart1.CategoryLabels.RowX = 1 chart1.CategoryLabels.RowY = 9 chart1.CategoryLabelsColumn = "Name" chart1.DataRangeSheet = "charts" chart1.Position.AutoPosition.Height = 12 chart1.Position.AutoPosition.Top = 1 chart1.Position.AutoPosition.Width = 6 chartSeries1.DataColumn = "Population" chartSeries1.DataRangeSheet = "charts" chartSeries1.Title = "Population" chart1.Series.Add(chartSeries1) chart1.Style = Spire.DataExport.XLS.ChartStyle.Pie3d chart1.Title = "Chart demo" workSheet3.Charts.Add(chart1) workSheet3.FormatsExport.CultureName = "zh-CN" workSheet3.FormatsExport.Currency = "¥#,###,##0.00" workSheet3.FormatsExport.DateTime = "yyyy-M-d H:mm" workSheet3.FormatsExport.Float = "#,###,##0.00" workSheet3.FormatsExport.[Integer] = "#,###,##0" workSheet3.FormatsExport.Time = "H:mm" stripStyle5.Borders.Bottom.Style = Spire.DataExport.XLS.CellBorderStyle.Medium stripStyle5.Borders.Left.Style = Spire.DataExport.XLS.CellBorderStyle.Medium stripStyle5.Borders.Right.Style = Spire.DataExport.XLS.CellBorderStyle.Medium stripStyle5.Borders.Top.Style = Spire.DataExport.XLS.CellBorderStyle.Medium stripStyle5.FillStyle.Background = Spire.DataExport.XLS.CellColor.LightGreen stripStyle5.FillStyle.Pattern = Spire.DataExport.XLS.Pattern.Solid stripStyle5.Font.Name = "Arial" stripStyle6.Borders.Bottom.Style = Spire.DataExport.XLS.CellBorderStyle.Medium stripStyle6.Borders.Left.Style = Spire.DataExport.XLS.CellBorderStyle.Medium stripStyle6.Borders.Right.Style = Spire.DataExport.XLS.CellBorderStyle.Medium stripStyle6.Borders.Top.Style = Spire.DataExport.XLS.CellBorderStyle.Medium stripStyle6.FillStyle.Background = Spire.DataExport.XLS.CellColor.LightTurquoise stripStyle6.FillStyle.Pattern = Spire.DataExport.XLS.Pattern.Solid stripStyle6.Font.Name = "Arial" workSheet3.ItemStyles.Add(stripStyle5) workSheet3.ItemStyles.Add(stripStyle6) workSheet3.ItemType = Spire.DataExport.XLS.CellItemType.Col workSheet3.Options.AggregateFormat.Font.Name = "Arial" workSheet3.Options.CustomDataFormat.Font.Name = "Arial" workSheet3.Options.DefaultFont.Name = "Arial" workSheet3.Options.FooterFormat.Font.Name = "Arial" workSheet3.Options.HeaderFormat.Font.Bold = True workSheet3.Options.HeaderFormat.Font.Color = Spire.DataExport.XLS.CellColor.Blue workSheet3.Options.HeaderFormat.Font.Name = "Arial" workSheet3.Options.HyperlinkFormat.Font.Color = Spire.DataExport.XLS.CellColor.Blue workSheet3.Options.HyperlinkFormat.Font.Name = "Arial" workSheet3.Options.HyperlinkFormat.Font.Underline = Spire.DataExport.XLS.XlsFontUnderline.[Single] workSheet3.Options.NoteFormat.Alignment.Horizontal = Spire.DataExport.XLS.HorizontalAlignment.Left workSheet3.Options.NoteFormat.Alignment.Vertical = Spire.DataExport.XLS.VerticalAlignment.Top workSheet3.Options.NoteFormat.Font.Bold = True workSheet3.Options.NoteFormat.Font.Name = "Tahoma" workSheet3.Options.NoteFormat.Font.Size = 8.0F workSheet3.Options.TitlesFormat.Borders.Bottom.Style = Spire.DataExport.XLS.CellBorderStyle.Medium workSheet3.Options.TitlesFormat.Borders.Left.Style = Spire.DataExport.XLS.CellBorderStyle.Medium workSheet3.Options.TitlesFormat.Borders.Right.Style = Spire.DataExport.XLS.CellBorderStyle.Medium workSheet3.Options.TitlesFormat.Borders.Top.Style = Spire.DataExport.XLS.CellBorderStyle.Medium workSheet3.Options.TitlesFormat.FillStyle.Background = Spire.DataExport.XLS.CellColor.LightYellow workSheet3.Options.TitlesFormat.FillStyle.Pattern = Spire.DataExport.XLS.Pattern.Solid workSheet3.Options.TitlesFormat.Font.Bold = True workSheet3.Options.TitlesFormat.Font.Name = "Arial" workSheet3.SheetName = "charts" workSheet3.SQLCommand = oleDbCommand2 workSheet3.StartDataCol = CByte(0) cellExport2.Sheets.Add(workSheet3) cellExport2.SQLCommand = oleDbCommand1 oleDbConnection1.Open() Try cellExport2.SaveToFile() Finally oleDbConnection1.Close() End Try End Sub