如何使用C# EPPlus 6.0生成带虚线样式的折线图
EPPlus 6.0 折线图虚线样式实现方案
EPPlus 6.0支持折线图的虚线样式,你之前代码未生效的核心原因是散点图(XYScatter系列)的线条样式配置逻辑和普通折线图不同,需通过Series.Format.Line而非直接操作Border来设置。
以下是修正后的完整代码:
ExcelPackage.LicenseContext = LicenseContext.NonCommercial; var newFile = new FileInfo(filepath); using (ExcelPackage xlPackage = new ExcelPackage(newFile)) { ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets.Add("test"); // 填充数据 worksheet.Cells["A1:A6"].Value = new int[] { 1, 2, 3, 4, 5, 6 }; worksheet.Cells["B1:B6"].Value = new int[] { 10000, 10100, 10200, 10150, 10250, 10200 }; // 创建散点图(无标记平滑线) ExcelChart chart = worksheet.Drawings.AddChart("LineChart", eChartType.XYScatterSmoothNoMarkers); chart.Series.Add(ExcelRange.GetAddress(1, 2, 6, 2), ExcelRange.GetAddress(1, 1, 6, 1)); // 设置系列标题 chart.Series[0].Header = "Blah"; // 关键:通过Format.Line设置虚线样式 var seriesLine = chart.Series[0].Format.Line; seriesLine.Style = eLineStyle.Dash; // 可选样式:Dash, DashDot, LongDashDot, SystemDash等 seriesLine.Width = 1; // 可选:设置线条宽度 seriesLine.Color = Color.Black; // 可选:设置线条颜色 // 移除网格线 chart.Axis[0].RemoveGridlines(); chart.Axis[1].RemoveGridlines(); xlPackage.Save(); MessageBox.Show("Done"); }
核心注意点
- 针对
XYScatter类型图表,EPPlus将线条样式归类在Format.Line节点下,普通折线图(eChartType.Line)则可直接用Series.Border.LineStyle。 - 可选的虚线样式枚举包括:
eLineStyle.Dash(短虚线)、eLineStyle.DashDot(点划线)、eLineStyle.LongDashDot(长点划线)等,可根据需求替换。
内容的提问来源于stack exchange,提问作者Thomas
相关产品推荐
相关产品推荐

