Wednesday, 5 June 2024

filltemplate class for above code

     public class WordTemplateService

    {

        private readonly IWebHostEnvironment _env;


        public WordTemplateService(IWebHostEnvironment env)

        {

            _env = env;

        }


        public void FillTemplate(string templateFileName, string outputFileName, Dictionary<string, string> placeholders, List<Dictionary<string, string>> tableData)

        {

            // Construct the full path to the template in the wwwroot folder

            string templatePath = System.IO.Path.Combine(_env.WebRootPath, templateFileName);


            // Construct the full path to the output file in the wwwroot folder

            string outputPath = System.IO.Path.Combine(_env.WebRootPath, outputFileName);


            // Copy the template to the output path

            File.Copy(templatePath, outputPath, true);


            // Open the output document

            using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(outputPath, true))

            {

                // Replace text placeholders

                ReplaceTextPlaceholders(wordDocument, placeholders);


                // Replace table placeholders

                FillTableData(wordDocument, tableData);

                //FillTableData(wordDocument, tableData);


                // Save the changes

                wordDocument.MainDocumentPart.Document.Save();

            }

        }


        private void ReplaceTextPlaceholders(WordprocessingDocument document, Dictionary<string, string> placeholders)

        {

            MainDocumentPart mainPart = document.MainDocumentPart;

            string docText = null;


            // Read the document text

            using (StreamReader sr = new StreamReader(mainPart.GetStream()))

            {

                docText = sr.ReadToEnd();

            }


            // Replace the placeholders with actual data

            foreach (var placeholder in placeholders)

            {

                docText = docText.Replace(placeholder.Key, placeholder.Value);

            }


            // Write the updated text back to the document

            using (StreamWriter sw = new StreamWriter(mainPart.GetStream(FileMode.Create)))

            {

                sw.Write(docText);

            }

        }


        private void FillTableData(WordprocessingDocument document, List<Dictionary<string, string>> tableData)

        {

           


                Body body = document.MainDocumentPart.Document.Body;


                // Locate the existing table. Adjust the method of locating the table as needed.

                Table table = body.Elements<Table>().FirstOrDefault();

                if (table == null)

                {

                    throw new System.Exception("Table not found in the document.");

                }

                var templateRow = table.Elements<TableRow>().FirstOrDefault();

                if (templateRow == null) return;

                var headers = tableData[0].Keys;

                // Clone the template row to create new rows

                foreach (var rowData in tableData)

                {

                    TableRow tableRow = new TableRow();

                    foreach (var header in headers)

                    {

                        TableCell cell = new TableCell(new Paragraph(new Run(new Text(rowData[header]))));

                        tableRow.Append(cell);

                    }

                    table.Append(tableRow);

                }


                document.MainDocumentPart.Document.Save();

                // Remove the template row

                //table.RemoveChild(templateRow);

            

        }


    }

}


No comments:

Post a Comment

Best UI