Export textbox and datagridview data to a WORD document. All to the same document

Melger Stander 1 Reputation point
2022-02-16T08:09:22.967+00:00

I got a Windows Forms application using C# and struggling now for over a week trying to export some textboxes and a datagridview data to a word document. I'v managed to get the textboxes data over to word but cant get the datagridview data in to the same word document. I need both to be on the same document. Please help me with ideas... I'm loosing my mind here. Some Code and examples will be highly appreciated. Thank you in advance.

Code below does get my texbox data over to word but not the datagridview.

private void btnSaveReport_Click(object sender, EventArgs e)
{
ToViewFile(docxPath);

    document = new Spire.Doc.Document();
    document.LoadFromFile(samplePath);

    Dictionary<string, string> dictReplace = GetReplaceDictionary();

    foreach (KeyValuePair<string, string> kvp in dictReplace)
    {
        document.Replace(kvp.Key, kvp.Value, true, true);
    }

    document.SaveToFile(docxPath, FileFormat.Docx);
}

private void ToViewFile(string fileName)
{
    try
    {
        System.Diagnostics.Process.Start(fileName);
    }
    catch 
    { 
    }
}

Dictionary<string, string> GetReplaceDictionary()
{
    Dictionary<string, string> replaceDict = new Dictionary<string, string>();
    replaceDict.Add("#Date#", txtDate.Text.Trim());
    replaceDict.Add("#DataGrid#", "DataGridView Entries Here");
    replaceDict.Add("#Lead Tech#", txtLeadTech.Text.Trim());
    replaceDict.Add("#Gaming Security#", txtSecurity.Text.Trim());
    replaceDict.Add("#Surveillance#", txtSurv.Text.Trim());
    replaceDict.Add("#Comments#", txtComments.Text.Trim());
    replaceDict.Add("#Book#", txtBooks.Text.Trim());
    return replaceDict;
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,839 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sreeju Nair 12,346 Reputation points
    2022-02-16T15:34:09.237+00:00

    As per the code you shared, you are loading a word document from a file, and replace the values in the document with values in the controls. cool. Now I assume you have a datagridview, that needs to be written to the word file, correct? If yes let me give you some pointers.

    1. You can use the Rows property of datagridview to loop through each row in the gridview. e.g. below foreach (DataGridViewRow row in datagridviews.Rows)
      {
      // You will get each cell data by using the code
      var cell1= row.Cells["cell name 1"].Value
      }
    2. Since you are using Spire.Doc, you can refer the tutorials in spire to write the data to table in word document. Refer the following example
      https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/NET-Create-Table-Create-Word-Table-in-C-and-VB.NET.html

    So in the loop in step 1, use the word table creation.

    Hope this helps

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.