C# Excel 也图添加模拟运算表

  Excel中之图能够用数据可视化,方便我们比分析数据。但为生一定之受制,例如:不克直接由图纸中读来本数据的准确值。Excel提供的化解方案是,在图片下方添加一个模拟运算表,即以坐标轴下方添加含建表时所按照的数量的报表。本文旨在介绍通过免费的Free
Spire.XLS在C#独立创建Excel文件,添加数码,生成图表,并累加模拟运算表。

急需用的命名空间:

using Spire.Xls;
using System.Drawing;

 

步骤详解:

 

步骤1:创建Excel文件,添加表单。

            Workbook workbook = new Workbook();
            workbook.CreateEmptySheets(1);
            Worksheet sheet = workbook.Worksheets[0];

 

步骤二:为单元格添加样本数,用于一下非生成图表。

 

            sheet.Name = “Demo”;
            sheet.Range[“A1”].Value = “月份”;
            sheet.Range[“A2”].Value = “一月”;
            sheet.Range[“A3”].Value = “二月”;
            sheet.Range[“A4”].Value = “三月”;
            sheet.Range[“A5”].Value = “四月”;
            sheet.Range[“A6”].Value = “五月”;
            sheet.Range[“A7”].Value = “六月”;
            sheet.Range[“B1”].Value = “小王”;
            sheet.Range[“B2”].NumberValue = 3.3;
            sheet.Range[“B3”].NumberValue = 2.5;
            sheet.Range[“B4”].NumberValue = 2.0;
            sheet.Range[“B5”].NumberValue = 3.7;
            sheet.Range[“B6”].NumberValue = 4.5;
            sheet.Range[“B7”].NumberValue = 4.0;
            sheet.Range[“C1”].Value = “小新”;
            sheet.Range[“C2”].NumberValue = 3.8;
            sheet.Range[“C3”].NumberValue = 3.2;
            sheet.Range[“C4”].NumberValue = 1.7;
            sheet.Range[“C5”].NumberValue = 3.5;
            sheet.Range[“C6”].NumberValue = 4.5;
            sheet.Range[“C7”].NumberValue = 4.3;
            sheet.Range[“D1”].Value = “小白”;
            sheet.Range[“D2”].NumberValue = 3.0;
            sheet.Range[“D3”].NumberValue = 2.8;
            sheet.Range[“D4”].NumberValue = 3.5;
            sheet.Range[“D5”].NumberValue = 2.3;
            sheet.Range[“D6”].NumberValue = 3.3;
            sheet.Range[“D7”].NumberValue = 3.8;

手续三:利用步骤二填充的数额,生成柱形图,设置其于Excel中之岗位,图表名称等。

           
Chart chart = sheet.Charts.Add(ExcelChartType.Column3DClustered);
            chart.DataRange = sheet.Range[“B1:D7”];
            chart.SeriesDataFromRange = false;
            chart.TopRow = 7;
            chart.BottomRow = 28;
            chart.LeftColumn = 3;
            chart.RightColumn =11;
            chart.ChartTitle = “带模拟数据表的柱状图示例”;
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Size = 12;
            Spire.Xls.Charts.ChartSerie cs1 = chart.Series[0];      
            cs1.CategoryLabels = sheet.Range[“A2:A7”];

 

手续四:为柱状图上加模拟数据表。

           chart.HasDataTable = true;

 

手续五:保存文档为2010版Excel。

           workbook.SaveToFile(“S3.xlsx”, ExcelVersion.Version2010);

 

代码运行后效果截图:

C# 1

 

 

 完整代码:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Xls;
using System.Drawing;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.CreateEmptySheets(1);
            Worksheet sheet = workbook.Worksheets[0];

            sheet.Name = “Demo”;
            sheet.Range[“A1”].Value = “月份”;
            sheet.Range[“A2”].Value = “一月”;
            sheet.Range[“A3”].Value = “二月”;
            sheet.Range[“A4”].Value = “三月”;
            sheet.Range[“A5”].Value = “四月”;
            sheet.Range[“A6”].Value = “五月”;
            sheet.Range[“A7”].Value = “六月”;
            sheet.Range[“B1”].Value = “小王”;
            sheet.Range[“B2”].NumberValue = 3.3;
            sheet.Range[“B3”].NumberValue = 2.5;
            sheet.Range[“B4”].NumberValue = 2.0;
            sheet.Range[“B5”].NumberValue = 3.7;
            sheet.Range[“B6”].NumberValue = 4.5;
            sheet.Range[“B7”].NumberValue = 4.0;
            sheet.Range[“C1”].Value = “小新”;
            sheet.Range[“C2”].NumberValue = 3.8;
            sheet.Range[“C3”].NumberValue = 3.2;
            sheet.Range[“C4”].NumberValue = 1.7;
            sheet.Range[“C5”].NumberValue = 3.5;
            sheet.Range[“C6”].NumberValue = 4.5;
            sheet.Range[“C7”].NumberValue = 4.3;
            sheet.Range[“D1”].Value = “小白”;
            sheet.Range[“D2”].NumberValue = 3.0;
            sheet.Range[“D3”].NumberValue = 2.8;
            sheet.Range[“D4”].NumberValue = 3.5;
            sheet.Range[“D5”].NumberValue = 2.3;
            sheet.Range[“D6”].NumberValue = 3.3;
            sheet.Range[“D7”].NumberValue = 3.8;
            
            Chart chart = sheet.Charts.Add(ExcelChartType.Column3DClustered);
            chart.DataRange = sheet.Range[“B1:D7”];
            chart.SeriesDataFromRange = false;
            chart.TopRow = 7;
            chart.BottomRow = 28;
            chart.LeftColumn = 3;
            chart.RightColumn =11;
            chart.ChartTitle = “带模拟数据表的柱状图示例”;
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Size = 12;
            Spire.Xls.Charts.ChartSerie cs1 = chart.Series[0];      
            cs1.CategoryLabels = sheet.Range[“A2:A7”];

            chart.HasDataTable = true;
        
            workbook.SaveToFile(“S3.xlsx”, ExcelVersion.Version2010);
            System.Diagnostics.Process.Start(“S3.xlsx”);
        }
    }
}

 

 

 

 

相关文章