diff --git a/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart.sln b/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart.sln new file mode 100644 index 000000000..563811da6 --- /dev/null +++ b/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34330.188 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-IChartShape-in-XlsIO-to-WChart", "Convert-IChartShape-in-XlsIO-to-WChart\Convert-IChartShape-in-XlsIO-to-WChart.csproj", "{8AA439D5-CB5A-43ED-A7FB-52C02ABA680D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8AA439D5-CB5A-43ED-A7FB-52C02ABA680D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8AA439D5-CB5A-43ED-A7FB-52C02ABA680D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8AA439D5-CB5A-43ED-A7FB-52C02ABA680D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8AA439D5-CB5A-43ED-A7FB-52C02ABA680D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C2F7F2E8-4DD4-446F-AE6D-C08FC5223C1E} + EndGlobalSection +EndGlobal diff --git a/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart/Convert-IChartShape-in-XlsIO-to-WChart.csproj b/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart/Convert-IChartShape-in-XlsIO-to-WChart.csproj new file mode 100644 index 000000000..23ca67864 --- /dev/null +++ b/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart/Convert-IChartShape-in-XlsIO-to-WChart.csproj @@ -0,0 +1,22 @@ + + + + Exe + net8.0 + Convert_IChartShape_in_XlsIO_to_WChart + enable + enable + + + + + + + + + + Always + + + + diff --git a/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart/Data/Input.xlsx b/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart/Data/Input.xlsx new file mode 100644 index 000000000..9ab9bc0cd Binary files /dev/null and b/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart/Data/Input.xlsx differ diff --git a/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart/Output/.gitkeep b/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart/Program.cs b/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart/Program.cs new file mode 100644 index 000000000..c30559b06 --- /dev/null +++ b/Charts/Convert-IChartShape-in-XlsIO-to-WChart/.NET/Convert-IChartShape-in-XlsIO-to-WChart/Program.cs @@ -0,0 +1,167 @@ +using Syncfusion.DocIO.DLS; +using Syncfusion.OfficeChart; +using Syncfusion.XlsIO; + +class Program +{ + static void Main(string[] args) + { + ConvertExcelChartToWordChart(@"../../../Data/Input.xlsx", @"../../../Output/Output.docx"); + } + public static void ConvertExcelChartToWordChart(string excelFilePath, string outputPath) + { + // Load the Excel document + ExcelEngine excelEngine = new ExcelEngine(); + IApplication application = excelEngine.Excel; + IWorkbook workbook = application.Workbooks.Open(excelFilePath); + IWorksheet worksheet = workbook.Worksheets[0]; + + // Get the first chart from Excel + IChart excelChart = worksheet.Charts[0]; + + // Create Word document + WordDocument wordDocument = new WordDocument(); + + // Create a new section or use existing + WSection section = wordDocument.LastSection as WSection; + if (section == null) + { + section = wordDocument.AddSection() as WSection; + } + + // Create a paragraph to add the chart + IWParagraph paragraph = section.AddParagraph(); + + // Create a new chart in Word with the same chart type + WChart wordChart = paragraph.AppendChart(500, 400); + wordChart.ChartType = ConvertExcelChartTypeToWord(excelChart.ChartType); + + // Set chart size + wordChart.Width = (float)excelChart.Width; + wordChart.Height = (float)excelChart.Height; + + // Copy chart data + CopyChartData(excelChart, wordChart); + + // Apply formatting + ApplyChartFormatting(excelChart, wordChart); + + // Save the Word document + wordDocument.Save(outputPath); + wordDocument.Close(); + + // Close Excel + workbook.Close(); + excelEngine.Dispose(); + } + + private static OfficeChartType ConvertExcelChartTypeToWord(ExcelChartType excelChartType) + { + switch (excelChartType) + { + case ExcelChartType.Column_Clustered: + return OfficeChartType.Column_Clustered; + + case ExcelChartType.Column_Stacked: + return OfficeChartType.Column_Stacked; + + case ExcelChartType.Line: + return OfficeChartType.Line; + + case ExcelChartType.Pie: + return OfficeChartType.Pie; + + case ExcelChartType.Bar_Clustered: + return OfficeChartType.Bar_Clustered; + + case ExcelChartType.Area: + return OfficeChartType.Area; + + default: + return OfficeChartType.Column_Clustered; // Default fallback + } + } + private static void CopyChartData(IChart excelChart, WChart wordChart) + { + IRange dataRange = excelChart.DataRange; + + int rowCount = dataRange.Rows.Length; + int colCount = dataRange.Columns.Length; + + for (int i = 0; i < rowCount; i++) + { + for (int j = 0; j < colCount; j++) + { + object cellValue = dataRange[i + 1, j + 1].Value; + + if (cellValue != null) + { + wordChart.ChartData.SetValue(i + 1, j + 1, cellValue); + } + else + { + wordChart.ChartData.SetValue(i + 1, j + 1, string.Empty); + } + } + } + + wordChart.DataRange = wordChart.ChartData[ + dataRange.Row, + dataRange.Column, + dataRange.LastRow, + dataRange.LastColumn]; + } + + private static void ApplyChartFormatting(IChart excelChart, WChart wordChart) + { + // Set chart title + if (excelChart.HasTitle && !string.IsNullOrWhiteSpace(excelChart.ChartTitle)) + { + wordChart.ChartTitle = excelChart.ChartTitle; + wordChart.ChartTitleArea.FontName = excelChart.ChartTitleArea.FontName; + } + + // Set legend properties + wordChart.HasLegend = excelChart.HasLegend; + + if (excelChart.HasLegend && + excelChart.Legend != null && + wordChart.Legend != null) + { + switch (excelChart.Legend.Position) + { + case ExcelLegendPosition.Bottom: + wordChart.Legend.Position = OfficeLegendPosition.Bottom; + break; + + case ExcelLegendPosition.Left: + wordChart.Legend.Position = OfficeLegendPosition.Left; + break; + + case ExcelLegendPosition.Right: + wordChart.Legend.Position = OfficeLegendPosition.Right; + break; + + case ExcelLegendPosition.Top: + wordChart.Legend.Position = OfficeLegendPosition.Top; + break; + } + } + + // Set value axis title + if (excelChart.PrimaryValueAxis != null && + !string.IsNullOrEmpty(excelChart.PrimaryValueAxis.Title)) + { + wordChart.PrimaryValueAxis.Title = + excelChart.PrimaryValueAxis.Title; + } + + // Set category axis title + if (excelChart.PrimaryCategoryAxis != null && + !string.IsNullOrEmpty(excelChart.PrimaryCategoryAxis.Title)) + { + wordChart.PrimaryCategoryAxis.Title = + excelChart.PrimaryCategoryAxis.Title; + } + } +}