Skip to content

Commit d4a47c9

Browse files
ES-992113-Changes Added
1 parent 71f68ab commit d4a47c9

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Extracting-data-from-multiple-excel-files/Extracting-data-from-multiple-excel-files.csproj" />
3+
</Solution>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Extracting_data_from_multiple_excel_files</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
13+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<None Update="Data\Excel1.xlsx">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
<None Update="Data\Excel2.xlsx">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
<None Update="Output\.gitkeep">
24+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
25+
</None>
26+
</ItemGroup>
27+
28+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)