有没有办法使用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;
我可以编辑轴的最小,最大,主要/次要单位,但不能编辑标签的对齐方式。
答案 0 :(得分:2)
我能够通过保存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();
答案 1 :(得分:1)
在这种情况下,我发现使用OpenXML SDK比使用Excel Interop更受欢迎。实际的代码是可怕的,但至少它不依赖于打开隐藏的Excel副本,实际上必须安装在用户的计算机或服务器等上。
真正的魔力在于BodyProperties
加Rotation = 5400000
的部分。其他一切都是OpenXML样板/ 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);
}
答案 2 :(得分:-1)
对于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中使用录制宏按钮并创建新宏供您使用