分类:
2008-11-28 22:15:47
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);
}
}
}