Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Sir,

I am trying to create a pie chart, then export to word using OpenXml in c#.net. But the problem is,

1) It is exporting but nothing shows ms word 2010 only.

2) I am using ASP.NET MVC4 project, there is a google pie chart which has around 1000 data by representing with multiple color. But I cannot see all details from Word when it is exported at first time. Now it seems color only. How can I see all data when exporting word 2016 at first time?

This is my Google Pie-chart. I am trying to export to word 2016 using OpenXml in ASP.NET MVC4, same as the image looking. Please see below link.



I have used the sample code for this below link.



Kindly answer me.
Advance Thanks.

What I have tried:

C#
CreateWordChart createWordChart = null;
//
try
{
    createWordChart = new CreateWordChart(p);             
    List<chartsubarea> chartAreas = new List<chartsubarea>();
    int j=0;

    foreach (var item in charts)
    {
        if (j == 5)
        {
            j = 0;
        }

        chartAreas.Add(new ChartSubArea() 
            { 
                Color = (SchemeColorValues)listArr[j], 
                Label = item.Label, 
                Value = item.Value1.ToString() 
            });
        j++;    
    }               

    createWordChart.CreateChart(chartAreas);
}
catch (Exception ex)
{
}
finally
{
    if (createWordChart != null)
    {
        createWordChart.Dispose();
    }
}

public void CreateChart(List<chartsubarea> chartList)
{
    // Get MainDocumentPart of Document
    MainDocumentPart mainPart = document.AddMainDocumentPart();
    mainPart.Document = new Document(new Body());

    // Create ChartPart object in Word Document
    ChartPart chartPart = mainPart.AddNewPart<chartpart>("rId110");

    // the root element of chartPart 
    dc.ChartSpace chartSpace = new dc.ChartSpace();
    chartSpace.Append(new dc.EditingLanguage() { Val = "en-us" });

    // Create Chart 
    dc.Chart chart = new dc.Chart();
    chart.Append(new dc.AutoTitleDeleted() { Val = true });

    // Define the 3D view
    dc.View3D view3D = new dc.View3D();
    view3D.Append(new dc.RotateX() { Val = 30 });
    view3D.Append(new dc.RotateY() { Val = 0 });

    // Intiliazes a new instance of the PlotArea class
    dc.PlotArea plotArea = new dc.PlotArea();
    plotArea.Append(new dc.Layout());

    // the type of Chart 
    dc.Pie3DChart pie3DChart = new dc.Pie3DChart();
    pie3DChart.Append(new dc.VaryColors() { Val = true });
    dc.PieChartSeries pieChartSers = new dc.PieChartSeries();
    pieChartSers.Append(new dc.Index() { Val = 0U });
    pieChartSers.Append(new dc.Order() { Val = 0U });
    dc.SeriesText seriesText = new dc.SeriesText();
    seriesText.Append(new dc.NumericValue() { Text = "Series" });

    uint rowcount = 0;
    uint count = UInt32.Parse(chartList.Count.ToString());
    string endCell = (count + 1).ToString();
    dc.ChartShapeProperties chartShapePros = new dc.ChartShapeProperties();

    // Define cell for lable information
    dc.CategoryAxisData cateAxisData = new dc.CategoryAxisData();
    dc.StringReference stringRef = new dc.StringReference();
    stringRef.Append(new dc.Formula() { Text = "Main!$A$2:$A$" + endCell });
    dc.StringCache stringCache = new dc.StringCache();
    stringCache.Append(new dc.PointCount() { Val = count });

    // Define cells for value information
    dc.Values values = new dc.Values();
    dc.NumberReference numRef = new dc.NumberReference();
    numRef.Append(new dc.Formula() { Text = "Main!$B$2:$B$" + endCell });

    dc.NumberingCache numCache = new dc.NumberingCache();
    numCache.Append(new dc.FormatCode() { Text = "General" });
    numCache.Append(new dc.PointCount() { Val = count });

    // Fill data for chart
    foreach (var item in chartList)
    {
        if (count == 0)
        {
            chartShapePros.Append(new d.SolidFill(new d.SchemeColor() { Val = item.Color }));
            pieChartSers.Append(chartShapePros);
        }
        else
        {
            dc.DataPoint dataPoint = new dc.DataPoint();
            dataPoint.Append(new dc.Index() { Val = rowcount });
            chartShapePros = new dc.ChartShapeProperties();
            chartShapePros.Append(new d.SolidFill(new d.SchemeColor() { Val = item.Color }));
            dataPoint.Append(chartShapePros);
            pieChartSers.Append(dataPoint);
        }

        dc.StringPoint stringPoint = new dc.StringPoint() { Index = rowcount };
        stringPoint.Append(new dc.NumericValue() { Text = item.Label });
        stringCache.Append(stringPoint);

        dc.NumericPoint numericPoint = new dc.NumericPoint() { Index = rowcount };
        numericPoint.Append(new dc.NumericValue() { Text = item.Value });
        numCache.Append(numericPoint);
        rowcount++;
    }

    // Create c:cat and c:val element 
    stringRef.Append(stringCache);
    cateAxisData.Append(stringRef);
    numRef.Append(numCache);
    values.Append(numRef);

    // Append c:cat and c:val to the end of c:ser element
    pieChartSers.Append(cateAxisData);
    pieChartSers.Append(values);

    // Append c:ser to the end of c:pie3DChart element
    pie3DChart.Append(pieChartSers);

    // Append c:pie3DChart to the end of s:plotArea element
    plotArea.Append(pie3DChart);

    // create child elements of the c:legend element
    dc.Legend legend = new dc.Legend();
    legend.Append(new dc.LegendPosition() { Val = LegendPositionValues.Bottom });
    dc.Overlay overlay = new dc.Overlay() { Val = false };
    legend.Append(overlay);

    dc.TextProperties textPros = new TextProperties();
    textPros.Append(new d.BodyProperties());
    textPros.Append(new d.ListStyle());

    d.Paragraph paragraph = new d.Paragraph();
    d.ParagraphProperties paraPros = new d.ParagraphProperties();
    d.DefaultParagraphProperties defaultParaPros = new d.DefaultParagraphProperties();
        defaultParaPros.Append(new d.LatinFont() { Typeface = "Arial", PitchFamily = 34, CharacterSet = 0 });
        defaultParaPros.Append(new d.ComplexScriptFont() { Typeface = "Arial", PitchFamily = 34, CharacterSet = 0 });
        paraPros.Append(defaultParaPros);
        paragraph.Append(paraPros);
        paragraph.Append(new d.EndParagraphRunProperties() { Language = "en-Us" });

        textPros.Append(paragraph);
        legend.Append(textPros);

        // Append c:view3D, c:plotArea and c:legend elements to the end of c:chart element
        chart.Append(view3D);
        chart.Append(plotArea);
        chart.Append(legend);

        // Append the c:chart element to the end of c:chartSpace element
        chartSpace.Append(chart);

        // Create c:spPr Elements and fill the child elements of it
        chartShapePros = new dc.ChartShapeProperties();
        d.Outline outline = new d.Outline();
        outline.Append(new d.NoFill());
        chartShapePros.Append(outline);

        // Append c:spPr element to the end of c:chartSpace element
        chartSpace.Append(chartShapePros);

        chartPart.ChartSpace = chartSpace;

        // Generate content of the MainDocumentPart
        GeneratePartContent(mainPart);
}

public class ChartSubArea
{
    public SchemeColorValues Color { get; set; }
    public String Label { get; set; }
    public string Value { get; set; }
}</chartpart></chartsubarea></chartsubarea></chartsubarea>
Posted
Updated 11-Jul-16 19:40pm
v3
Comments
Zujaj 11-Jul-16 11:05am    
Sorry for being rude but kindly please use code tags for writing code
such as
<pre lang ="c#">
Console.WriteLine("Hello World");
</pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900