问题描述
点击button后,导出word文档。运行至:try{this._wordDocument.SaveAs(refmyFileName,refmyWordFormatDocument,refmyLockd,refmyPassword,refmyAddto,refmyPassword,refmyLockd,refmyLockd,refmyLockd,refmyLockd,refmyNothing,refmyNothing,refmyNothing,refmyNothing,refmyNothing,refmyNothing);}报错,求解源代码:protectedvoidButton1_Click(objectsender,EventArgse){CreateAWord();InsertText("ceshiceshiceshi",10,Microsoft.Office.Interop.Word.WdColor.wdColorBlack,0,Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter);NewLine();InsertText("啦啦啦啦啦啦",10,Microsoft.Office.Interop.Word.WdColor.wdColorBlack,0,Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter);NewLine();SaveWord("测试报告");}///<SUMMARY></SUMMARY>///word应用对象privateMicrosoft.Office.Interop.Word.Application_wordApplication;///<SUMMARY></SUMMARY>///word文件对象privateMicrosoft.Office.Interop.Word.Document_wordDocument;///<SUMMARY></SUMMARY>///创建文档///publicvoidCreateAWord(){//实例化word应用对象this._wordApplication=newMicrosoft.Office.Interop.Word.ApplicationClass();ObjectmyNothing=System.Reflection.Missing.Value;this._wordDocument=this._wordApplication.Documents.Add(refmyNothing,refmyNothing,refmyNothing,refmyNothing);}///<SUMMARY></SUMMARY>///添加页眉//////<PARAMname="pPageHeader"/>publicvoidSetPageHeader(stringpPageHeader){//添加页眉this._wordApplication.ActiveWindow.View.Type=Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;this._wordApplication.ActiveWindow.View.SeekView=Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);//设置中间对齐this._wordApplication.Selection.ParagraphFormat.Alignment=Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//跳出页眉设置this._wordApplication.ActiveWindow.View.SeekView=Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;}///<SUMMARY></SUMMARY>///插入文字//////<PARAMname="pText"/>文本信息///<PARAMname="pFontSize"/>字体大小///<PARAMname="pFontColor"/>字体颜色///<PARAMname="pFontBold"/>字体粗体///<PARAMname="ptextAlignment"/>方向publicvoidInsertText(stringpText,intpFontSize,Microsoft.Office.Interop.Word.WdColorpFontColor,intpFontBold,Microsoft.Office.Interop.Word.WdParagraphAlignmentptextAlignment){//设置字体样式以及方向this._wordApplication.Application.Selection.Font.Size=pFontSize;this._wordApplication.Application.Selection.Font.Bold=pFontBold;this._wordApplication.Application.Selection.Font.Color=pFontColor;this._wordApplication.Application.Selection.ParagraphFormat.Alignment=ptextAlignment;this._wordApplication.Application.Selection.TypeText(pText);}///<SUMMARY></SUMMARY>///换行///publicvoidNewLine(){//换行this._wordApplication.Application.Selection.TypeParagraph();}///<SUMMARY></SUMMARY>///插入一个图片//////<PARAMname="pPictureFileName"/>publicvoidInsertPicture(stringpPictureFileName){objectmyNothing=System.Reflection.Missing.Value;//图片居中显示this._wordApplication.Selection.ParagraphFormat.Alignment=Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName,refmyNothing,refmyNothing,refmyNothing);}///<SUMMARY></SUMMARY>///保存文件//////<PARAMname="pFileName"/>保存的文件名publicvoidSaveWord(stringpFileName){objectmyNothing=System.Reflection.Missing.Value;objectmyFileName=pFileName;objectmyWordFormatDocument=Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;objectmyLockd=false;objectmyPassword="";objectmyAddto=true;try{this._wordDocument.SaveAs(refmyFileName,refmyWordFormatDocument,refmyLockd,refmyPassword,refmyAddto,refmyPassword,refmyLockd,refmyLockd,refmyLockd,refmyLockd,refmyNothing,refmyNothing,refmyNothing,refmyNothing,refmyNothing,refmyNothing);}catch{thrownewException("导出word文档失败!");}}
解决方案
解决方案二:
太长了,看不下去。。。
解决方案三:
报什么错误?
解决方案四:
将Word文档数据导入到指定数据库中//执行导入数据privatevoidbtn_Begin_Click(objectsender,EventArgse){btn_Begin.Enabled=false;ThreadP_th=newThread(//创建线程()=>//使用Lambda表达式{while(true)//开始无限循环{this.Invoke(//在窗体线程中执行(MethodInvoker)(()=>//使用Lambda表达式{foreach(objectP_Oinlbox_Task.Items){TimeP_Time=(Time)P_O;//将对象转换为Time类型if(P_Time.Hours.ToString()==//判断时间是否相等DateTime.Now.Hour.ToString()&&P_Time.Minutes.ToString()==DateTime.Now.Minute.ToString()&&P_Time.Seconds.ToString()==DateTime.Now.Second.ToString()){if(P_Time.Execute)//判断任务是否已经执行{P_Time.Execute=false;//设置任务为已执行InsertData();//开始插入数据}}}if("0"==DateTime.Now.Hour.ToString()&&//是否重置任务"0"==DateTime.Now.Minute.ToString()&&"0"==DateTime.Now.Second.ToString()){foreach(objectP_1inlbox_Task.Items){((Time)P_1).Execute=true;}}//CodeGo.net/}));Thread.Sleep(1000);//线程挂起1秒钟}});P_th.IsBackground=true;//设置线程为后台线程P_th.Start();//线程开始执行}//向SQL数据库插入数据方法privatevoidInsertData(){try{G_wa.ActiveDocument.Save();//保存文档objectP_Save=false;//创建object对象((Word._Application)G_wa.Application).Quit(//退出应用程序refP_Save,refG_missing,refG_missing);}catch(Exceptionex){Console.WriteLine(ex.Message);}btn_Begin.Enabled=false;//停用插入数据按钮G_wa=newMicrosoft.Office.Interop.Word.Application();//创建应用程序对象objectP_Path=Directory.GetCurrentDirectory()+@"Word.doc";Word.DocumentP_Document=G_wa.Documents.Open(//打开Word文档refP_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);try{Word.RangeP_Range=//得到文档范围P_Document.Range(refG_missing,refG_missing);Word.TableP_Table=P_Range.Tables[1];//得到文档内的表格对象List<InstanceClass>P_List_InstanceClass=//建立集合对象newList<InstanceClass>();for(inti=2;i<7;i++){try{if(P_Table.Cell(i,1).Range.Text!="ra"&&//判断表格内是否已经添加信息P_Table.Cell(i,2).Range.Text!="ra"&&P_Table.Cell(i,3).Range.Text!="ra"&&P_Table.Cell(i,4).Range.Text!="ra"){P_List_InstanceClass.Add(//向数据集合中添加数据newInstanceClass(){Name=P_Table.Cell(i,1).Range.Text.Replace("ra",""),Chinese=float.Parse(P_Table.Cell(i,2).Range.Text.Replace("ra","")),Math=float.Parse(P_Table.Cell(i,3).Range.Text.Replace("ra","")),English=float.Parse(P_Table.Cell(i,4).Range.Text.Replace("ra",""))});}}catch(Exceptionex){MessageBox.Show(string.Format(//提示异常信息"Word表格中第{0}行数据不正确,插入失败!"+ex.Message,(i-1).ToString()),"错误!");}}newDataTier(txt_Server.Text,txt_DataBase.Text,txt_UserName.Text,//向SQL数据库中插入数据txt_PassWord.Text).InsertMessage(P_List_InstanceClass);objectP_Save=false;//创建object对象((Word._Application)G_wa.Application).Quit(//退出应用程序refP_Save,refG_missing,refG_missing);this.Invoke(//窗体线程执行(MethodInvoker)(()=>//使用Lambda表达式{dgv_Message.DataSource=//绑定数据库中的数据newDataTier(txt_Server.Text,txt_DataBase.Text,txt_UserName.Text,txt_PassWord.Text).GetMessage();MessageBox.Show("向SQL中插入数据成功!","提示!");}));}catch(Exceptionex){objectP_Save=false;//创建object对象((Word._Application)G_wa.Application).Quit(//退出应用程序refP_Save,refG_missing,refG_missing);thrownewException(ex.Message);//将异常抛向上一层}}
解决方案五:
你认为有人会看....比如googleitaspose.words...来的更快...
解决方案六:
贴代码太多了。。楼主可以试一下docx
解决方案七:
代码太多。我愿意看至少40%文字分析的问题。