如何将winform datagrid导出到word landscape? [英] How to export winform datagrid to word landscape?

查看:82
本文介绍了如何将winform datagrid导出到word landscape?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中,我可以导出为A4大小的单词。数据网格中有许多标题,因此A4大小看起来不太好,因为标题是2行。如何以横向大小导出以使布局看起来容易阅读。



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.

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();
        }

推荐答案

你可以使用 Interop [ ^ ]或 OpenXML [ ^ ](如何:更改文字处理文档的打印方向(Open XML SDK) [ ^ ])。



提示:你需要添加表格在Word文档中,列和行的数量将等于DGV列/行。

Tables.Add - Interop [ ^ ]

使用Open XML SDK 2.0将表添加到Word 2010文档中 [ ^ ]
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[^]


这篇关于如何将winform datagrid导出到word landscape?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆