.net 创建Word文件

Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = true;
                Microsoft.Office.Interop.Word.Document oDoc;
                object oMissing = System.Reflection.Missing.Value;
                object Range = System.Reflection.Missing.Value;
                object strFileName = "D:\\wwwroot\\test.doc";
                if (File.Exists((string)strFileName))
                    File.Delete((string)strFileName);

                //Ceate An New Word创建一个新的word文档
                oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                //oWord.Selection.InlineShapes.AddPicture("E:\\Project.Dev\\dev.cheua\\Lawyer\\Web\\upload\\name\\1.jpg", ref oMissing, ref oMissing, ref Range);
               
                oDoc.Paragraphs.First.Range.Text = "Create An New Word!\n\r";
                oDoc.SaveAs(ref strFileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                oDoc.Paragraphs.First.Range.Text = "Create An New Word!\n\r";
                oDoc.Paragraphs.First.Range.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineDash;

                oDoc.Paragraphs.Last.Range.Application.Selection.InlineShapes.AddPicture("E:\\Project.Dev\\dev.cheua\\Lawyer\\Web\\upload\\name\\1.jpg", ref oMissing, ref oMissing, ref Range);

                oWord.Application.Selection.TypeText("Create An New Word!");
                oWord.Application.Selection.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineSingle; //设置下划线
               



                oDoc.SaveAs(ref strFileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                oWord.Quit(ref oMissing, ref oMissing, ref oMissing);

 

posted @ 2021-10-30 20:23  Chanwah  阅读(177)  评论(0编辑  收藏  举报