C# 打开XML数据标签
我正在用openxml创建一个条形图,需要将图表底部的x轴标签旋转45度。使用openxml条形图文档,我无法理解如何显示标签。下面是我用来创建图表的函数。如有任何建议,将不胜感激C# 打开XML数据标签,c#,excel,openxml,C#,Excel,Openxml,我正在用openxml创建一个条形图,需要将图表底部的x轴标签旋转45度。使用openxml条形图文档,我无法理解如何显示标签。下面是我用来创建图表的函数。如有任何建议,将不胜感激 public static void InsertChartInSpreadsheet(string docName, string worksheetName, string title, System.Data.DataTable data) { // Open the d
public static void InsertChartInSpreadsheet(string docName, string worksheetName, string title,
System.Data.DataTable data)
{
// Open the document for editing.
using (SpreadsheetDocument document = SpreadsheetDocument.Open(docName, true))
{
IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.Descendants<Sheet>().
Where(s => s.Name == worksheetName);
if (sheets.Count() == 0)
{
// The specified worksheet does not exist.
return;
}
WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.First().Id);
// Add a new drawing to the worksheet.
DrawingsPart drawingsPart = worksheetPart.AddNewPart<DrawingsPart>();
worksheetPart.Worksheet.Append(new DocumentFormat.OpenXml.Spreadsheet.Drawing() { Id = worksheetPart.GetIdOfPart(drawingsPart) });
worksheetPart.Worksheet.Save();
// Add a new chart and set the chart language to English-US.
ChartPart chartPart = drawingsPart.AddNewPart<ChartPart>();
chartPart.ChartSpace = new ChartSpace();
chartPart.ChartSpace.Append(new EditingLanguage() { Val = new StringValue("en-US") });
DocumentFormat.OpenXml.Drawing.Charts.Chart chart = chartPart.ChartSpace.AppendChild<DocumentFormat.OpenXml.Drawing.Charts.Chart>(
new DocumentFormat.OpenXml.Drawing.Charts.Chart());
// Create a new bar chart.
PlotArea plotArea = chart.AppendChild<PlotArea>(new PlotArea());
Layout layout = plotArea.AppendChild<Layout>(new Layout());
BarChart barChart = plotArea.AppendChild<BarChart>(new BarChart(new BarDirection() { Val = new EnumValue<BarDirectionValues>(BarDirectionValues.Column) },
new DataLabels(new DataLabelPosition { Val = new EnumValue<DataLabelPositionValues>(DataLabelPositionValues.InsideBase) }
)));
uint i = 0;
// Iterate through each key in the Dictionary collection and add the key to the chart Series
// and add the corresponding value to the chart Values.
foreach (DataRow r in data.Rows)
{
BarChartSeries barChartSeries = barChart.AppendChild<BarChartSeries>(new BarChartSeries(new Index()
{
Val = new UInt32Value(i)
},
new Order() { Val = new UInt32Value(i) },
new SeriesText(new NumericValue() { Text = r[2].ToString().Trim() }
)));
StringLiteral strLit = barChartSeries.AppendChild<CategoryAxisData>(new CategoryAxisData()).AppendChild<StringLiteral>(new StringLiteral());
strLit.Append(new PointCount() { Val = new UInt32Value(1U) });
strLit.AppendChild<StringPoint>(new StringPoint() { Index = new UInt32Value(0U) }).Append(new NumericValue(title));
NumberLiteral numLit = barChartSeries.AppendChild<DocumentFormat.OpenXml.Drawing.Charts.Values>(
new DocumentFormat.OpenXml.Drawing.Charts.Values()).AppendChild<NumberLiteral>(new NumberLiteral());
numLit.Append(new FormatCode("General"));
numLit.Append(new PointCount() { Val = new UInt32Value(1U) });
numLit.AppendChild<NumericPoint>(new NumericPoint() { Index = new UInt32Value(0u) }).Append(new NumericValue(r[8].ToString().Trim()));
i++;
}
barChart.Append(new AxisId() { Val = new UInt32Value(48650112u) });
barChart.Append(new AxisId() { Val = new UInt32Value(48672768u) });
// Add the Category Axis.
CategoryAxis catAx = plotArea.AppendChild<CategoryAxis>(new CategoryAxis(new AxisId() { Val = new UInt32Value(48650112u) }, new Scaling(new Orientation()
{
Val = new EnumValue<DocumentFormat.
OpenXml.Drawing.Charts.OrientationValues>(DocumentFormat.OpenXml.Drawing.Charts.OrientationValues.MinMax)
}),
new AxisPosition() { Val = new EnumValue<AxisPositionValues>(AxisPositionValues.Bottom) },
new TickLabelPosition() { Val = new EnumValue<TickLabelPositionValues>(TickLabelPositionValues.NextTo) },
new CrossingAxis() { Val = new UInt32Value(48672768U) },
new Crosses() { Val = new EnumValue<CrossesValues>(CrossesValues.AutoZero) },
new AutoLabeled() { Val = new BooleanValue(true) },
new LabelAlignment() { Val = new EnumValue<LabelAlignmentValues>(LabelAlignmentValues.Left) },
new LabelOffset() { Val = new UInt16Value((ushort)100) }));
// Add the Value Axis.
ValueAxis valAx = plotArea.AppendChild<ValueAxis>(new ValueAxis(new AxisId() { Val = new UInt32Value(48672768u) },
new Scaling(new Orientation()
{
Val = new EnumValue<DocumentFormat.OpenXml.Drawing.Charts.OrientationValues>(
DocumentFormat.OpenXml.Drawing.Charts.OrientationValues.MinMax)
}),
new AxisPosition() { Val = new EnumValue<AxisPositionValues>(AxisPositionValues.Left) },
new MajorGridlines(),
new DocumentFormat.OpenXml.Drawing.Charts.NumberingFormat()
{
FormatCode = new StringValue("General"),
SourceLinked = new BooleanValue(true)
}, new TickLabelPosition()
{
Val = new EnumValue<TickLabelPositionValues>
(TickLabelPositionValues.NextTo)
}, new CrossingAxis() { Val = new UInt32Value(48650112U) },
new Crosses() { Val = new EnumValue<CrossesValues>(CrossesValues.AutoZero) },
new CrossBetween() { Val = new EnumValue<CrossBetweenValues>(CrossBetweenValues.Between) }));
// Save the chart part.
chartPart.ChartSpace.Save();
// Position the chart on the worksheet using a TwoCellAnchor object.
drawingsPart.WorksheetDrawing = new WorksheetDrawing();
TwoCellAnchor twoCellAnchor = drawingsPart.WorksheetDrawing.AppendChild<TwoCellAnchor>(new TwoCellAnchor());
twoCellAnchor.Append(new DocumentFormat.OpenXml.Drawing.Spreadsheet.FromMarker(new ColumnId("0"),
new ColumnOffset("581025"),
new RowId("3"),
new RowOffset("114300")));
twoCellAnchor.Append(new DocumentFormat.OpenXml.Drawing.Spreadsheet.ToMarker(new ColumnId("17"),
new ColumnOffset("276225"),
new RowId("32"),
new RowOffset("0")));
// Append a GraphicFrame to the TwoCellAnchor object.
DocumentFormat.OpenXml.Drawing.Spreadsheet.GraphicFrame graphicFrame =
twoCellAnchor.AppendChild<DocumentFormat.OpenXml.
Drawing.Spreadsheet.GraphicFrame>(new DocumentFormat.OpenXml.Drawing.
Spreadsheet.GraphicFrame());
graphicFrame.Macro = "";
graphicFrame.Append(new DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualGraphicFrameProperties(
new DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualDrawingProperties() { Id = new UInt32Value(2u), Name = "Chart 1" },
new DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualGraphicFrameDrawingProperties()));
graphicFrame.Append(new Transform(new Offset() { X = 0L, Y = 0L },
new Extents() { Cx = 0L, Cy = 0L }));
graphicFrame.Append(new Graphic(new GraphicData(new ChartReference() { Id = drawingsPart.GetIdOfPart(chartPart) }) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/chart" }));
twoCellAnchor.Append(new ClientData());
// Save the WorksheetDrawing object.
drawingsPart.WorksheetDrawing.Save();
}
}
public static void InsertChartInSpreadsheet(字符串文档名、字符串工作表名、字符串标题、,
System.Data.DataTable数据)
{
//打开文档进行编辑。
使用(电子表格文档=电子表格文档.Open(docName,true))
{
IEnumerable sheets=document.WorkbookPart.Workbook.subjections()。
其中(s=>s.Name==工作表名称);
如果(sheets.Count()==0)
{
//指定的工作表不存在。
返回;
}
工作表部件工作表部件=(工作表部件)document.WorkbookPart.GetPartById(sheets.First().Id);
//将新图形添加到工作表中。
DrawingsPart DrawingsPart=工作表部件.AddNewPart();
worksheetPart.Worksheet.Append(新的DocumentFormat.OpenXml.Spreadsheet.Drawing(){Id=worksheetPart.GetIdOfPart(drawingsPart)});
worksheetPart.Worksheet.Save();
//添加新图表并将图表语言设置为英语US。
ChartPart ChartPart=drawingsPart.AddNewPart();
chartPart.ChartSpace=新的ChartSpace();
Append(neweditinglanguage(){Val=newstringvalue(“en-US”)});
DocumentFormat.OpenXml.Drawing.Charts.Chart Chart=chartPart.ChartSpace.AppendChild(
新的DocumentFormat.OpenXml.Drawing.Charts.Chart());
//创建一个新的条形图。
PlotArea PlotArea=chart.AppendChild(new PlotArea());
Layout Layout=plotArea.AppendChild(新布局());
BarChart BarChart=plotArea.AppendChild(新的BarChart(新的BarDirection(){Val=new EnumValue(BarDirectionValues.Column)},
新的DataLabelPosition(新的DataLabelPosition{Val=新的EnumValue(DataLabelPositionValues.InsideBase)}
)));
uint i=0;
//迭代字典集合中的每个键,并将该键添加到图表系列中
//并将相应的值添加到图表值中。
foreach(data.Rows中的数据行r)
{
BarChartSeries BarChartSeries=barChart.AppendChild(新的BarChartSeries(新的索引)()
{
Val=新的UINT32值(i)
},
新顺序(){Val=new uint32值(i)},
new SerieText(new NumericValue(){Text=r[2].ToString().Trim()}
)));
StringLiteral strLit=barChartSeries.AppendChild(新CategoryAxisData()).AppendChild(新StringLiteral());
Append(new PointCount(){Val=new uint32值(1U)});
AppendChild(newStringPoint(){Index=new uint32值(0U)}).Append(new NumericValue(title));
NumberTerral numLit=barChartSeries.AppendChild(
新的DocumentFormat.OpenXml.Drawing.Charts.Values()).AppendChild(新的numberTerral());
追加(新格式代码(“通用”));
Append(new PointCount(){Val=new uint32值(1U)});
AppendChild(new NumericPoint(){Index=new uint32 value(0u)}).Append(new numericpvalue(r[8].ToString().Trim());
i++;
}
Append(new AxisId(){Val=new uint32值(48650112u)});
Append(new AxisId(){Val=new uint32值(48672768u)});
//添加类别轴。
CategoryAxis catAx=plotArea.AppendChild(新的CategoryAxis(新的AxisId(){Val=new UINT32值(48650112u)},新的缩放(新的方向()
{
Val=新的枚举值(DocumentFormat.OpenXml.Drawing.Charts.OrientationValues.MinMax)
}),
new AxisPosition(){Val=new EnumValue(AxisPositionValues.Bottom)},
new TickLabelPosition(){Val=new EnumValue(TickLabelPositionValues.NextTo)},
新交叉轴(){Val=new uint32值(48672768U)},
new crosss(){Val=new EnumValue(CrossesValues.AutoZero)},
新的自动标记(){Val=new BooleanValue(true)},
new LabelAlignment(){Val=new EnumValue(LabelAlignmentValues.Left)},
新的LabelOffset(){Val=newuint16value((ushort)100)});
//添加值轴。
ValueAxis valAx=plotArea.AppendChild(新的ValueAxis(新的AxisId(){Val=new UINT32值(48672768u)},
新缩放(新方向()
{
Val=新的枚举值(
DocumentFormat.OpenXml.Drawing.Charts.OrientationValues.MinMax)
}),
new AxisPosition(){Val=new EnumValue(AxisPositionValues.Left)},
新的MajorGridlines(),
新的DocumentFormat.OpenXml.Drawing.Charts.NumberingFormat()
{
FormatCode=新的StringValue(“常规”),
SourceLinked=新布尔值(true)
},新的TickLabelPosition()
{
Val=新的枚举值
(勾选LabelPositionValues.NextTo)
},新交叉轴(){Val=new uint32值(48650112U)},
new crosss(){Val=new EnumValue(CrossesValues.AutoZero)},
new CrossBetween(){Val=new EnumValue(CrossBetweenValues.Between)});
//保存图表部分。
chartPart.ChartSpace.Save();
//使用TwoCellAnchor对象在工作表上定位图表。
drawingsPart.WorksheetDrawing=新工作表DRA
new C.Delete { Val = false },
new C.TextProperties(new BodyProperties { Rotation = -5400000, Vertical = TextVerticalValues.Horizontal },
new ListStyle(),
new Paragraph(new ParagraphProperties(new DefaultRunProperties(), new EndParagraphRunProperties { Language = "en-US" }))),