问题描述
publicboolCreateWordFile(string_filename,"数据List或者你C#要写的数据"){#region开始生成Wordtry{stringstrtitle="任务导出";objectoEndOfDoc="\endofdoc";ObjectNothing=System.Reflection.Missing.Value;Objectfilename=_filename;//创建Word文档Microsoft.Office.Interop.Word.ApplicationWordApp=newMicrosoft.Office.Interop.Word.ApplicationClass();Microsoft.Office.Interop.Word.DocumentWordDoc=WordApp.Documents.Add(refNothing,refNothing,refNothing,refNothing);//设置页眉WordApp.ActiveWindow.View.Type=Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;WordApp.ActiveWindow.View.SeekView=Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("WPFOA任务导出");WordApp.Selection.ParagraphFormat.Alignment=Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐WordApp.ActiveWindow.View.SeekView=Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//跳出页眉设置//任务导出------名字Word.ParagraphoPara1;oPara1=WordDoc.Content.Paragraphs.Add(refNothing);oPara1.Range.Text=strtitle;oPara1.Range.Font.Bold=1;oPara1.Range.Font.Name="宋体";oPara1.Range.Font.Size=20;oPara1.Range.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter;oPara1.Format.SpaceAfter=5;//24ptspacingafterparagraph.oPara1.Range.InsertParagraphAfter();#region循环每个表foreach(varvinlst_task){#region循环每一个列,产生一行数据//描述信息Word.ParagraphoPara3;objectoRng=WordDoc.Bookmarks.get_Item(refoEndOfDoc).Range;oPara3=WordDoc.Content.Paragraphs.Add(refoRng);oPara3.Range.Text="";oPara3.Range.Font.Bold=0;oPara3.Range.Font.Name="宋体";oPara3.Range.Font.Size=9;oPara3.Range.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter;oPara3.Format.SpaceBefore=1;oPara3.Format.SpaceAfter=1;oPara3.Range.InsertParagraphAfter();//插入表格Word.TablenewTable;Word.RangewrdRng=WordDoc.Bookmarks.get_Item(refoEndOfDoc).Range;newTable=WordDoc.Tables.Add(wrdRng,5,4,refNothing,refNothing);newTable.Columns[1].Width=60;newTable.Columns[2].Width=145;newTable.Columns[3].Width=80;newTable.Columns[4].Width=145;newTable.Borders.OutsideLineStyle=Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;newTable.Borders.InsideLineStyle=Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;//填充表格内容newTable.Cell(1,1).Range.Text="项目名称";newTable.Cell(1,2).Range.Text="1";newTable.Cell(1,3).Range.Text="模块名称";newTable.Cell(1,4).Range.Text="2";newTable.Cell(2,1).Range.Text="负责人名";newTable.Cell(2,2).Range.Text="3";newTable.Cell(2,3).Range.Text="状态";newTable.Cell(2,4).Range.Text="4";newTable.Cell(3,1).Range.Text="发布时间";newTable.Cell(3,2).Range.Text="5";newTable.Cell(3,3).Range.Text="要求完成时间";newTable.Cell(3,4).Range.Text="6";newTable.Cell(4,1).Range.Text="完成时间";newTable.Cell(4,2).Range.Text="7";newTable.Cell(4,3).Range.Text="分配人";newTable.Cell(4,4).Range.Text="8";newTable.Cell(5,1).Range.Text="任务描述";newTable.Cell(5,2).Merge(newTable.Cell(5,4));newTable.Cell(5,2).Range.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphLeft;newTable.Cell(5,2).Range.Text="12313213123213";#endregion}WordDoc.SaveAs(reffilename,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing);WordDoc.Close(refNothing,refNothing,refNothing);WordApp.Quit(refNothing,refNothing,refNothing);#endregion//关闭WinWord进程System.Diagnostics.Process[]MyProcess=System.Diagnostics.Process.GetProcessesByName("WINWORD");MyProcess[0].Kill();returntrue;}catch{System.Diagnostics.Process[]MyProcess=System.Diagnostics.Process.GetProcessesByName("WINWORD");MyProcess[0].Kill();returnfalse;}#endregion}看看哪有问题
解决方案
解决方案二:
解决方案三:
需要电脑上安装好Office,并且引用的库正确。一般这种你直接粘贴来的代码,本身应该基本可以,就看你会不会用了。
解决方案四:
C#创建word文档示例privateobjectG_missing=//定义G_missing字段并添加引用System.Reflection.Missing.Value;privateFolderBrowserDialogG_FolderBrowserDialog;//定义浏览文件夹字段privateobjectG_str_path;//定义文件保存路径字段privatevoidbtn_New_Click(objectsender,EventArgse){btn_New.Enabled=false;//将新建按钮设置为不可用ThreadPool.QueueUserWorkItem(//开始线程池(pp)=>//使用lambda表达式{G_wa=newMicrosoft.Office.Interop.Word.Application();//创建应用程序对象objectP_obj="Normal.dot";//定义文档模板Word.DocumentP_wd=G_wa.Documents.Add(//向Word应用程序中添加文档refP_obj,refG_missing,refG_missing,refG_missing);G_str_path=string.Format(//计算文件保存路径@"{0}{1}",G_FolderBrowserDialog.SelectedPath,DateTime.Now.ToString("yyyy年M月d日h时s分m秒fff毫秒")+".doc");P_wd.SaveAs(//保存Word文件refG_str_path,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing);((Word._Application)G_wa.Application).Quit(//退出应用程序refG_missing,refG_missing,refG_missing);this.Invoke(//调用窗体线程(MethodInvoker)(()=>//使用lambda表达式{//codego.net/tags/1/1/MessageBox.Show(//提示已经创建Word"成功创建Word文档!","提示!");btn_display.Enabled=true;//启用显示按钮}));});}