Melger

Melger

  • NA
  • 112
  • 0

Export textbox and datagridview to one word document.

Feb 4 2022 2:22 PM

Hi everyone, I'm fairly new in C# programming, I Igot 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;
        }

 

 


Answers (1)