Doubt when generating a. doc file with C#

Asked

Viewed 574 times

1

The below method downloads the document that is generated dynamically, but when the file opens it shows a file conversion option, leaving the item marked by default Unicode(UTF-8).

What to do not show this message?

    public static void gerarRelatorioAtoDoc(string texto)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.ContentType = "application/msword";

        String strNomeArquiv = "RelAtoGerado" + DateTime.Now.ToShortDateString().ToString() + ".doc";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strNomeArquiv);

        StringBuilder strHTMLContent = new StringBuilder();
        strHTMLContent.Append(texto);
        HttpContext.Current.Response.Write(strHTMLContent);
        HttpContext.Current.Response.End();
        HttpContext.Current.Response.Flush();
    }
  • 1

    What do you have in text? By the way, what is the impediment to using a library that generates a formatted document?

  • i pass a parameter pro metodo, text can be anything, in case there is a statement coming from a <Asp:Textbox>

  • and what would be the library in question, which generates the document?

  • 1

    I’m talking about something like this. Your schema works, but I see it more as an improv. It gets harder to format the document this way.

  • 1

    Okay I’m gonna take this library and implement it here, see how it behaves.

  • It has a procedure that saves in a specific directory, so much so that I can not point to an existing directory on my drive; but by what seems to be is very simple and useful. I’ll test at home. Thank you very much!

Show 1 more comment

1 answer

2


With the help of gypsy-Morrison-Mendez and using the library Docx v1.0.0.14 I solved the problem as follows:

    public static void gerarRelatorioAtoDoc(string texto)
    {
        string fileName = @"C:\Users\seu-nome\DocXExample.docx";
        var doc = DocX.Create(fileName, DocumentTypes.Document);
        doc.InsertParagraph(texto);
        doc.Save();
        Process.Start("WINWORD.EXE", fileName);
    }

The text parameter can be anything, in the above case it receives the contents of a Textbox.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.