C# Excel图表

Microsoft.Office.Interop.Excel.Range oResizeRange;
Microsoft.Office.Interop.Excel.Series oSeries;
//创建一个Excel对象(同时启动EXCEL.EXE进程)
Microsoft.Office.Interop.Excel.Application m_Excel = new Microsoft.Office.Interop.Excel.Application();
m_Excel.Visible = true;
m_Excel.SheetsInNewWorkbook = 1;//设置工作表的个数
Microsoft.Office.Interop.Excel._Workbook m_Book = (Microsoft.Office.Interop.Excel._Workbook)(m_Excel.Workbooks.Add(System.Reflection.Missing.Value));//添加新工作簿 Microsoft.Office.Interop.Excel._Worksheet m_Sheet;
m_Sheet = (Microsoft.Office.Interop.Excel._Worksheet)m_Excel.ActiveSheet;
m_Book.Charts.Add(System.Reflection.Missing.Value, System.Reflection.Missing.Value, 1, System.Reflection.Missing.Value);
m_Book.ActiveChart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlColumnClustered;//设置图形xlColumnClustered
int num=2;
m_Sheet.Cells[2, 1] = 100;
m_Sheet.Cells[2, 2] = 200;
m_Sheet.Cells[2, 3] = 180;
//设置数据取值范围
m_Book.ActiveChart.SetSourceData(m_Sheet.get_Range("A2", "C" + num.ToString()), Microsoft.Office.Interop.Excel.XlRowCol.xlColumns);
//m_Book.ActiveChart.Location(Excel.XlChartLocation.xlLocationAutomatic, title);
//以下是给图表放在指定位置
m_Book.ActiveChart.Location(Microsoft.Office.Interop.Excel.XlChartLocation.xlLocationAsObject, m_https://www.360docs.net/doc/457224098.html,);
oResizeRange = (Microsoft.Office.Interop.Excel.Range)m_Sheet.Rows.get_Item(10, System.Reflection.Missing.Value);
m_Sheet.Shapes.Item("Chart 1").Top = (float)(double)oResizeRange.Top;
//调图表的位置上边距
oResizeRange = (Microsoft.Office.Interop.Excel.Range)m_Sheet.Columns.get_Item(6, System.Reflection.Missing.Value);
//调图表的位置左边距 // m_Sheet.Shapes.Item("Chart 1").Left = (float)(double)oResizeRange.Left;
m_Sheet.Shapes.Item("Chart 1").Width = 400; //调图表的宽度
m_Sheet.Shapes.Item("Chart 1").Height = 250; //调图表的高度
m_Book.ActiveChart.PlotArea.Interior.ColorIndex = 19; //设置绘图区的背景色
m_Book.ActiveChart.PlotArea.Border.LineStyle = 2 Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone;//设置绘图区边框线条
m_Book.ActiveChart.PlotArea.Width = 400; //设置绘图区宽度
//m_Book.ActiveChart.ChartArea.Interior.ColorIndex = 10; //设置整个图表的背影颜色
//m_Book.ActiveChart.ChartArea.Border.ColorIndex = 8;// 设置整个图表的边框颜色
m_Book.ActiveChart.ChartArea.Border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone;//设置边框线条
m_Book.ActiveChart.HasDataTable = false; //设置Legend图例的位置和格式
m_Book.ActiveChart.Legend.Top = 20.00; //具体设置图例的上边距
m_Book.ActiveChart.Legend.Left = 60.00;//具体设置图例的左边距
m_Book.ActiveChart.Legend.Interior.ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexNone

; m_Book.ActiveChart.Legend.Width = 150; m_Book.ActiveChart.Legend.Font.Size = 9.5;
//m_Book.ActiveChart.Legend.Font.Bold = true; m_https://www.360docs.net/doc/457224098.html, = "宋体";
//m_Book.ActiveChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionTop;//设置图例的位置
m_Book.ActiveChart.Legend.Border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone;//设置图例边框线条
//设置X轴的显示
Microsoft.Office.Interop.Excel.Axis xAxis = (Microsoft.Office.Interop.Excel.Axis)m_Book.ActiveChart.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary);
xAxis.MajorGridlines.Border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlDot;
xAxis.MajorGridlines.Border.ColorIndex = 1;//gridLine横向线条的颜色
xAxis.HasTitle = false;
xAxis.MinimumScale = 1;
xAxis.MaximumScale = 200;
https://www.360docs.net/doc/457224098.html, = "宋体";
xAxis.TickLabels.Font.Size = 9;
//设置Y轴的显示
Microsoft.Office.Interop.Excel.Axis yAxis = (Microsoft.Office.Interop.Excel.Axis)m_Book.ActiveChart.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlCategory, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary);
yAxis.TickLabelSpacing = 30;
yAxis.TickLabels.NumberFormat = "M月D日";
yAxis.TickLabels.Orientation = Microsoft.Office.Interop.Excel.XlTickLabelOrientation.xlTickLabelOrientationHorizontal;//Y轴显示的方向,是水平还是垂直等
yAxis.TickLabels.Font.Size = 8;
https://www.360docs.net/doc/457224098.html, = "宋体";
//设置标题和坐标轴
m_Book.ActiveChart.HasTitle = true;
m_Book.ActiveChart.ChartTitle.Text = "图表标题";
m_Book.ActiveChart.Axes(Microsoft.Office.Interop.Excel.XlCategoryType xlCategory, Microsoft.Office.Interop.Excel.xlPrimary).HasTitle = true;
//m_Book.ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "横坐标";
//m_Book.ActiveChart.Axes(xlValue, xlPrimary).HasTitle = true;
//m_Book.ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "纵坐标";
/* //m_Book.ActiveChart.Floor.Interior.ColorIndex = 8;
/***以下是设置标题*****
m_Book.ActiveChart.HasTitle=true;
m_Book.ActiveChart.ChartTitle.Text = "净值指数";
m_Book.ActiveChart.ChartTitle.Shadow = true;
m_Book.ActiveChart.ChartTitle.Border.LineStyle = Excel.XlLineStyle.xlContinuous;
oSeries = (Microsoft.Office.Interop.Excel.Series)m_Book.ActiveChart.SeriesCollection(1);
oSeries.Border.ColorIndex = 45;
oSeries.Border.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;
oSeries = (Microsoft.Office.Interop.Excel.Series)m_Book.ActiveChart.SeriesCollection(2);
oSeries.Border.ColorIndex = 9;
oSeries.Border.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick; */





Microsoft.Office.Interop.Excel.Range oResizeRange; Microsoft.Office.Interop.Excel

.Series oSeries; //创建一个Excel对象(同时启动EXCEL.EXE进程) Microsoft.Office.Interop.Excel.Application m_Excel = new Microsoft.Office.Interop.Excel.Application(); m_Excel.Visible = true; m_Excel.SheetsInNewWorkbook = 1;//设置工作表的个数 Microsoft.Office.Interop.Excel._Workbook m_Book = (Microsoft.Office.Interop.Excel._Workbook)(m_Excel.Workbooks.Add(System.Reflection.Missing.Value));//添加新工作簿 Microsoft.Office.Interop.Excel._Worksheet m_Sheet; m_Sheet = (Microsoft.Office.Interop.Excel._Worksheet)m_Excel.ActiveSheet; m_Book.Charts.Add(System.Reflection.Missing.Value, System.Reflection.Missing.Value, 1, System.Reflection.Missing.Value); m_Book.ActiveChart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlColumnClustered;//设置图形xlColumnClustered int num=2;

相关文档
最新文档