作者:乔山办公网日期:
返回目录:excel表格制作
// <summary>
/// 创建Chart,并设置相关属性
/// 最后按照固定路径输出成gif图
/// </summary>
/// <param name="saveDocPath">保存图片路径</param>
public void CreateChart(string saveDocPath)
{
Excel.Series oSeries;
Excel.Chart oChart;
oWB = (Excel.Workbook)sheet.Parent;
oChart = (Excel.Chart)oWB.Charts.Add(Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
//应用的数据"C39:C53"
oChart.SetSourceData((Excel.Range)sheet.get_Range("C39:C53", Missing.Value), Missing.Value);
oChart.ChartArea.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;//设置整个图表的边框线条
oChart.ChartArea.Border.Color = ColorTranslator.ToOle(Color.Black);// 设置整个图表的边框颜色
oChart.ChartArea.Interior.Color = ColorTranslator.ToOle(Color.White);// 设置整个图表的内部颜色
oChart.PlotArea.Interior.Color = ColorTranslator.ToOle(Color.LightCyan); //设置绘图区的背景
oChart.PlotArea.Width = 630;
oChart.PlotArea.Height = 420;
oChart.PlotArea.Top = 60;
oChart.PlotArea.Left = 15;
//oChart.PlotArea.Interior.Color = ColorTranslator.ToOle(Color.FromName("#b2d235"));//一片黑,e799bee5baa6e79fa5e98193e78988e69d83332貌似不能用
//oChart.PlotArea.Interior.Color = ColorTranslator.ToOle(Color.FromArgb(64, 224, 208));
oChart.HasLegend = false;//没有图例
oChart.HasTitle = true;//设置标题
oChart.ChartTitle.Text = DateTime.Now.Year.ToString()+"年"+DateTime.Now.Month.ToString()+"月"+DateTime.Now.Day.ToString()+"日北京市**区降雨量柱状图";
oChart.ChartTitle.Font.Name = "黑体";
//oChart.ChartTitle.Font.Bold = true;
oChart.ChartTitle.Font.Size = 23;
//oChart.ChartTitle.Border.LineStyle = Excel.XlLineStyle.xlDouble;//标题的线框样式
//oChart.ChartTitle.Shadow = true;
////Use the ChartWizard to create a new chart from the selected data.
//Rng = sheet.get_Range("C39:C53", Missing.Value);
//oChart.ChartWizard(oResizeRng, Excel.XlChartType.xl3DColumn, Missing.Value,
// Excel.XlRowCol.xlColumns, Missing.Value, Missing.Value, Missing.Value,
// Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//oChart.Walls.Border.Color = ColorTranslator.ToOle(Color.Green);//walls只是在3D时使用
//oChart.Walls.Interior.Color = ColorTranslator.ToOle(Color.Blue);
//oChart.WallsAndGridlines2D = true;
//设置圆柱样式
oSeries = (Excel.Series)oChart.SeriesCollection(1);
oSeries.XValues = sheet.get_Range("B39", "B53");//x轴街道名称
oSeries.Border.Color = ColorTranslator.ToOle(Color.Black);//圆柱的边框
oSeries.Border.Weight = Excel.XlBorderWeight.xlThin;//圆柱边框线宽
oSeries.Interior.Color = ColorTranslator.ToOle(Color.Lime);//圆柱的内部颜色
oSeries.HasDataLabels = true;//圆柱有数据标签
//oChart.Location(Excel.XlChartLocation.xlLocationAsObject, sheet.Name);
//Excel.ChartGroup grp = (Excel.ChartGroup)oChart.ChartGroups(1);
//grp.GapWidth = 20;//柱形不同的颜色
//grp.VaryByCategories = true;
//设置Y轴的显示
Excel.Axis yAxis = (Excel.Axis)oWB.ActiveChart.Axes(Excel.XlAxisType.xlValue,Excel.XlAxisGroup.xlPrimary);
yAxis.MajorGridlines.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone;
yAxis.MajorGridlines.Border.Color = ColorTranslator.ToOle(Color.Aqua);//gridLine横向线条的颜色
yAxis.MajorGridlines.Border.Weight = Excel.XlBorderWeight.xlThin;
yAxis.Border.Color = ColorTranslator.ToOle(Color.Black);//y轴颜色
yAxis.Border.Weight = Excel.XlBorderWeight.xlThin;
yAxis.HasTitle = true;//y轴有标题
yAxis.AxisTitle.Text = "单位:mm";
yAxis.AxisTitle.Font.Name = "宋体";
yAxis.AxisTitle.Font.Size = 17;
//yAxis.AxisTitle.Font.Bold = true;
yAxis.AxisTitle.Top = 35;
yAxis.AxisTitle.Orientation = 0;//角度
//yAxis.MinimumScale = 1;
//yAxis.MaximumScale = 200;
yAxis.TickLabels.Font.Name = "宋体";//y轴数字样式
yAxis.TickLabels.Font.Size = 14;
yAxis.TickLabels.Font.Bold = true;
//设置X轴的显示
Excel.Axis xAxis = (Excel.Axis)oWB.ActiveChart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
xAxis.Border.Color = ColorTranslator.ToOle(Color.Black);//x轴颜色
xAxis.Border.Weight = Excel.XlBorderWeight.xlThin;
//yAxis.TickLabelSpacing = 30;
//xAxis.TickLabels.Orientation = Excel.XlTickLabelOrientation.xlTickLabelOrientationHorizontal;//Y轴显示的方向,是水平
xAxis.TickLabels.Orientation = (Excel.XlTickLabelOrientation)45;
xAxis.TickLabels.Font.Size = 15;
xAxis.TickLabels.Font.Name = "楷体_GB2312";
xAxis.TickLabels.Font.Bold =false;
//chart另存图片
string chartName = "chart" + DateTime.Now.ToString("yyyyMMdd");
oChart.Export(saveDocPath+"\\"+chartName+".gif", "GIF", false);
}
chart是图表的意思,在Excel中选择插入——图表就可以了
用数据透视表就可以,不管按日、按周、按月
我想了一个也不是很好的办法,不过倒是可以解决你的问题。你应该有两列数据,百一列是要测试的数据,另一列是这些数据的编号(或其他的用以区分这些数据的标志)。选中这两列->插入图表->XY散点图->完成,这时就做了这些数据的散点图,X轴为数度据编号,Y轴为数据坐标。选中Y坐标,右键->坐标轴问选项,主要刻度单位为(上限值 — 下限值),最小值为(下限值 — 主要刻度单位,这是为了显示低于下答限值的测试数据),最大值只要大于上限值就行,还要大于测试数内据的最大值(这样才能显示大于上限值的测试数据)。这样就有了两条平行于X轴的横线,在两条平行线之间的数据才是合格的。你还可以在图表中显示各数据点的坐标值,以方便比较,方法为:在图表中选中数据点,右键->数据系列格式->数据标志,勾选“容X值”、“Y值”。