.NET导出Word

本文介绍了一种使用C#将DataGridView中的数据导出到Word文档的方法。通过创建Word应用程序对象和服务于DataGridView相同行数和列数的表格,可以将数据显示在Word中,并允许用户指定保存路径。
public static void ExportToWord(DataGridView dgv, ProgressBar progress, SaveFileDialog savefile)
{
    Microsoft.Office.Interop.Word.Document WordDoc = new Microsoft.Office.Interop.Word.Document();
    Microsoft.Office.Interop.Word.Table WordTable;
    object WordObj;
    if (dgv.Rows.Count == 0)
    {
        return;
    }
    else
    {
        savefile.AddExtension = true;
        savefile.DefaultExt = ".doc";
        savefile.CreatePrompt = true;
        savefile.Title = "导出文件保存路径";
        savefile.Filter = "Word files (*.doc)|*.doc";
        if (savefile.ShowDialog() == DialogResult.OK)
        {
            progress.Visible = true;
            object path = savefile.FileName;
            WordObj = System.Reflection.Missing.Value;
            //建立word对象
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            WordDoc = word.Documents.Add(ref WordObj, ref WordObj, ref WordObj, ref WordObj);
            //建立表格
            //将数据生成word表格文件
            WordTable = WordDoc.Tables.Add(WordDoc.Paragraphs.Last.Range, dgv.RowCount, dgv.ColumnCount, ref WordObj, ref WordObj);
            WordTable.Columns.SetWidth(50, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);

            try
            {
                for (int i = 0; i < dgv.Columns.Count; i++)//设置标题
                {
                    WordTable.Cell(0, i + 1).Range.Text = dgv.Columns[i].HeaderText;
                    WordTable.Cell(0, i + 1).Range.Font.Size = 5;

                }
                for (int i = 1; i < dgv.Rows.Count; i++)//填充数据
                {
                    for (int j = 0; j < dgv.Columns.Count; j++)
                    {
                        WordTable.Cell(i + 1, j + 1).Range.Text = dgv[j, i - 1].Value.ToString();
                        WordTable.Cell(i + 1, j + 1).Range.Font.Size = 5;
                    }
                    progress.Value += 100 / dgv.RowCount;
                }
                WordDoc.SaveAs(ref path, ref WordObj, ref WordObj, ref WordObj, ref WordObj, ref WordObj, ref WordObj, ref WordObj, ref WordObj, ref WordObj, ref WordObj, ref WordObj, ref WordObj, ref WordObj, ref WordObj, ref WordObj);
                WordDoc.Close(ref WordObj, ref WordObj, ref WordObj);
                progress.Value = 100;
                MessageBox.Show("数据已经成功导出到:" + savefile.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
                progress.Value = 0;
                progress.Visible = false;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "友情提示", MessageBoxButtons.OK);
            }
            finally
            {
                word.Quit(ref WordObj, ref WordObj, ref WordObj);
            }

        }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值