diff --git a/HTML-conversions/Convert_EMF-images_to_PNG_in_Word-document/.NET/Convert_EMF-images_to_PNG_in_Word-document/Program.cs b/HTML-conversions/Convert_EMF-images_to_PNG_in_Word-document/.NET/Convert_EMF-images_to_PNG_in_Word-document/Program.cs index e09fb0c6..9d8d29ff 100644 --- a/HTML-conversions/Convert_EMF-images_to_PNG_in_Word-document/.NET/Convert_EMF-images_to_PNG_in_Word-document/Program.cs +++ b/HTML-conversions/Convert_EMF-images_to_PNG_in_Word-document/.NET/Convert_EMF-images_to_PNG_in_Word-document/Program.cs @@ -44,7 +44,9 @@ picture.Height = height; picture.Width = width; } + imageByteStream.Dispose(); } + stream.Dispose(); } // Save the modified document to the specified output file. using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.html"), FileMode.Create, FileAccess.Write)) diff --git a/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Program.cs b/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Program.cs index acb223ff..2b03fb73 100644 --- a/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Program.cs +++ b/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Program.cs @@ -127,6 +127,16 @@ private static void IterateParagraph(ParagraphItemCollection paraItems) InlineContentControl inlineContentControl = entity as InlineContentControl; IterateParagraph(inlineContentControl.ParagraphItems); break; + case EntityType.Symbol: + WSymbol symbol = entity as WSymbol; + symbol.CharacterFormat.LocaleIdASCII = (short)LocaleIDs.fr_FR; + break; + case EntityType.Footnote: + WFootnote footer = entity as WFootnote; + footer.MarkerCharacterFormat.LocaleIdASCII = (short)LocaleIDs.fr_FR; + // Also iterate its TextBody to set on inner WTextRange + IterateTextBody(footer.TextBody); + break; } } } diff --git a/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable.sln b/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable.sln new file mode 100644 index 00000000..38c7ba1b --- /dev/null +++ b/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36930.0 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Extract-Table-to-DataTable", "Extract-Table-to-DataTable\Extract-Table-to-DataTable.csproj", "{109F9D9A-2486-6191-E3ED-B9C89550B0D4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {109F9D9A-2486-6191-E3ED-B9C89550B0D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {109F9D9A-2486-6191-E3ED-B9C89550B0D4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {109F9D9A-2486-6191-E3ED-B9C89550B0D4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {109F9D9A-2486-6191-E3ED-B9C89550B0D4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8D7007A5-9202-4A9B-A8E2-50EAB025B312} + EndGlobalSection +EndGlobal diff --git a/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Data/Input.docx b/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Data/Input.docx new file mode 100644 index 00000000..94acc0a0 Binary files /dev/null and b/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Data/Input.docx differ diff --git a/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Extract-Table-to-DataTable.csproj b/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Extract-Table-to-DataTable.csproj new file mode 100644 index 00000000..91411574 --- /dev/null +++ b/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Extract-Table-to-DataTable.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + Extract-Table-to-DataTable + enable + enable + + + + + + + + + + Always + + + Always + + + diff --git a/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Output/.gitkeep b/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Output/.gitkeep new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Output/Result.xlsx b/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Output/Result.xlsx new file mode 100644 index 00000000..332d028d Binary files /dev/null and b/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Output/Result.xlsx differ diff --git a/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Program.cs b/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Program.cs new file mode 100644 index 00000000..f37df381 --- /dev/null +++ b/Tables/Extract-Table-to-DataTable/.NET/Extract-Table-to-DataTable/Program.cs @@ -0,0 +1,182 @@ +using System.Text; +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using Syncfusion.XlsIO; + +class Program +{ + static void Main(string[] args) + { + // Load existing word document + using (FileStream inputfileStream = new FileStream(Path.GetFullPath(@"../../../Data/Input.docx"), FileMode.Open)) + { + using (WordDocument document = new WordDocument(inputfileStream, FormatType.Automatic)) + { + using (ExcelEngine engine = new ExcelEngine()) + { + IApplication app = engine.Excel; + app.DefaultVersion = ExcelVersion.Excel2016; + + // Create one sheet to start with; we’ll add sheets as we find more tables. + IWorkbook workbook = app.Workbooks.Create(1); + int sheetIndex = 0; + int tableNumber = 0; + + // Get table entities in word document + List entities = document.FindAllItemsByProperty(EntityType.Table, null, null); + + foreach (Entity entity in entities) + { + WTable wTable = (WTable)entity; + + if (sheetIndex >= workbook.Worksheets.Count) + workbook.Worksheets.Create(); + + IWorksheet worksheet = workbook.Worksheets[sheetIndex++]; + worksheet.Name = $"Table{++tableNumber}"; + + // Export with merges starting + ExportWordTableToExcelMerged(wTable, worksheet); + + // Formatting + worksheet.UsedRange.AutofitRows(); + worksheet.UsedRange.AutofitColumns(); + } + using (FileStream outputStream = new FileStream(Path.GetFullPath(@"../../../Output/Result.xlsx"), FileMode.Create)) + { + workbook.SaveAs(outputStream); + } + workbook.Close(); + } + } + } + } + + /// + /// Writes a Word table to the worksheet, preserving horizontal and vertical merges. + /// startRow/startCol are 1-based Excel coordinates where the table should be placed. + /// + static void ExportWordTableToExcelMerged(IWTable table, IWorksheet worksheet) + { + for (int r = 0; r < table.Rows.Count; r++) + { + WTableRow wRow = (WTableRow)table.Rows[r]; + + // Map Word's logical grid to Excel columns using GridSpan + int gridCol = 1; + + for (int i = 0; i < wRow.Cells.Count; i++) + { + WTableCell wCell = wRow.Cells[i]; + + // Horizontal width in grid columns + int hSpan = (int)wCell.GridSpan; + + // Merge flags + CellMerge vFlag = wCell.CellFormat.VerticalMerge; // None | Start | Continue + CellMerge hFlag = wCell.CellFormat.HorizontalMerge; + + // Excel start cell for this Word cell + int excelStartRowIndex = r + 1; + int excelStartColIndex = gridCol; + + // Compute vertical span when this cell is the START of a vertical merge + int vSpan = 1; + if (vFlag == CellMerge.Start) + { + // Count how many subsequent rows continue the merge at the same grid column + for (int nr = r + 1; nr < table.Rows.Count; nr++) + { + WTableRow nextRow = (WTableRow)table.Rows[nr]; + WTableCell nextCell = GetCellAtGridColumn(nextRow, excelStartColIndex); // 1-based grid col + if (nextCell != null && nextCell.CellFormat.VerticalMerge == CellMerge.Continue) + vSpan++; + else + break; + } + } + if (hFlag == CellMerge.Start) + { + for( int nc = i + 1; nc < wRow.Cells.Count; nc++) + { + WTableCell cell = wRow.Cells[nc]; + if (cell != null && cell.CellFormat.HorizontalMerge == CellMerge.Continue) + hSpan += cell.GridSpan; + else + break; + } + } + + // Is Start or None of a merge region + bool isNotContinuedCell = + (vFlag != CellMerge.Continue) && + (hFlag != CellMerge.Continue); + + if (isNotContinuedCell) + { + int vMergeEndIndex = excelStartRowIndex + vSpan - 1; + int hMergeEndColIndex = excelStartColIndex + hSpan - 1; + + // Merge in Excel if region spans multiple cells + if (vMergeEndIndex > excelStartRowIndex || hMergeEndColIndex > excelStartColIndex) + worksheet.Range[excelStartRowIndex, excelStartColIndex, vMergeEndIndex, hMergeEndColIndex].Merge(); + + // Write the visible text to the top-left Excel cell + IRange range = worksheet.Range[excelStartRowIndex, excelStartColIndex]; + range.Text = BuildCellText(wCell); + + // Format styling + range.CellStyle.HorizontalAlignment = ExcelHAlign.HAlignCenter; + range.CellStyle.VerticalAlignment = ExcelVAlign.VAlignCenter; + + worksheet.Range[excelStartRowIndex, excelStartColIndex, vMergeEndIndex, hMergeEndColIndex].CellStyle.Borders[ExcelBordersIndex.EdgeLeft].LineStyle = ExcelLineStyle.Thin; + worksheet.Range[excelStartRowIndex, excelStartColIndex, vMergeEndIndex, hMergeEndColIndex].CellStyle.Borders[ExcelBordersIndex.EdgeRight].LineStyle = ExcelLineStyle.Thin; + worksheet.Range[excelStartRowIndex, excelStartColIndex, vMergeEndIndex, hMergeEndColIndex].CellStyle.Borders[ExcelBordersIndex.EdgeTop].LineStyle = ExcelLineStyle.Thin; + worksheet.Range[excelStartRowIndex, excelStartColIndex, vMergeEndIndex, hMergeEndColIndex].CellStyle.Borders[ExcelBordersIndex.EdgeBottom].LineStyle = ExcelLineStyle.Thin; + + } + + // Advance Excel column cursor by the horizontal span of this Word cell + gridCol += hSpan; + } + } + } + + /// + /// Returns the WTableCell occupying the given 1-based "grid column" in this row, + /// taking each cell's GridSpan into account. + /// + static WTableCell GetCellAtGridColumn(WTableRow row, int gridColumn) + { + int cursor = 1; // 1-based grid column within the table + foreach (WTableCell c in row.Cells) + { + int span = (int)c.GridSpan; + int start = cursor; + int end = cursor + span - 1; + if (gridColumn >= start && gridColumn <= end) + return c; + cursor += span; + } + return null; + } + + /// + /// Concatenate all paragraph texts in a Word cell (one per line). + /// + static string BuildCellText(WTableCell cell) + { + StringBuilder sb = new StringBuilder(); + for (int p = 0; p < cell.Paragraphs.Count; p++) + { + WParagraph para = cell.Paragraphs[p]; + string text = para.Text?.TrimEnd(); + if (!string.IsNullOrEmpty(text)) + { + if (sb.Length > 0) sb.AppendLine(); + sb.Append(text); + } + } + return sb.ToString(); + } +} \ No newline at end of file