Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


What I have tried:

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;
    }
Posted
Updated 16-Feb-22 5:13am
Comments
Richard MacCutchan 4-Feb-22 11:06am    
A DataGridView is just a visula representation of some data. So you need to structure that data into a format that you can then add to your document. It is not clear why you are using Key Value pairs for the text, or why you cannot do that for the other data.
Melger Stander 16-Feb-22 2:47am    
Thank you for replying. I'm new in C# and not coming right with this problem. Is there a other way for export my windows form data to a word document? I just found the key value example on a code group. No specific reason for using it.
Richard MacCutchan 16-Feb-22 3:26am    
It is not clear exactly what you are trying to do. What is the data that you are trying to export? What library or interface are you using in the export process?
Melger Stander 16-Feb-22 3:35am    
I got a windows application using C#. In my form I got texboxes and a datagridview. I need to create a report. I'm trying to export it to a word document. If there is s better reporting way please point me in the right direction. As I said I'm new in this and still learning better and correct ways. Thank you
Richard MacCutchan 16-Feb-22 3:50am    
The use of TextBoxes and DataGridViews is irrelevant; they are only used in your Windows Form. You need to decide first how you want your data to appear in the Word document, and how you are going to export it. Until you can answer those questions it is difficult to suggest anything.

If you just want to generate a report from your application then take a look at Crystal Reports, and/or Microsoft's own reporting tools.

1 solution

Assuming that DataGridView is bounded with datasource, like DataTable:

private void btnSaveReport_Click(object sender, EventArgs e)
{
    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);
    }
    
    DataTable dt = DataGridView1.DataSource as DataTable;
    foreach(DataRow dr in dt.Rows)
    {
        //here add code with your logic      
    }

    document.SaveToFile(docxPath, FileFormat.Docx);

    ToViewFile(docxPath);

    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900