EPPlus绘制刻度线

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OfficeOpenXml;
using OfficeOpenXml.Drawing.Chart;

namespace _2022_03_10_测试Epplus
{
    class Program
    {
        static void Main(string[] args)
        {
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;


            List<MyData> li = new List<MyData>();
            for (int i = 0; i < 100; i++)
            {
                li.Add(new MyData() { X = i, Y = i });
            }


            using (var package = new ExcelPackage(new FileInfo("MyWo0123.xlsx")))
            {


                var ws = package.Workbook.Worksheets.Add("123");

                ws.Cells["A1"].LoadFromCollection(li, true);
                var yRange = ws.Cells[$"A2:A" + (li.Count() + 1)];
                var xRange = ws.Cells[$"B2:B" + (li.Count() + 1)];
                 
                int i = 0; 
                // 添加时间图
                var lineChart = ws.Drawings.AddChart("line", eChartType.Line);

                var lineSeries = lineChart.Series.Add(yRange, xRange);




                lineChart.Legend.Position = eLegendPosition.Top;
                lineChart.SetSize(1620, 350);
                lineChart.SetPosition(2 + 20 * i, 0, 1, 0);

                string specialRange = "";
                string xRanges = "";
                for (int j = 0; j < 100; j++)
                {
                    if (j % 30 == 0)
                    {
                        specialRange += $"A{li.Count + 1}:A{(li.Count + 1)},";
                        xRanges += $"B{j + 1},";
                    }
                    else
                    {
                        specialRange += $"Z{j + 1}:Z{(j + 1)},";
                        xRanges += $"B{j + 1},";
                    }
                }

                specialRange = specialRange.TrimEnd(',');
                xRanges = xRanges.TrimEnd(',');
                var xychart = lineChart.PlotArea.ChartTypes.Add(eChartType.ColumnClustered) as ExcelBarChart;
                xychart.GapWidth = 500;

                var newSeries = xychart.Series.Add(ws.Cells[specialRange], ws.Cells[xRanges]);
                newSeries.Fill.Color = Color.Red;

                package.Save();
            }
            Console.WriteLine("ok");
            Console.Read();
        }


        public class MyData
        {
            public int X { get; set; }


            public int Y { get; set; }

            public int Z { get; set; }
        }
    }
} 

 

效果图:

 

posted @ 2022-06-27 22:02  灰主流  阅读(133)  评论(0编辑  收藏  举报