1
resposta

Exportar

Boa tarde, como eu poderia exportar esse documento para Word? obrigado

1 resposta

Boa noite, Guilherme.

Se você tiver o MS-Word instalado na máquina que vai rodar a aplicação/c[odigo, então pode seguir o seguinte passo-a-passo, inspirado no link https://www.c-sharpcorner.com/UploadFile/muralidharan.d/how-to-create-word-document-using-C-Sharp/ :

Etapa 1: Crie um aplicativo simples do Windows e coloque um controle de botão nele.

Etapa 2: clique duas vezes no controle do botão e vá para a janela de código.

Etapa 3: adicione uma referência para "Microsoft.Office.Interop.Word", via Visual Studio, clicando com botão direito no projeto e adicionando uma referência (essa DLL não vai aparecer se você não tiver o MS-Word instalado).

Etapa 4: copie e cole o código que está no link acima para gerar o documento do Word.

private void button1_Click(object sender, EventArgs e)  
......
//Create document method  
private void CreateDocument()  
{  
    try  
    {  
        Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();  

        winword.ShowAnimation = false;  

        winword.Visible = false;  

        object missing = System.Reflection.Missing.Value;  

        Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);  

        foreach (Microsoft.Office.Interop.Word.Section section in document.Sections)  
        {  
            Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;  
            headerRange.Fields.Add(headerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage);  
            headerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;  
            headerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;  
            headerRange.Font.Size = 10;  
            headerRange.Text = "Header text goes here";  
        }  

        foreach (Microsoft.Office.Interop.Word.Section wordSection in document.Sections)  
        {  
            //Get the footer range and add the footer details.  
            Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;  
            footerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;  
            footerRange.Font.Size =10;  
            footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;  
            footerRange.Text = "Footer text goes here";  
          }  

        //adding text to document  
        document.Content.SetRange(0, 0);  
        document.Content.Text = "This is test document "+ Environment.NewLine;  

        Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);                  
        object styleHeading1 = "Heading 1";  
        para1.Range.set_Style(ref styleHeading1);                  
        para1.Range.Text = "Para 1 text";  
        para1.Range.InsertParagraphAfter();  

        Microsoft.Office.Interop.Word.Paragraph para2 = document.Content.Paragraphs.Add(ref missing);  
        object styleHeading2 = "Heading 2";  
        para2.Range.set_Style(ref styleHeading2);  
        para2.Range.Text = "Para 2 text";  
        para2.Range.InsertParagraphAfter();  

        Table firstTable = document.Tables.Add(para1.Range, 5, 5, ref missing, ref missing);  

        firstTable.Borders.Enable = 1;  
        foreach (Row row in firstTable.Rows)  
        {  
            foreach (Cell cell in row.Cells)  
            {  
                if (cell.RowIndex == 1)  
                {  
                    cell.Range.Text = "Column " + cell.ColumnIndex.ToString();  
                    cell.Range.Font.Bold = 1;  
                    //other format properties goes here  
                    cell.Range.Font.Name = "verdana";  
                    cell.Range.Font.Size = 10;  
                    //cell.Range.Font.ColorIndex = WdColorIndex.wdGray25;                              
                    cell.Shading.BackgroundPatternColor = WdColor.wdColorGray25;  
                    //Center alignment for the Header cells  
                    cell.Vert

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software