乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 请问如何把C#当中的一系列<em>chart</em>类图表输出到<em>Excel

请问如何把C#当中的一系列<em>chart</em>类图表输出到<em>Excel

作者:乔山办公网日期:

返回目录:excel表格制作


下载并引用spire.xls.dll, 使用下面的代码生成柱形图

using Spire.Xls;

namespace ExcelColumnChart
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建e68a847a64332Workbook对象
            Workbook workbook = new Workbook();

            //获取工作表
            Worksheet sheet = workbook.Worksheets[0];

            //写入数据
            sheet.Range["A1"].Value = "部门名称";
            sheet.Range["A2"].Value = "开发部";
            sheet.Range["A3"].Value = "测试部";
            sheet.Range["A4"].Value = "销售部";
            sheet.Range["A5"].Value = "技术支持部";
            sheet.Range["B1"].Value = "部门人数";
            sheet.Range["B2"].NumberValue = 121;
            sheet.Range["B3"].NumberValue = 43;
            sheet.Range["B4"].NumberValue = 35;
            sheet.Range["B5"].NumberValue = 67;

            //创建柱状图
            Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered);

            //指定用于生成图表的数据区域
            chart.DataRange = sheet.Range["A1:B5"];
            chart.SeriesDataFromRange = false;

            //指定图表所在的位置
            chart.LeftColumn = 3;
            chart.TopRow = 1;
            chart.RightColumn = 10;
            chart.BottomRow = 19;

            //设置图表的名称以及x、y轴的名称
            chart.ChartTitle = "部门信息";
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Size = 12;

            chart.PrimaryCategoryAxis.Title = "部门";
            chart.PrimaryCategoryAxis.Font.IsBold = true;
            chart.PrimaryCategoryAxis.TitleArea.IsBold = true;

            chart.PrimaryValueAxis.Title = "人数";
            chart.PrimaryValueAxis.HasMajorGridLines = false;
            chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;
            chart.PrimaryValueAxis.MinValue = 0;
            chart.PrimaryValueAxis.TitleArea.IsBold = true;

            //设置图例的位置
            chart.Legend.Position = LegendPositionType.Right;

            //不填充plot area
            chart.PlotArea.Fill.FillType = ShapeFillType.NoFill;

            //保存工作薄
            workbook.SaveToFile("output.xlsx", ExcelVersion.Version2013);
        }
    }
}

结果:



具体怎么做,要看你的图形类型了。
1、条形图。如果你的图已经做好了,则在图表的空白区域,右键-》图表类型-》在自定义类型里,选择最后的一个“自然条形图”,就可以把竖着的变成横向的。
2、其他。在图表的空白区域,右键-》源数据-》切换到“系列”标签页,选择需要变动的系列,然后把X、Y值的数据区域互换(通过剪切粘贴等操作)-》确定。

在菜单栏中插入(insert)-项目(objective)中进入,选择从文件中创建(creat from file),选择浏览(browse),找到需要添加的word文档,然后点击右边的显示图标(display as icon).就可以了.
补充:要调整成你的要求,好像不可以,要么调整图标的大小(可以调整的),要么调整excel单元格的大小.不过都不是很好.

// <summary>
        /// 创建Chart,并设置相关属性
        /// 最后按照固定路径输出成gif图e799bee5baa6e78988e69d83332
        /// </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"));//一片黑,貌似不能用
            //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);

        }

相关阅读

  • <em>xy</em> <em>chart</em> labeler 怎

  • 乔山办公网excel表格制作
  • 选项里面加载宏~进labeler安装项选".xlam"的文件~XYChartLabeler如何卸载" src="/uploads/tu/810.jpg" style="width: 400px; height: 267px;" />直接到xy chart labeler 安装目录中,找到XYChar
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网