C# EPPLUS 设置图表 X 轴刻度标签的角度
C# EPPLUS setting the angle of the chart's X axis tick labels
有没有办法使用 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;
我可以编辑轴的最小值、最大值 major/minor 单位,但不能编辑标签的对齐方式。
我可以通过保存 Epplus ExcelPackage 来设置 45 度轴标签对齐,通过 Microsoft.Office.Interop.Excel 重新打开文件然后格式化它。完成后不要忘记保存、关闭和退出。
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();
对 Epplus 框架代码中不存在的 Excel 文件做一些事情...
您可以使用 添加在打开 Excel 文件中执行的宏 。
查看此示例代码,用于旋转图表中系列中的所有标签数据文本,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 中录制宏按钮 并创建新宏供您使用
在这种情况下,我发现使用 OpenXML SDK 比使用 Excel Interop 更好。实际代码是丑陋的,但至少它不依赖于打开 Excel 的隐藏副本,该副本实际上必须安装在用户的计算机或服务器等
真正的魔法在于添加 BodyProperties
和 Rotation = 5400000
的部分。其他一切都是 OpenXML boilerplate/cruft,并使用 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);
}
有没有办法使用 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;
我可以编辑轴的最小值、最大值 major/minor 单位,但不能编辑标签的对齐方式。
我可以通过保存 Epplus ExcelPackage 来设置 45 度轴标签对齐,通过 Microsoft.Office.Interop.Excel 重新打开文件然后格式化它。完成后不要忘记保存、关闭和退出。
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();
对 Epplus 框架代码中不存在的 Excel 文件做一些事情...
您可以使用 添加在打开 Excel 文件中执行的宏 。 查看此示例代码,用于旋转图表中系列中的所有标签数据文本,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 中录制宏按钮 并创建新宏供您使用
在这种情况下,我发现使用 OpenXML SDK 比使用 Excel Interop 更好。实际代码是丑陋的,但至少它不依赖于打开 Excel 的隐藏副本,该副本实际上必须安装在用户的计算机或服务器等
真正的魔法在于添加 BodyProperties
和 Rotation = 5400000
的部分。其他一切都是 OpenXML boilerplate/cruft,并使用 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);
}