【问题标题】:C# EPPLUS setting the angle of the chart's X axis tick labelsC# EPPLUS 设置图表 X 轴刻度标签的角度
【发布时间】:2016-02-17 15:47:20
【问题描述】:

有没有办法使用 EPPLUS 设置图表中轴刻度标签的角度对齐方式?我正在生成 eChartType.XYScatterLinesNoMarkers 图表,我的 X 轴(有很多刻度标签)看起来非常混乱。

图表当前的 X 轴杂乱无章: X axis horizontal alignment

我希望图表的外观: X axis 45 degree alignment

如果无法设置角度,是否可以将标签的方向设置为垂直方向;即90°?

var chart = chartWorksheet.Drawings.AddChart(entry.Key, eChartType.XYScatterLinesNoMarkers);
                    chart.XAxis.MaxValue = businessDayDate.ToOADate();
                    chart.XAxis.MinValue = businessDayDate.AddDays(chartDayThreshold * -1).ToOADate();
                    chart.XAxis.MajorUnit = 20;

我可以编辑轴的最小、最大、主要/次要单位,但不能编辑标签的对齐方式。

【问题讨论】:

  • 不要认为Epplus有这样的选择。恐怕您必须通过 XML 添加它或使用另一个库。路径为c:chartSpace/c:chart/c:plotArea/c:valAx/c:txPr/a:bodyPr,并将rot="2700000" 的属性添加到其中。
  • 谢谢@Ernie,这在Epplus 中不可用,这很糟糕。我查看了其他一些帖子,但找不到任何东西。看起来这样做的唯一方法是通过package.save 保存ExcelPackage package,然后按照您的建议通过XML 或通过Microsoft.Office.Interop.Excel 重新打开和格式化工作表。我将发布这个问题的答案,以展示我是如何使用Microsoft.Office.Interop.Excel 做到的。

标签: c# charts epplus


【解决方案1】:

在这种情况下,我发现使用 OpenXML SDK 比使用 Excel 互操作更可取。实际的代码是可怕的,但至少它不依赖于打开隐藏的 Excel 副本,它必须实际安装在用户的计算机或服务器等上。

真正的魔力在于添加BodyPropertiesRotation = 5400000 的部分。其他一切都是 OpenXML 样板/杂乱无章,并使用 OpenXML Productivity Tool 自动生成。

using (SpreadsheetDocument document = SpreadsheetDocument.Open(outputFile, true))
        {
            var chartSheet = document.WorkbookPart.Workbook.Descendants<Sheet>().SingleOrDefault(s => s.Name == "Geo Type Chart");
            if (chartSheet == null)
            {
                return;
            }

            WorksheetPart wsPart = (WorksheetPart)document.WorkbookPart.GetPartById(chartSheet.Id);
            DrawingsPart dp = wsPart.DrawingsPart;
            ChartPart cp = dp.ChartParts.FirstOrDefault();
            if (cp == null)
            {
                return;
            }

            C.ChartSpace chartSpace1 = cp.ChartSpace;
            C.Chart chart1=chartSpace1.GetFirstChild<C.Chart>();
            C.PlotArea plotArea1=chart1.GetFirstChild<C.PlotArea>();
            C.CategoryAxis categoryAxis1=plotArea1.GetFirstChild<C.CategoryAxis>();
            C.CrossingAxis crossingAxis1=categoryAxis1.GetFirstChild<C.CrossingAxis>();

            C.TextProperties textProperties1 = new C.TextProperties();
            A.BodyProperties bodyProperties1 = new A.BodyProperties(){ Rotation = 5400000 };
            A.ListStyle listStyle1 = new A.ListStyle();

            A.Paragraph paragraph1 = new A.Paragraph();

            A.ParagraphProperties paragraphProperties1 = new A.ParagraphProperties();
            A.DefaultRunProperties defaultRunProperties1 = new A.DefaultRunProperties();

            paragraphProperties1.Append(defaultRunProperties1);
            A.EndParagraphRunProperties endParagraphRunProperties1 = new A.EndParagraphRunProperties(){ Language = "en-US" };

            paragraph1.Append(paragraphProperties1);
            paragraph1.Append(endParagraphRunProperties1);

            textProperties1.Append(bodyProperties1);
            textProperties1.Append(listStyle1);
            textProperties1.Append(paragraph1);
            categoryAxis1.InsertBefore(textProperties1,crossingAxis1);
        }

【讨论】:

    【解决方案2】:

    我可以通过保存 Epplus ExcelPackage、通过 Microsoft.Office.Interop.Excel 重新打开文件然后对其进行格式化来设置 45 度轴标签对齐。完成后不要忘记保存、关闭和退出。

    string fullFileNameWithPath = "C:\Temp\chartSheet.xlsx";
    Application excelApp = new Application();
    Workbook excelWorkbook = excelApp.Workbooks.Open(fullFileNameWithPath,
                    0, false, 5, "", "", false, XlPlatform.xlWindows, "",
                    true, false, 0, true, false, false);
    
    //Get all sheets in workbook.
    Sheets excelSheets = excelWorkbook.Worksheets;
    
    //Get main chart sheet.
    string currentSheet = "Chart Report";
    Worksheet excelWorksheet = (Worksheet)excelSheets.get_Item(currentSheet);
    
    //Access the chart.
    ChartObject chartObject2 = (ChartObject)excelWorksheet.ChartObjects("Chart 1");
    Microsoft.Office.Interop.Excel.Chart chartPage = chartObject2.Chart;
    chartPage.Axes(XlAxisType.xlCategory).TickLabels.Orientation = -45;
    
    excelWorkbook.Save();
    excelApp.Workbooks.Close();
    excelApp.Quit();
    

    【讨论】:

      【解决方案3】:

      对于 Epplus 框架代码中不存在的 Excel 文件做一些事情...

      您可以使用在打开的 Excel 文件中执行的添加宏。 查看此示例代码,用于在图表中的 Serie 上旋转所有标签数据文本,而 Epplus 中不存在执行此操作的代码。

      package.Workbook.CreateVBAProject();
      OfficeOpenXml.VBA.ExcelVBAModule excelVbaModule =
                         package.Workbook.VbaProject.Modules.AddModule("Module1");
      System.Text.StringBuilder mac = new System.Text.StringBuilder();
      mac.AppendLine("Sub Auto_Start()");
      mac.AppendLine("Sheets(2).Select");
      mac.AppendLine("ActiveSheet.ChartObjects(\"chartWeeklyReport\").Activate");
      mac.AppendLine("ActiveChart.FullSeriesCollection(1).DataLabels.Select");
      mac.AppendLine("Selection.Position = xlLabelPositionAbove");
      mac.AppendLine("Selection.Orientation = xlUpward");
      mac.AppendLine("Selection.Position = xlLabelPositionAbove");
      mac.AppendLine("End Sub");
      excelVbaModule.Code = mac.ToString();
      package.Save();
      

      对于创建其他宏,您可以在 Excel 中使用 录制宏按钮 并创建新宏供您使用

      【讨论】:

        猜你喜欢
        • 2019-10-29
        • 1970-01-01
        • 2012-03-14
        • 2011-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-02
        • 1970-01-01
        相关资源
        最近更新 更多