Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i export the content of a datagridview to Microsoft Word with the Borderlines, column headers, correct data format. Looking forward to your favorable reply
Posted

Maybe this link Exporting DataGrid to Excel, Word and Text Files[^] will help you!

For winforms, the best thing I have seen to date is the True DB Grid Exporting example from ComponentOne Studio for Winforms.

I just tried it out now, and it even exports the images and background colours.

http://www.componentone.com/SuperProducts/StudioWinForms/[^]
Downfall is cost. As it is a 3rd Party Control.
Standard version is around $895!!

Also see http://www.eggheadcafe.com/community/aspnet/2/10225945/export-datagridview-to-word.aspx[^]

Another 3rd party control is available that does to excel and pdf
http://www.completit.com/Portfolio/DGVE/Features.aspx[^]
 
Share this answer
 
v2
Comments
LakshmiNarayana Nalluri 26-Apr-11 8:53am    
hi The Link which had you provided is useful for web application but not windows apps.
Rob Branaghan 26-Apr-11 10:24am    
Updated my post with a few more links.
Hi,

Paste code in button click you get your requirement.Don't forget mark as answer.Happy coding.
C#
Object oMissing = System.Reflection.Missing.Value;
            //OBJECTS OF FALSE AND TRUE
            Object oTrue = true;
            Object oFalse = false;

            //CREATING OBJECTS OF WORD AND DOCUMENT
           Microsoft.Office.Interop .Word.Application oWord = new Microsoft.Office.Interop .Word.Application();
            Microsoft.Office.Interop .Word.Document oWordDoc = new Microsoft.Office.Interop .Word.Document();

            //MAKING THE APPLICATION VISIBLE
            oWord.Visible = true;

            //ADDING A NEW DOCUMENT TO THE APPLICATION
            oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);


            //SETTING THE RANGE ON THE BOOKMARK
            Object oBookMarkName = "My_Inserted_Bookmark_On_Template";



            oWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop .Word.WdParagraphAlignment.wdAlignParagraphLeft;
                    oWord.Selection.Font.Bold = (int)Microsoft.Office.Interop .Word.WdConstants.wdToggle;
                    oWord.Selection.TypeText("Set Code:" + "        "+ "Set test:");
                    oWord.Selection.TypeParagraph();

                    oWord.Selection.TypeText("Set Description" + "        "+ " test Description ");



            oWord.Selection.TypeParagraph();
            oWord.Selection.TypeParagraph();


            Object start = Type.Missing;
            Object end = Type.Missing;
            Microsoft.Office.Interop.Word.Range rng = oWordDoc.Range(ref start, ref end);
            //ADD TABLE
            Microsoft.Office.Interop.Word.Table tbl = oWordDoc.Tables.Add(rng, dataGridView1.Rows.Count , dataGridView1 .Columns.Count , ref oMissing, ref oMissing);
            //END ADD TABLE

            Object defaultTableBehavior = Type.Missing;
            Object autoFitBehavior = Type.Missing;
            object missing = System.Type.Missing;

            Microsoft.Office.Interop.Word.Row newRow = oWordDoc.Tables[1].Rows.Add(ref missing);
            newRow.Range.Font.Bold = 0;
            newRow.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (i == dataGridView1.Rows.Count - 1)
                        MessageBox.Show("Successfully Exported");
                else
                {
                    for (int j = 0; j < dataGridView1.Columns.Count; j++)
                    {

                       tbl.Cell(i + 1, j + 1).Range.Text = dataGridView1.Rows[i].Cells[j].Value.ToString();
                    }
                }
            }
            oWord.Selection.TypeParagraph();
 
Share this answer
 
v2
Comments
DaveAuld 29-Apr-11 7:41am    
edit formatting.

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