Export DIV html to Word in ASP.Net MVC

Last Reply 3 months ago By dharmendr

Posted 3 months ago

i want to print some data from div to word in mvc.how can i acheive this please refer any code ?thanks in advance.

Results 1 - 5 of 6 12
Posted 3 months ago
Hi @krish2018,
Please try the following

Export HTML DIV to Word or PDF in ASP.Net

It might help you.

Cheers Andrea.

Posted 3 months ago

 

public static class WordHelper
{
    public static byte[] HtmlToWord(String html)
    {
        const string filename = "test.docx";
        if (File.Exists(filename)) File.Delete(filename);

        using (MemoryStream generatedDocument = new MemoryStream())
        {
            using (WordprocessingDocument package = WordprocessingDocument.Create(
                   generatedDocument, WordprocessingDocumentType.Document))
            {
                MainDocumentPart mainPart = package.MainDocumentPart;
                if (mainPart == null)
                {
                    mainPart = package.AddMainDocumentPart();
                    new Document(new Body()).Save(mainPart);
                }

                HtmlConverter converter = new HtmlConverter(mainPart);
                Body body = mainPart.Document.Body;

                var paragraphs = converter.Parse(html);
                for (int i = 0; i < paragraphs.Count; i++)
                {
                    body.Append(paragraphs[i]);
                }

                mainPart.Document.Save();
            }

            return generatedDocument.ToArray();
        }
    }
}

 [HttpPost]
    [ValidateInput(false)]
    public FileResult Demo(CkEditorViewModel viewModel)
    {
        return File(WordHelper.HtmlToWord(viewModel.CkEditorContent),
          "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    }

 


Posted 3 months ago

 

https://www.c-sharpcorner.com/uploadfile/d2dcfc/convert-html-to-word-then-word-to-pdf-with-C-Sharp/

 



Posted 3 months ago
dharmendr says:

Hi krish2018,

Refer below article.

Export Grid (Html Table) data from database to Word in ASP.Net MVC

 Thank U Dharmendr Sir.

Your code is proved very useful for me but actually my doc file also contains a photo
and this photo is not showing in the downloaded doc file