Chinaunix首页 | 论坛 | 博客
  • 博客访问: 373074
  • 博文数量: 94
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 823
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-04 16:49
文章分类

全部博文(94)

文章存档

2015年(1)

2011年(1)

2010年(3)

2008年(8)

2007年(55)

2006年(26)

我的朋友

分类:

2008-11-28 22:15:47

首先要添加引用:
在 解决方案资源管理器 的 引用 中添加,选择 COM 选项卡,引入“Microsoft Word 11.0 Object Library”;
然后添加命名空间:“using System.Reflection;”和“using Word;”。
使用方法参考下面的例子:

private void buttonPrint1_Click(object sender, EventArgs e)

{

    SaveFileDialog saveFileDialog = new SaveFileDialog();

    saveFileDialog.Filter = @"(*.doc)|*.doc";

    saveFileDialog.DefaultExt = ".doc";

    saveFileDialog.FilterIndex = 0;

    saveFileDialog.RestoreDirectory = true;

    saveFileDialog.Title = "导出造林方案";

    if (saveFileDialog.ShowDialog() == DialogResult.OK)

    {

        try

        {

            object oDefault = System.Reflection.Missing.Value;

            object oFilePath = saveFileDialog.FileName;

            object oWordExit = WdSaveOptions.wdSaveChanges;

            // Word文档导出

            object oMissing = System.Reflection.Missing.Value;

            object oEndOfDoc = "\\endofdoc";

            Word._Application oWord;

            Word._Document oDoc;

            oWord = new Word.Application();

            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            //插入标题 //

            Word.Paragraph oParaTitle;

            oParaTitle = oDoc.Content.Paragraphs.Add(ref oMissing);

            oParaTitle.Range.Font.Size = 18;

            oParaTitle.Range.Font.Bold = 1;

            oParaTitle.Range.Text = "造林方案设计";

            oParaTitle.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            oParaTitle.Format.SpaceAfter = 18;

            oParaTitle.Range.InsertParagraphAfter();

            //插入空白

            Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;

            //

            Word.Paragraph oParaDesign;

            oParaDesign = oDoc.Content.Paragraphs.Add(ref oMissing);

            oParaDesign.Range.Font.Bold = 0;

            oParaDesign.Range.Font.Size = 14;

            oParaDesign.Range.Text = "基本方案:";

            oParaDesign.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;

            oParaDesign.Format.SpaceBefore = 12;

            oParaDesign.Format.SpaceAfter = 6;

            oParaDesign.Format.LeftIndent = 0;

            oParaDesign.Range.InsertParagraphAfter();

            //注意下面语句的顺序,这样才能控制好格式(如果没有新的格式控制语句,则word中自动使用上一行的格式)

            Word.Paragraph oParaDesignContent;

            oParaDesignContent = oDoc.Content.Paragraphs.Add(ref oMissing);

            oParaDesignContent.Range.Font.Size = 10.5f;

            oParaDesignContent.Range.Text = "推荐树种:" + recommandSpecies[0];

            oParaDesignContent.Format.SpaceBefore = 6;

            oParaDesignContent.Format.LeftIndent = 21f;

            oParaDesignContent.Range.Text += "\n推荐培育目标:" + labelTJMB1.Text;

            oParaDesignContent.Range.Text += "\n初值密度:" + labelMD1.Text;

            oParaDesignContent.Range.Text += "\n株行距:" + labelMD_ZHJ1.Text;

            oParaDesignContent.Range.InsertParagraphAfter();

 

            oDoc.SaveAs(ref oFilePath, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault);

            //关闭WordDoc文档对象

            oDoc.Close(ref oWordExit, ref oDefault, ref oDefault);

            //关闭WordApp组件对象

            oWord.Quit(ref oWordExit, ref oDefault, ref oDefault);

            MessageBox.Show("造林方案导出成功!", "提示");

        }

        catch

        {

            MessageBox.Show("造林方案导出失败!\n提示:请先关闭您的word应用程序后再使用导出功能!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

    }

}

 

其他参考文章:

http://blog.sina.com.cn/s/blog_4b50ff730100acg8.html

 

阅读(1744) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~