|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using Syncfusion.DocIORenderer; |
| 4 | +using Syncfusion.Drawing; |
| 5 | +using Syncfusion.XlsIO; |
| 6 | + |
| 7 | + |
| 8 | +namespace Extract_data_from_multiple_excel |
| 9 | +{ |
| 10 | + class Program |
| 11 | + { |
| 12 | + static void Main(string[] args) |
| 13 | + { |
| 14 | + //Creates a new Word document. |
| 15 | + using (WordDocument document = new WordDocument()) |
| 16 | + { |
| 17 | + // List of Excel file names (without extension) |
| 18 | + string[] excelFiles = { "Excel1", "Excel2" }; |
| 19 | + // Loop through each Excel file |
| 20 | + foreach (string excelName in excelFiles) |
| 21 | + { |
| 22 | + // Get the Excel content |
| 23 | + UpdateExcelToWord(document, excelName); |
| 24 | + } |
| 25 | + //Creates file stream. |
| 26 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 27 | + { |
| 28 | + //Saves the Word document to file stream. |
| 29 | + document.Save(outputFileStream, FormatType.Docx); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Reads an Excel workbook and appends each worksheet as a table into the specified Word document. |
| 37 | + /// </summary> |
| 38 | + /// <param name="document"></param> |
| 39 | + |
| 40 | + private static void UpdateExcelToWord(WordDocument document, string excelName) |
| 41 | + { |
| 42 | + // Open the Excel file |
| 43 | + ExcelEngine excelEngine = new ExcelEngine(); |
| 44 | + IApplication application = excelEngine.Excel; |
| 45 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 46 | + // Load the file into stream |
| 47 | + using (FileStream inputExcelStream = new FileStream(Path.GetFullPath(@"Data/") + excelName + ".xlsx", FileMode.Open, FileAccess.Read)) |
| 48 | + { |
| 49 | + // Open the workbook from the stream. |
| 50 | + IWorkbook workbook = application.Workbooks.Open(inputExcelStream); |
| 51 | + // Loop through all worksheets |
| 52 | + foreach (IWorksheet worksheet in workbook.Worksheets) |
| 53 | + { |
| 54 | + // Add a new section for each worksheet |
| 55 | + IWSection section = document.AddSection(); |
| 56 | + // Add a new table to the section |
| 57 | + WTable table = section.AddTable() as WTable; |
| 58 | + table.AutoFit(AutoFitType.FitToContent); |
| 59 | + //Set Title for table |
| 60 | + table.Title = excelName + "_" + worksheet.Name; |
| 61 | + // Determine the number of rows and columns in the worksheet. |
| 62 | + int rows = worksheet.Rows.Length; |
| 63 | + int columns = worksheet.Columns.Length; |
| 64 | + // Initialize the table with the required number of rows and columns. |
| 65 | + table.ResetCells(rows, columns); |
| 66 | + table.TableFormat.Borders.BorderType = BorderStyle.Single; |
| 67 | + table.TableFormat.Borders.LineWidth = 1; |
| 68 | + table.TableFormat.Borders.Color = Color.Black; |
| 69 | + // Populate the table cell-by-cell from the worksheet. |
| 70 | + for (int rowIndex = 0; rowIndex < rows; rowIndex++) |
| 71 | + { |
| 72 | + // Get the current row range in the worksheet. |
| 73 | + IRange rowRange = worksheet.Rows[rowIndex]; |
| 74 | + |
| 75 | + for (int cellIndex = 0; cellIndex < columns; cellIndex++) |
| 76 | + { |
| 77 | + // Access the specific cell within the current row. |
| 78 | + IRange cell = rowRange.Cells[cellIndex]; |
| 79 | + // Add a paragraph into the corresponding Word table cell. |
| 80 | + WParagraph paragraph = table[rowIndex, cellIndex].AddParagraph() as WParagraph; |
| 81 | + // Insert the cell's display text into the Word paragraph. |
| 82 | + WTextRange textRange = paragraph.AppendText(cell.DisplayText) as WTextRange; |
| 83 | + // Preserve basic font styling from the Excel cell into the Word text. |
| 84 | + textRange.CharacterFormat.Bold = cell.CellStyle.Font.Bold; |
| 85 | + textRange.CharacterFormat.Italic = cell.CellStyle.Font.Italic; |
| 86 | + textRange.CharacterFormat.FontSize = (float)cell.CellStyle.Font.Size; |
| 87 | + textRange.CharacterFormat.FontName = cell.CellStyle.Font.FontName; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
0 commit comments