Jayanta Mukherjee

Jayanta Mukherjee

  • NA
  • 120
  • 43.1k

How to export datagridview to docx using interop c#?

Apr 2 2021 2:17 PM

Hi,

I'm trying to export the selected rows of a datagridview to a docx file with a specific header when I hit a button (a text and a date time picker which writes the current date):

Also note, every time I hit the button after selecting rows of the datagridview, the data(i.e. table) should be added to a new page of that same docx file with the same header format and not overwrite data.

I've done:

  1. void Export2docClick(object sender, EventArgs e)    
  2. {    
  3.     //Table start.    
  4.     string html = "<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-size: 9pt;font-family:arial'>";    
  5.         
  6.     //Adding HeaderRow.    
  7.     html += "<tr><th colspan=3 style='background-color: #B8DBFD;border: 1px solid #ccc'>" + comboBox1.Text + "</th><tr>";    
  8.     foreach (DataGridViewColumn column in dataGridView1.Columns)    
  9.     {    
  10.         html += "<th style='background-color: #B8DBFD;border: 1px solid #ccc'>" + column.HeaderText + "</th>";    
  11.     }    
  12.     html += "</tr>";    
  13.         
  14.     //Adding DataRow.    
  15.     foreach (DataGridViewRow row in dataGridView1.SelectedRows.OfType<DataGridViewRow>().OrderBy(s=>s.Index))    
  16.     {    
  17.         html += "<tr>";    
  18.         foreach (DataGridViewCell cell in row.Cells)    
  19.         {    
  20.             html += "<td style='width:180px;border: 1px solid #ccc'>" + cell.Value.ToString() + "</td>";    
  21.         }    
  22.         html += "</tr>";    
  23.     }    
  24.         
  25.     //Table end.    
  26.     html += "</table>";    
  27.         
  28.     //Save the HTML string as HTML File.    
  29.     string htmlFilePath = @"F:\checking\DataGridView.htm";    
  30.     File.WriteAllText(htmlFilePath, html);    
  31.         
  32.     //Convert the HTML File to Word document.    
  33.     _Application word = new Microsoft.Office.Interop.Word.Application();    
  34.     _Document wordDoc = word.Documents.Open(FileName: htmlFilePath, ReadOnly: false);    
  35.         
  36.         
  37.     foreach (Microsoft.Office.Interop.Word.Section section in wordDoc.Sections)    
  38.     {    
  39.         //Get the header range and add the header details.    
  40.         Microsoft.Office.Interop.Word.Range headerRange = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;    
  41.         headerRange.Fields.Add(headerRange, WdFieldType.wdFieldPage);    
  42.         headerRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;    
  43.         headerRange.Font.ColorIndex = WdColorIndex.wdBlue;    
  44.         headerRange.Font.Size = 10;    
  45.         headerRange.Text = "Vizferma";    
  46.     }    
  47.         
  48.     wordDoc.SaveAs(FileName: @"F:\checking\DataGridView.docx", FileFormat: WdSaveFormat.wdFormatXMLDocument);    
  49.     ((_Document)wordDoc).Close();    
  50.     ((_Application)word).Quit();    
  51.         
  52.     //Delete the HTML File.    
  53.     File.Delete(htmlFilePath);    
  54.     MessageBox.Show("Done");    
  55. } 
How do I add the header part and also how do I make the code paste data in a new page instead of overwriting the file every time?

Furthermore, how do I sort the selected rows of the datagridview by say, column 2 values in ascending order?

Thanks in advance


Answers (3)