回家前一个晚上,老板临时安排让写一个读取txt文件的数据和多个图片数据生成一个word文档。时间给得太短只能是回家继续加班,百度真是个好东西这里引用一下我参考的博客http://blog.csdn.net/jiutao_tang/article/details/6574740/
http://xuzhihong1987.blog.163.com/blog/static/2673158720109188465634/ 这个博客讲的用录制word宏的方法做参考很有用。感谢这位博友
静下心,认真做,不抱怨,总能把事解决。不扯了,直接进入正题
1.在开发word程序时一定要引用这个库:
using MSWord = Microsoft.Office.Interop.Word;
2.新建一个空word文档
MSWord.Application wordApp = null;
MSWord.Document wordDoc = null;
wordApp = new MSWord.ApplicationClass();
wordDoc = wordApp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);
3.给word文档中添加文字
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;//设置字体居中
wordApp.Selection.Font.Size = 30;//设置字体大小
wordApp.Selection.Font.Bold = 2;//设置字体为粗体
wordApp.Selection.TypeText(this.RouteTxtBox.Text + "分析报告");
wordApp.Selection.TypeParagraph(); //换行
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;//设置字体左对齐,以后的文字都是左对齐
wordApp.Selection.Font.Size = 20;
wordApp.Selection.Font.Bold = 2;
wordApp.Selection.TypeText("1.基本信息");
wordApp.Selection.TypeParagraph();
wordApp.Selection.Font.Size = 10;
wordApp.Selection.Font.Bold = 0;//设置字体正常,不加粗
wordApp.Selection.TypeText(" 名称: " + this.RouteTxtBox.Text);
wordApp.Selection.TypeParagraph();
wordApp.Selection.TypeText(" 等级:" + this.KVtextBox.Text);
wordApp.Selection.TypeParagraph();
wordApp.Selection.TypeText(" 时间: " + this.TimeTxtBox.Text);
wordApp.Selection.TypeParagraph();
wordApp.Selection.TypeText(" 标准:");
wordApp.Selection.TypeParagraph();
4.读取txt文件中的数据(给表格中写)
OpenFileDialog opentxtDlg = n