Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Dec 02, 2015 7:35 pm

We are currently using Spire office in our project, We have a requirement to add dynamic charts(Bar chart, PIE chart) based on data from the database. I went through the tutorial but I don’t see any examples on this using spire.doc.

Is adding a chart to word possible using Spire.Doc.

If so please provide some examples. Thanks and appreciate any help on this.

Aneil
 
Posts: 14
Joined: Wed Dec 02, 2015 7:32 pm

Thu Dec 03, 2015 6:24 am

Hi,

Thanks for your inquiry.
Sorry that at present our Spire.Doc doesn't support to create a chart in word document.
But there are alternative solutions, one is to create the charts dynamically by Spire.Xls and then insert
the chart image into Word document by Spire.Doc.
Code: Select all
 public void AddChartsIntoWord(string result,Spire.Doc.FileFormat format)
        {
            Document doc = new Document();
            Section section = doc.AddSection();
            Paragraph paragraph = section.AddParagraph();

            Image image = ChartToImage();
     
            paragraph.AppendPicture(image);
            doc.SaveToFile(result, format);
        }
     
        private Image ChartToImage()
        {
            Workbook workbook = new Workbook();

         //Initailize worksheet
         workbook.CreateEmptySheets(1);
         Worksheet sheet = workbook.Worksheets[0];

            CreatePieChart(sheet);

            Image image= workbook.SaveChartAsImage(sheet, 0);

            return image;
      }
      private void CreatePieChart(Worksheet sheet)
      {
            sheet.Name = "Chart data";
            sheet.GridLinesVisible = false;
           
            Chart chart = sheet.Charts.Add(ExcelChartType.Pie);
            //dataTable from DataBase
           sheet.InsertDataTable(dataTable, true, 1, 1);

            chart.PlotArea.Fill.Visible = false;
         //Set region of chart data
         chart.DataRange = sheet.Range["B2:B5"];
         chart.SeriesDataFromRange = false;

         //Set position of chart
         chart.LeftColumn = 1;
         chart.TopRow = 6;
         chart.RightColumn = 9;
         chart.BottomRow = 25;

         //Chart title
         chart.ChartTitle = "Sales by year";
         chart.ChartTitleArea.IsBold = true;
         chart.ChartTitleArea.Size = 12;

         Spire.Xls.Charts.ChartSerie cs = chart.Series[0];
         cs.CategoryLabels = sheet.Range["A2:A5"];
         cs.Values = sheet.Range["B2:B5"];
         cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
        }


The other is to embed chart excel as an ole object into word document.
Sample code:
Code: Select all
public void AddChartsIntoWord(string result,Spire.Doc.FileFormat format)
{
Document doc = new Document();
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();
MemoryStream ms = new MemoryStream();
Image image = ChartToImage(out ms);

DocPicture docP = new DocPicture(doc);
docP.LoadImage(image);
DocOleObject ole = paragraph.AppendOleObject(ms, docP, Spire.Doc.Documents.OleObjectType.ExcelWorksheet);

doc.SaveToFile(result, format);
}

private Image ChartToImage(out MemoryStream stream)
{
Workbook workbook = new Workbook();

//Initailize worksheet
workbook.CreateEmptySheets(1);
Worksheet sheet = workbook.Worksheets[0];

CreatePieChart(sheet);
stream = new MemoryStream();
workbook.SaveToStream(stream, Spire.Xls.FileFormat.Version2007);
Image image= workbook.SaveChartAsImage(sheet, 0);

return image;
}

private void CreatePieChart(Worksheet sheet)
{
sheet.Name = "Chart data";
sheet.GridLinesVisible = false;

Chart chart = sheet.Charts.Add(ExcelChartType.Pie);

sheet.InsertDataTable(dataTable, true, 1, 1);

chart.PlotArea.Fill.Visible = false;
//Set region of chart data
chart.DataRange = sheet.Range["B2:B5"];
chart.SeriesDataFromRange = false;

//Set position of chart
chart.LeftColumn = 1;
chart.TopRow = 6;
chart.RightColumn = 9;
chart.BottomRow = 25;

//Chart title
chart.ChartTitle = "Sales by year";
chart.ChartTitleArea.IsBold = true;
chart.ChartTitleArea.Size = 12;

Spire.Xls.Charts.ChartSerie cs = chart.Series[0];
cs.CategoryLabels = sheet.Range["A2:A5"];
cs.Values = sheet.Range["B2:B5"];
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;

}


Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Fri Dec 11, 2015 5:50 am

Thanks. I tried both the approaches which you had mentioned. But converting the chart as Image object is making the chart to loose its quality. With the borders not being sharp enough. Below is the output in word file
ExcelChartConvertedAsImageInWord.JPG



I tried creating the same chart to display in excel file using spire.xls. Its looks perfect. Below is the output
ExcelChart.JPG


Some how I could not achieve the same quality of chart in word when creating chart dynamically using spire.xls and converting as Image.

Aneil
 
Posts: 14
Joined: Wed Dec 02, 2015 7:32 pm

Fri Dec 11, 2015 8:24 am

Hi,

I did a test with the latest version, Spire.Office Platinum (Hot Fix) Version:2.12.7 but didn't recreate the quality issue. Please try the version.
If you still have the quality issue, please share all your code to help us recreate your issue.
Thank you.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Fri Dec 11, 2015 10:05 pm

I am using the version 2.12.7. Here is the sample code that i tried
Code: Select all
static void Main(string[] args)
        {
           Program cs = new Program();
            cs.AddChartsIntoWord(@"WordReport\NewDoc.docx",Spire.Doc.FileFormat.Docx2010);
            System.Diagnostics.Process.Start(@"WordReport\NewDoc.docx");


        }

        public void AddChartsIntoWord(string result, Spire.Doc.FileFormat format)
        {
            Document doc = new Document();
            Section section = doc.AddSection();
            Paragraph paragraph = section.AddParagraph();
            MemoryStream ms = new MemoryStream();
            Image image = ChartToImage(out ms);

            DocPicture docP = new DocPicture(doc);
            docP.LoadImage(image);
            DocOleObject ole = paragraph.AppendOleObject(ms, docP, Spire.Doc.Documents.OleObjectType.ExcelWorksheet);

            doc.SaveToFile(result, format);
        }

        private Image ChartToImage(out MemoryStream stream)
        {
            Workbook workbook = new Workbook();

            //Initailize worksheet
            workbook.CreateEmptySheets(1);
            Worksheet sheet = workbook.Worksheets[0];

            CreatePieChart(sheet);
            stream = new MemoryStream();
            workbook.SaveToStream(stream, Spire.Xls.FileFormat.Version2010);
            Image image = workbook.SaveChartAsImage(sheet, 0);

            return image;
        }

        private void CreatePieChart(Worksheet sheet)
        {
            //Create a chart
            Chart chart = sheet.Charts.Add(ExcelChartType.Pie);

            //Set region of chart data
            chart.DataRange = sheet.Range["B2:B5"];
            chart.SeriesDataFromRange = false;

            //Set position of chart
            chart.LeftColumn = 1;
            chart.TopRow = 6;
            chart.RightColumn = 9;
            chart.BottomRow = 25;

            //Chart title
            chart.ChartTitle = "Sales by year";
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Size = 12;

            //Initialize the chart series
            Spire.Xls.Charts.ChartSerie cs = chart.Series[0];
            //cs.Name = "";
            cs.DataPoints.DefaultDataPoint.DataLabels.HasSeriesName = false;
            cs.DataPoints.DefaultDataPoint.DataLabels.HasCategoryName = false;
            cs.DataPoints.DefaultDataPoint.DataLabels.HasPercentage = true;
            cs.DataPoints.DefaultDataPoint.DataFormat.LineProperties.Weight = ChartLineWeightType.Wide;
            cs.DataPoints.DefaultDataPoint.DataFormat.Fill.FillType = ShapeFillType.SolidColor;
            cs.DataPoints.DefaultDataPoint.DataFormat.LineProperties.Color = Color.White;
            cs.DataPoints.DefaultDataPoint.DataFormat.LineProperties.Weight = ChartLineWeightType.Wide;
            //Chart Labels resource
            cs.CategoryLabels = sheet.Range["A2:A5"];

            //Chart value resource
            cs.Values = sheet.Range["B2:B5"];

            //Set the value visible in the chart
            cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = false;

            //Year
            sheet.Range["A1"].Value = "Year";
            sheet.Range["A2"].Value = "2002";
            sheet.Range["A3"].Value = "2003";
            sheet.Range["A4"].Value = "2004";
            sheet.Range["A5"].Value = "2005";

            //Sales
            sheet.Range["B1"].Value = "Sales";
            sheet.Range["B2"].NumberValue = 4000;
            sheet.Range["B3"].NumberValue = 6000;
            sheet.Range["B4"].NumberValue = 500;
            sheet.Range["B5"].NumberValue = 8500;

            //Style
            sheet.Range["A1:B1"].Style.Font.IsBold = true;
            sheet.Range["A2:B2"].Style.KnownColor = ExcelColors.LightYellow;
            sheet.Range["A3:B3"].Style.KnownColor = ExcelColors.LightGreen1;
            sheet.Range["A4:B4"].Style.KnownColor = ExcelColors.LightOrange;
            sheet.Range["A5:B5"].Style.KnownColor = ExcelColors.LightTurquoise;

            //Border
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeTop].Color = Color.FromArgb(0, 0, 128);
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeBottom].Color = Color.FromArgb(0, 0, 128);
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeLeft].Color = Color.FromArgb(0, 0, 128);
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeRight].Color = Color.FromArgb(0, 0, 128);
            sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;

            //Number format
            sheet.Range["B2:C5"].Style.NumberFormat = "\"$\"#,##0";
            chart.PlotArea.Fill.Visible = false;

        }


Below is the chart output in word.
ChartOutputinWord.JPG

Aneil
 
Posts: 14
Joined: Wed Dec 02, 2015 7:32 pm

Mon Dec 14, 2015 3:00 am

Hi,

Thanks for providing.
I used Spire.XLS Pack Hotfix Version:7.8.64 to test the code about converting excel chart to image. There is just data lable value(32%) is wrong,the result image is attached.
I have forwarded it to our dev team. At present Spire.Xls.dll of Spire.Office doesn't update to the latest version(version 7.8.64), so the result you got was generated by old spire.xls.dll.

We will provide you a new version of Spire.Office when the issue is resolved.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Fri Jan 29, 2016 8:55 am

Hi,

Thanks for waiting.
There is new version of Spire.Office has been released. Welcome to download and test Spire.Office Platinum Version:2.13.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Wed Feb 03, 2016 9:00 am

Hi,

Did new version resolve your issue?
Thanks for your feedback.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc