Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the following code I am able to export to word in A4 size. There are many headers in datagrid and so the A4 size does not look nice because the headers are in 2 lines. How to export in landscape size to make the layout look easily readable.

C#
private void btnPrint_Click(object sender, EventArgs e)
        {
         //   printDocument1.Print();
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Word Documents (*.doc)|*.doc";
            sfd.FileName = "time.doc";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                //ToCsV(dataGridView1, @"c:\export.xls");
                ToCsV(gvUserInfo, sfd.FileName); // Here dataGridview1 is your grid view name
            }
        }

        private void ToCsV(DataGridView dGV, string filename)
        {
            string stOutput = "";
            // Export titles:
            string sHeaders = "";
            for (int j = 0; j < dGV.Columns.Count; j++)
                sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
            stOutput += sHeaders + "\r\n";
            // Export data.
            for (int i = 0; i < dGV.RowCount - 1; i++)
            {
                string stLine = "";
                for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
                    stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";
                stOutput += stLine + "\r\n";
            }
            Encoding utf16 = Encoding.GetEncoding(1254);
            byte[] output = utf16.GetBytes(stOutput);
            FileStream fs = new FileStream(filename, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(output, 0, output.Length); //write the encoded file
            bw.Flush();
            bw.Close();
            fs.Close();
        }
Posted
Updated 28-Apr-14 3:35am
v2
Comments
CHill60 28-Apr-14 9:38am    
That just looks to be plain text, not Word format, so you are not going to be able to change to landscape programatically
S.Rajendran from Coimbatore 28-Apr-14 10:54am    
How to for word format?
CHill60 28-Apr-14 11:57am    
Best bet is to search CodeProject articles (see top right corner of page) for "create Word document c#" ... it depends on exactly how you want to do it, but that search should give you plenty of examples to get started

1 solution

You can do it by using Interop[^] or OpenXML[^] (How to: Change the print orientation of a word processing document (Open XML SDK)[^]).

Tip: you need to add table to the Word document which count of columns and rows will be equal to DGV columns/rows.
Tables.Add - Interop[^]
Adding Tables to Word 2010 Documents by Using the Open XML SDK 2.0[^]
 
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