Ejemplo n.º 1
0
        private void button1_Click(object sender, System.EventArgs e)
        {
            try
            {
                //Getting Data files path.
                string dataPath = Application.StartupPath + @"..\..\..\..\..\..\..\common\Data\DocIO\";

                //A new document is created.
                WordDocument document = new WordDocument(dataPath + "PieChart.docx");
                //Get chart data from xml file
                DataSet ds = new DataSet();
                ds.ReadXml(dataPath + "Products.xml");
                //Merge the product table in the Word document
                document.MailMerge.ExecuteGroup(ds.Tables["Product"]);
                //Find the Placeholder of Pie chart to insert
                TextSelection selection = document.Find("<Pie Chart>", false, false);
                WParagraph    paragraph = selection.GetAsOneRange().OwnerParagraph;
                paragraph.ChildEntities.Clear();
                //Create and Append chart to the paragraph
                WChart pieChart = paragraph.AppendChart(446, 270);
                //Set chart data
                pieChart.ChartType  = OfficeChartType.Pie;
                pieChart.ChartTitle = "Best Selling Products";
                pieChart.ChartTitleArea.FontName = "Calibri (Body)";
                pieChart.ChartTitleArea.Size     = 14;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    pieChart.ChartData.SetValue(i + 2, 1, ds.Tables[0].Rows[i].ItemArray[1]);
                    pieChart.ChartData.SetValue(i + 2, 2, ds.Tables[0].Rows[i].ItemArray[2]);
                }
                //Create a new chart series with the name “Sales”
                IOfficeChartSerie pieSeries = pieChart.Series.Add("Sales");
                pieSeries.Values = pieChart.ChartData[2, 2, 11, 2];
                //Setting data label
                pieSeries.DataPoints.DefaultDataPoint.DataLabels.IsPercentage = true;
                pieSeries.DataPoints.DefaultDataPoint.DataLabels.Position     = OfficeDataLabelPosition.Outside;
                //Setting background color
                pieChart.ChartArea.Fill.ForeColor           = Color.FromArgb(242, 242, 242);
                pieChart.PlotArea.Fill.ForeColor            = Color.FromArgb(242, 242, 242);
                pieChart.ChartArea.Border.LinePattern       = OfficeChartLinePattern.None;
                pieChart.PrimaryCategoryAxis.CategoryLabels = pieChart.ChartData[2, 1, 11, 1];
                //Saving the document as .docx
                document.Save("Sample.docx", FormatType.Docx);
                //Message box confirmation to view the created document.
                if (MessageBoxAdv.Show("Do you want to view the generated Word document?", "Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    try
                    {
                        //Launching the MS Word file using the default Application.[MS Word Or Free WordViewer]
#if NETCORE
                        System.Diagnostics.Process process = new System.Diagnostics.Process();
                        process.StartInfo = new System.Diagnostics.ProcessStartInfo("Sample.docx")
                        {
                            UseShellExecute = true
                        };
                        process.Start();
#else
                        System.Diagnostics.Process.Start("Sample.docx");
#endif
                        //Exit
                        this.Close();
                    }
                    catch (Win32Exception ex)
                    {
                        MessageBoxAdv.Show("Microsoft Word Viewer or Microsoft Word is not installed in this system");
                        Console.WriteLine(ex.ToString());
                    }
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
Ejemplo n.º 2
0
        public ActionResult PieChart(string Group1)
        {
            if (Group1 == null)
            {
                return(View());
            }
            string basePath = _hostingEnvironment.WebRootPath;
            string datapath = basePath + @"/DocIO/PieChart.docx";
            //A new document is created.
            FileStream   fileStream = new FileStream(datapath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            WordDocument document   = new WordDocument(fileStream, FormatType.Docx);
            //Create MailMergeDataTable
            MailMergeDataTable mailMergeDataTable = GetMailMergeDataTableProductDetails();

            //Merge the product table in the Word document
            document.MailMerge.ExecuteGroup(mailMergeDataTable);
            //Find the Placeholder of Pie chart to insert
            TextSelection selection = document.Find("<Pie Chart>", false, false);
            WParagraph    paragraph = selection.GetAsOneRange().OwnerParagraph;

            paragraph.ChildEntities.Clear();
            //Create and Append chart to the paragraph
            WChart pieChart = paragraph.AppendChart(446, 270);

            //Set chart data
            pieChart.ChartType  = OfficeChartType.Pie;
            pieChart.ChartTitle = "Best Selling Products";
            pieChart.ChartTitleArea.FontName = "Calibri (Body)";
            pieChart.ChartTitleArea.Size     = 14;
            GetChartData(pieChart, 0);
            //Create a new chart series with the name “Sales”
            IOfficeChartSerie pieSeries = pieChart.Series.Add("Sales");

            pieSeries.Values = pieChart.ChartData[2, 2, 11, 2];
            //Setting data label
            pieSeries.DataPoints.DefaultDataPoint.DataLabels.IsPercentage = true;
            pieSeries.DataPoints.DefaultDataPoint.DataLabels.Position     = OfficeDataLabelPosition.Outside;
            //Setting background color
            pieChart.ChartArea.Fill.ForeColor           = Syncfusion.Drawing.Color.FromArgb(242, 242, 242);
            pieChart.PlotArea.Fill.ForeColor            = Syncfusion.Drawing.Color.FromArgb(242, 242, 242);
            pieChart.ChartArea.Border.LinePattern       = OfficeChartLinePattern.None;
            pieChart.PrimaryCategoryAxis.CategoryLabels = pieChart.ChartData[2, 1, 11, 1];

            string       filename    = "";
            string       contenttype = "";
            MemoryStream ms          = new MemoryStream();

            #region Document SaveOption
            if (Group1 == "WordDocx")
            {
                filename    = "Sample.docx";
                contenttype = "application/msword";
                document.Save(ms, FormatType.Docx);
            }
            else if (Group1 == "WordML")
            {
                filename    = "Sample.xml";
                contenttype = "application/msword";
                document.Save(ms, FormatType.WordML);
            }
            else
            {
                filename    = "Sample.pdf";
                contenttype = "application/pdf";
                DocIORenderer renderer = new DocIORenderer();
                renderer.ConvertToPDF(document).Save(ms);
            }
            #endregion Document SaveOption
            document.Close();
            ms.Position = 0;
            return(File(ms, contenttype, filename));
        }
Ejemplo n.º 3
0
        public ActionResult PieChart(string Group1)
        {
            if (Group1 == null)
            {
                return(View());
            }

            //A new document is created.
            WordDocument document = new WordDocument(ResolveApplicationDataPath("PieChart.docx", "App_Data\\DocIO"));
            //Get chart data from xml file
            DataSet ds = new DataSet();

            ds.ReadXml(ResolveApplicationDataPath("Products.xml", "App_Data\\DocIO"));
            //Merge the product table in the Word document
            document.MailMerge.ExecuteGroup(ds.Tables["Product"]);
            //Find the Placeholder of Pie chart to insert
            TextSelection selection = document.Find("<Pie Chart>", false, false);
            WParagraph    paragraph = selection.GetAsOneRange().OwnerParagraph;

            paragraph.ChildEntities.Clear();
            //Create and Append chart to the paragraph
            WChart pieChart = paragraph.AppendChart(446, 270);

            //Set chart data
            pieChart.ChartType  = OfficeChartType.Pie;
            pieChart.ChartTitle = "Best Selling Products";
            pieChart.ChartTitleArea.FontName = "Calibri (Body)";
            pieChart.ChartTitleArea.Size     = 14;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                pieChart.ChartData.SetValue(i + 2, 1, ds.Tables[0].Rows[i].ItemArray[1]);
                pieChart.ChartData.SetValue(i + 2, 2, ds.Tables[0].Rows[i].ItemArray[2]);
            }
            //Create a new chart series with the name “Sales”
            IOfficeChartSerie pieSeries = pieChart.Series.Add("Sales");

            pieSeries.Values = pieChart.ChartData[2, 2, 11, 2];
            //Setting data label
            pieSeries.DataPoints.DefaultDataPoint.DataLabels.IsPercentage = true;
            pieSeries.DataPoints.DefaultDataPoint.DataLabels.Position     = OfficeDataLabelPosition.Outside;
            //Setting background color
            pieChart.ChartArea.Fill.ForeColor           = System.Drawing.Color.FromArgb(242, 242, 242);
            pieChart.PlotArea.Fill.ForeColor            = System.Drawing.Color.FromArgb(242, 242, 242);
            pieChart.ChartArea.Border.LinePattern       = OfficeChartLinePattern.None;
            pieChart.PrimaryCategoryAxis.CategoryLabels = pieChart.ChartData[2, 1, 11, 1];

            #region Document save option
            //Save as .docx format
            if (Group1 == "WordDocx")
            {
                return(document.ExportAsActionResult("Sample.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment));
            }
            // Save as WordML(.xml) format
            else if (Group1 == "WordML")
            {
                return(document.ExportAsActionResult("Sample.xml", FormatType.WordML, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment));
            }
            return(View());

            #endregion Document save option
        }