/// <summary> /// Adds the string point. /// </summary> /// <param name="stringCache">The string cache.</param> /// <param name="index">The index.</param> /// <param name="stringPointNumericVal">The string point numeric val.</param> protected static void AddStringPoint(StringCache stringCache, int index, string stringPointNumericVal) { StringPoint stringPoint = new StringPoint(); stringPoint.Index = new UInt32Value((uint)index); stringPoint.NumericValue = new NumericValue(stringPointNumericVal); stringCache.Append(stringPoint); }
/// <summary> /// Gets the string reference. /// </summary> /// <param name="formulaText">The formula text.</param> /// <param name="pointCountVal">The point count val.</param> /// <returns></returns> protected static StringReference GetStringReference(string formulaText, int pointCountVal) { StringReference stringReference = new StringReference(); Formula formula = new Formula(); formula.Text = formulaText; StringCache stringCache = new StringCache(); PointCount pointCount = new PointCount() { Val = (UInt32Value)(uint)pointCountVal }; stringCache.Append(pointCount); stringReference.Append(formula); stringReference.Append(stringCache); return(stringReference); }
/// <summary> /// Modify/Add categories into chart XML /// </summary> /// <param name="column_index">Corresponds to the column index that needs to be modified in chart spreadsheet (Ex: A, B, C, ...)</param> /// <param name="row_index">Corresponds to the column index that needs to be modified in excel </param> /// <param name="new_value">Corresponds to the new value we need to insert to the cell </param> protected override void ModifyChartXML_Categories(string column_index, uint row_index, string new_value) { foreach (LineChartSeries linechart_series in chart_part.ChartSpace.Descendants <LineChartSeries>().ToList()) { CategoryAxisData category_axis_data = linechart_series.Descendants <CategoryAxisData>().FirstOrDefault(); if (category_axis_data == null) { // If no StringReference --> Clone one from the 1st (usually we go in this when we create a new BarChartSeries) BarChartSeries template_barchartseries = chart_part.ChartSpace.Descendants <BarChartSeries>().FirstOrDefault(); CategoryAxisData template_categoryaxisdata = template_barchartseries.Descendants <CategoryAxisData>().FirstOrDefault(); CategoryAxisData new_categoryaxisdata = new CategoryAxisData(template_categoryaxisdata.OuterXml); linechart_series.Append(new_categoryaxisdata); } else { StringReference sr = category_axis_data.Descendants <StringReference>().FirstOrDefault(); // If there is a StringReference --> Update its values StringCache sc = sr.Descendants <StringCache>().First(); try { StringPoint sp = sc.Descendants <StringPoint>().ElementAt((int)row_index - 2); NumericValue nv = sp.Descendants <NumericValue>().First(); nv.Text = new_value; } catch (Exception) { // Create new data and append to previous XML sc.PointCount.Val = sc.PointCount.Val + 1; NumericValue nv = new NumericValue(new_value); StringPoint sp = new StringPoint(nv); sp.Index = (uint)sc.Descendants <StringPoint>().ToList().Count; sc.Append(sp); // Change fomula range DocumentFormat.OpenXml.Drawing.Charts.Formula f = sr.Descendants <DocumentFormat.OpenXml.Drawing.Charts.Formula>().FirstOrDefault(); f.Text = worksheet_name + "!$A$2:$A$" + GetRowIndexByNum((int)row_index - 2).ToString(); } } } }
protected void modificaChartData(string FormatoValori, string titoloSerie, out SeriesText seriesText1, out CategoryAxisData categoryAxisData1, out Values values1) { seriesText1 = new SeriesText(); StringReference stringReference1 = new StringReference(); Formula formula1 = new Formula(); formula1.Text = "Foglio1!$B$1"; StringCache stringCache1 = new StringCache(); PointCount pointCount1 = new PointCount() { Val = (UInt32Value)1U }; StringPoint stringPoint1 = new StringPoint() { Index = (UInt32Value)0U }; NumericValue numericValue1 = new NumericValue(); numericValue1.Text = titoloSerie; stringPoint1.Append(numericValue1); stringCache1.Append(pointCount1); stringCache1.Append(stringPoint1); stringReference1.Append(formula1); stringReference1.Append(stringCache1); seriesText1.Append(stringReference1); DataPoint dataPoint1 = new DataPoint(); Index index2 = new Index() { Val = (UInt32Value)2U }; dataPoint1.Append(index2); //################################i testi #################################### categoryAxisData1 = new CategoryAxisData(); StringReference stringReference2 = new StringReference(); Formula formula2 = new Formula(); formula2.Text = string.Format("Foglio1!$A$2:$A${0}", valori.Count + 2); StringCache stringCache2 = new StringCache(); UInt32Value nValori = Convert.ToUInt32(valori.Count); PointCount pointCount2 = new PointCount() { Val = nValori }; StringPoint[] stringPoints = new StringPoint[nValori]; UInt32Value n = 0; foreach (KeyValuePair <string, double> item in valori) { stringPoints[n] = new StringPoint() { Index = (UInt32Value)n }; NumericValue numericValue2 = new NumericValue(); numericValue2.Text = item.Key; stringPoints[n].Append(numericValue2); n += 1; } stringCache2.Append(pointCount2); for (int i = 0; i < n; i++) { stringCache2.Append(stringPoints[i]); } stringReference2.Append(formula2); stringReference2.Append(stringCache2); categoryAxisData1.Append(stringReference2); //################################i valori#################################### values1 = new Values(); NumberReference numberReference1 = new NumberReference(); Formula formula3 = new Formula(); formula3.Text = string.Format("Foglio1!$B$2:$B${0}", valori.Count + 2); NumberingCache numberingCache1 = new NumberingCache(); FormatCode formatCode1 = new FormatCode(); formatCode1.Text = FormatoValori; //<----------------------------------------------------------- PointCount pointCount3 = new PointCount() { Val = nValori }; NumericPoint[] numericPoints = new NumericPoint[nValori]; n = 0; foreach (KeyValuePair <string, double> item in valori) { numericPoints[n] = new NumericPoint() { Index = (UInt32Value)n }; NumericValue numericValue = new NumericValue(); numericValue.Text = item.Value.ToString(); numericValue.Text = numericValue.Text.Replace(",", "."); // devo forzare il formato americano numericPoints[n].Append(numericValue); n += 1; } numberingCache1.Append(formatCode1); numberingCache1.Append(pointCount3); for (int i = 0; i < n; i++) { numberingCache1.Append(numericPoints[i]); } numberReference1.Append(formula3); numberReference1.Append(numberingCache1); values1.Append(numberReference1); }
public void CreateExcelDoc(string fileName) { List <Student> students = new List <Student>(); Initizalize(students); using (SpreadsheetDocument document = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook)) { WorkbookPart workbookPart = document.AddWorkbookPart(); workbookPart.Workbook = new Workbook(); WorksheetPart worksheetPart = workbookPart.AddNewPart <WorksheetPart>(); worksheetPart.Worksheet = new Worksheet(); Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets()); Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Students" }; SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData()); // Add drawing part to WorksheetPart DrawingsPart drawingsPart = worksheetPart.AddNewPart <DrawingsPart>(); worksheetPart.Worksheet.Append(new Drawing() { Id = worksheetPart.GetIdOfPart(drawingsPart) }); worksheetPart.Worksheet.Save(); drawingsPart.WorksheetDrawing = new WorksheetDrawing(); sheets.Append(sheet); workbookPart.Workbook.Save(); // Add a new chart and set the chart language ChartPart chartPart = drawingsPart.AddNewPart <ChartPart>(); chartPart.ChartSpace = new ChartSpace(); chartPart.ChartSpace.AppendChild(new EditingLanguage() { Val = "en-US" }); Chart chart = chartPart.ChartSpace.AppendChild(new Chart()); chart.AppendChild(new AutoTitleDeleted() { Val = true }); // We don't want to show the chart title // Create a new Clustered Column Chart PlotArea plotArea = chart.AppendChild(new PlotArea()); Layout layout = plotArea.AppendChild(new Layout()); BarChart barChart = plotArea.AppendChild(new BarChart( new BarDirection() { Val = new EnumValue <BarDirectionValues>(BarDirectionValues.Column) }, new BarGrouping() { Val = new EnumValue <BarGroupingValues>(BarGroupingValues.Clustered) }, new VaryColors() { Val = false } )); // Constructing header Row row = new Row(); int rowIndex = 1; row.AppendChild(ConstructCell(string.Empty, CellValues.String)); foreach (var month in Months.Short) { row.AppendChild(ConstructCell(month, CellValues.String)); } // Insert the header row to the Sheet Data sheetData.AppendChild(row); rowIndex++; // Create chart series for (int i = 0; i < students.Count; i++) { BarChartSeries barChartSeries = barChart.AppendChild(new BarChartSeries( new Index() { Val = (uint)i }, new Order() { Val = (uint)i }, new SeriesText(new NumericValue() { Text = students[i].Name }) )); // Adding category axis to the chart CategoryAxisData categoryAxisData = barChartSeries.AppendChild(new CategoryAxisData()); // Category // Constructing the chart category string formulaCat = "Students!$B$1:$G$1"; StringReference stringReference = categoryAxisData.AppendChild(new StringReference() { Formula = new DocumentFormat.OpenXml.Drawing.Charts.Formula() { Text = formulaCat } }); StringCache stringCache = stringReference.AppendChild(new StringCache()); stringCache.Append(new PointCount() { Val = (uint)Months.Short.Length }); for (int j = 0; j < Months.Short.Length; j++) { stringCache.AppendChild(new NumericPoint() { Index = (uint)j }).Append(new NumericValue(Months.Short[j])); } } var chartSeries = barChart.Elements <BarChartSeries>().GetEnumerator(); for (int i = 0; i < students.Count; i++) { row = new Row(); row.AppendChild(ConstructCell(students[i].Name, CellValues.String)); chartSeries.MoveNext(); string formulaVal = string.Format("Students!$B${0}:$G${0}", rowIndex); DocumentFormat.OpenXml.Drawing.Charts.Values values = chartSeries.Current.AppendChild(new DocumentFormat.OpenXml.Drawing.Charts.Values()); NumberReference numberReference = values.AppendChild(new NumberReference() { Formula = new DocumentFormat.OpenXml.Drawing.Charts.Formula() { Text = formulaVal } }); NumberingCache numberingCache = numberReference.AppendChild(new NumberingCache()); numberingCache.Append(new PointCount() { Val = (uint)Months.Short.Length }); for (uint j = 0; j < students[i].Values.Length; j++) { var value = students[i].Values[j]; row.AppendChild(ConstructCell(value.ToString(), CellValues.Number)); numberingCache.AppendChild(new NumericPoint() { Index = j }).Append(new NumericValue(value.ToString())); } sheetData.AppendChild(row); rowIndex++; } barChart.AppendChild(new DataLabels( new ShowLegendKey() { Val = false }, new ShowValue() { Val = false }, new ShowCategoryName() { Val = false }, new ShowSeriesName() { Val = false }, new ShowPercent() { Val = false }, new ShowBubbleSize() { Val = false } )); barChart.Append(new AxisId() { Val = 48650112u }); barChart.Append(new AxisId() { Val = 48672768u }); // Adding Category Axis plotArea.AppendChild( new CategoryAxis( new AxisId() { Val = 48650112u }, new Scaling(new Orientation() { Val = new EnumValue <DocumentFormat.OpenXml.Drawing.Charts.OrientationValues>(DocumentFormat.OpenXml.Drawing.Charts.OrientationValues.MinMax) }), new Delete() { Val = false }, new AxisPosition() { Val = new EnumValue <AxisPositionValues>(AxisPositionValues.Bottom) }, new TickLabelPosition() { Val = new EnumValue <TickLabelPositionValues>(TickLabelPositionValues.NextTo) }, new CrossingAxis() { Val = 48672768u }, new Crosses() { Val = new EnumValue <CrossesValues>(CrossesValues.AutoZero) }, new AutoLabeled() { Val = true }, new LabelAlignment() { Val = new EnumValue <LabelAlignmentValues>(LabelAlignmentValues.Center) } )); // Adding Value Axis plotArea.AppendChild( new ValueAxis( new AxisId() { Val = 48672768u }, new Scaling(new Orientation() { Val = new EnumValue <DocumentFormat.OpenXml.Drawing.Charts.OrientationValues>(DocumentFormat.OpenXml.Drawing.Charts.OrientationValues.MinMax) }), new Delete() { Val = false }, new AxisPosition() { Val = new EnumValue <AxisPositionValues>(AxisPositionValues.Left) }, new MajorGridlines(), new DocumentFormat.OpenXml.Drawing.Charts.NumberingFormat() { FormatCode = "General", SourceLinked = true }, new TickLabelPosition() { Val = new EnumValue <TickLabelPositionValues>(TickLabelPositionValues.NextTo) }, new CrossingAxis() { Val = 48650112u }, new Crosses() { Val = new EnumValue <CrossesValues>(CrossesValues.AutoZero) }, new CrossBetween() { Val = new EnumValue <CrossBetweenValues>(CrossBetweenValues.Between) } )); chart.Append( new PlotVisibleOnly() { Val = true }, new DisplayBlanksAs() { Val = new EnumValue <DisplayBlanksAsValues>(DisplayBlanksAsValues.Gap) }, new ShowDataLabelsOverMaximum() { Val = false } ); chartPart.ChartSpace.Save(); // Positioning the chart on the spreadsheet TwoCellAnchor twoCellAnchor = drawingsPart.WorksheetDrawing.AppendChild(new TwoCellAnchor()); twoCellAnchor.Append(new DocumentFormat.OpenXml.Drawing.Spreadsheet.FromMarker( new ColumnId("0"), new ColumnOffset("0"), new RowId((rowIndex + 2).ToString()), new RowOffset("0") )); twoCellAnchor.Append(new DocumentFormat.OpenXml.Drawing.Spreadsheet.ToMarker( new ColumnId("8"), new ColumnOffset("0"), new RowId((rowIndex + 12).ToString()), new RowOffset("0") )); // Append GraphicFrame to TwoCellAnchor GraphicFrame graphicFrame = twoCellAnchor.AppendChild(new GraphicFrame()); graphicFrame.Macro = string.Empty; graphicFrame.Append(new NonVisualGraphicFrameProperties( new NonVisualDrawingProperties() { Id = 2u, Name = "Sample Chart" }, new NonVisualGraphicFrameDrawingProperties() )); graphicFrame.Append(new Transform( new DocumentFormat.OpenXml.Drawing.Offset() { X = 0L, Y = 0L }, new DocumentFormat.OpenXml.Drawing.Extents() { Cx = 0L, Cy = 0L } )); graphicFrame.Append(new DocumentFormat.OpenXml.Drawing.Graphic( new DocumentFormat.OpenXml.Drawing.GraphicData( new ChartReference() { Id = drawingsPart.GetIdOfPart(chartPart) } ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/chart" } )); twoCellAnchor.Append(new ClientData()); drawingsPart.WorksheetDrawing.Save(); worksheetPart.Worksheet.Save(); } }