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