问题描述
小弟再做个word文档导出,要求可以根据模版导出多条数据放在一个word中,但是替换标签时出现了字符串过长的问题有没有别的替换方式呢stringtemplateFile="E:\dsc\wwwroot\download\导出模版.doc";stringfileNameWord="E:\dsc\wwwroot\download\temp\20140804110822.doc";DataTabledt=getDataTable();boolret=false;Microsoft.Office.Interop.Word.Applicationapp=newMicrosoft.Office.Interop.Word.Application();Microsoft.Office.Interop.Word.Documentdoc=newMicrosoft.Office.Interop.Word.Document();objectObj_FileName=fileNameWord;objectVisible=false;objectReadOnly=false;objectmissing=System.Reflection.Missing.Value;try{File.Copy(templateFile,fileNameWord,true);doc=app.Documents.Open(refObj_FileName,refmissing,refReadOnly,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refVisible,refmissing,refmissing,refmissing,refmissing);doc.Activate();doc.Tables[1].Select();//复制第一个表格,如果有多条粘贴到尾部app.Selection.Copy();//如果导入多条要把原来的模版粘贴下来for(inti=0;i<dt.Rows.Count;i++){objectreplaceArea=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;for(intj=0;j<dt.Columns.Count;j++){objectreplaceKey="$"+dt.Columns[j].ToString()+"$";objectreplaceValue=dt.Rows[i][j].ToString();doc.Tables[i+1].Range.Find.Execute(refreplaceKey,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refreplaceValue,refreplaceArea,refmissing,refmissing,refmissing,refmissing);}if(i+1!=dt.Rows.Count)//这里是导入多条把刚才的模版粘贴到尾部{objectmymissing=System.Reflection.Missing.Value;objectmyunit=Microsoft.Office.Interop.Word.WdUnits.wdStory;app.Selection.EndKey(refmyunit,refmymissing);objectpBreak=(int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;app.Selection.TypeParagraph();app.Selection.Paste();}}doc.Save();ret=true;}catch(Exception){}finally{objecto=false;doc.Close(refo,refmissing,refmissing);app.Quit(refo,refmissing,refmissing);}returnret;
解决方案
解决方案二:
在模板word里定义书签,然后要导出的时候,从模板copy一份,打开,在书签里插入内容,就OK了而不要把之前里面已经存在的内容复制来复制去的
解决方案三:
引用1楼Z65443344的回复:
在模板word里定义书签,然后要导出的时候,从模板copy一份,打开,在书签里插入内容,就OK了而不要把之前里面已经存在的内容复制来复制去的
嗯这个我这里要导出多条数据而不是单条数据,多条数据要求多个表格,书签的方法导出单条的好用
解决方案四:
可以设置多个书签
解决方案五:
namespaceExmaple{publicpartialclassGvExprot2:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){//getData();}//publicvoidgetData()//{//////创建连接对象cn,准备好连接//SqlConnectioncn=newSqlConnection("DataSource=.;InitialCatalog=Person;IntegratedSecurity=True");//////创建连接对象sqlcommand命令对象,准备好命令 //SqlCommandcmd=newSqlCommand("SELECT*FROM[TStudent]",cn);////创建适配器对象da,其作用是真正的向数据库提交查询或更新//SqlDataAdapterda=newSqlDataAdapter(cmd);//////创建数据集对象ds,实质是建立内存与数据库的一个映射//DataSetds=newDataSet();//////创建数据集对象ds,实质是建立内存与数据库的一个映射//da.Fill(ds,"[TStudent]");////this.gvPersonList.DataSource=ds.Tables[0].DefaultView;//////给数据源赋值 //this.gvPersonList.DataSource=ds;////绑定数据//this.gvPersonList.DataBind();//}///<summary>///导出数据函数///</summary>///<paramname="FileType">导出文件MIME类型</param>///<paramname="FileName">导出文件的名称</param>privatevoidExprot(StringFileType,StringFileName){//恢复分页gvPersonList.AllowPaging=false;//为GridView重新绑定数据源gvPersonList.DataBind();//getData();Response.Clear();Response.BufferOutput=true;//设定输出字符集Response.Charset="GB2312";Response.ContentEncoding=System.Text.Encoding.UTF8;Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8));//设置输出流HttpMiME类型(导出文件格式)Response.ContentType=FileType;//关闭ViewStatePage.EnableViewState=false;System.Globalization.CultureInfocultureInfo=newSystem.Globalization.CultureInfo("ZH-CN",true);System.IO.StringWriterstringWriter=newSystem.IO.StringWriter(cultureInfo);HtmlTextWritertextWriter=newHtmlTextWriter(stringWriter);gvPersonList.RenderControl(textWriter);//把HTML写回游览器Response.Write(stringWriter.ToString());Response.End();Response.Flush();//恢复分页gvPersonList.AllowPaging=true;//为GridView重新绑定数据源gvPersonList.DataBind();//getData();}//导出ExcelprotectedvoidbtnToExcel_Click(objectsender,EventArgse){Exprot("application/ms-excel","Employee.xls");}//导出WordprotectedvoidbtnToWord_Click(objectsender,EventArgse){//这里将导出的方法抽取了出来使用Exprot("application/ms-word","Employee.doc");}//确认在运行时为指定的ASP.NET服务器控件呈现在HtmlForm控件中。//(检验Asp.Net服务器空间是否呈现在HTMLForm控件中)publicoverridevoidVerifyRenderingInServerForm(Controlcontrol){#region/*对程序说明,在asp.net1.1中由于对控件呈现不是很严格,*所以无需overridevoidVerifyRenderingInServerForm(Controlcontrol)这个方法*但在asp.net2.0中,控件的校验严格了,RenderControl代码只有走正常流程在render方法*中它自己调用才能成功,在你自己写的事件方法中调用就会出现这个错误。这个错误信息有点误导,*你明明写在服务器控件Form内,它照样会这样提醒你,*实际上是asp.net2.0设置了内部变量控制RenderControl不允许在Render方法之外被轻易调用。*如果不overrideVerifyRenderingInServerForm就会报错。*我们overridevoidVerifyRenderingInServerForm(Controlcontrol)这个方法,*里面不写任何代码即可*///base.VerifyRenderingInServerForm(control);#endregion}///<summary>///分页///</summary>protectedvoidgvPersonList_PageIndexChanging(objectsender,GridViewPageEventArgse){gvPersonList.PageIndex=e.NewPageIndex;//BindGridView();}}}
解决方案六:
引用3楼Z65443344的回复:
可以设置多个书签
嗯我的模版是这样的如果这个数据是多条的话程序需要copy模版中的表格粘贴到文档的尾部,这个是copy过的替换书签的方法好像,书签不能重名
解决方案七:
将多个word文档导出到一个word文档privateWord.ApplicationG_wa;//定义Word应用程序privateobjectG_missing=//定义G_missing字段并添加引用System.Reflection.Missing.Value;privateOpenFileDialogG_OpenFileDialog;//定义打开文件对话框privateSaveFileDialogG_SaveFileDialog;//定义保存文件对话框privateList<string>G_Str_Files=newList<string>();//定义字符串集合privatevoidbtn_split_Click(objectsender,EventArgse){btn_Merge.Enabled=false;//停用合并按钮ThreadPool.QueueUserWorkItem(//开始线程池(pp)=>//使用lambda表达式{G_wa=newMicrosoft.Office.Interop.Word.Application();//创建应用程序对象Word.DocumentP_MainDocument=//新建合并文档对象G_wa.Documents.Add(refG_missing,refG_missing,refG_missing,refG_missing);foreach(stringP_StrinG_Str_Files)//遍历文档的集合{objectP_strs=P_Str;//创建object对象Word.DocumentP_Document=G_wa.Documents.Open(//打开Word文档refP_strs,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,refo);Word.RangeP_Range_temp=//得到文档全部范围P_Document.Range(refG_missing,refG_missing);P_Range_temp.Select();//选择文档全部范围P_Range_temp.Copy();//复制文档全部范围Word.RangeP_Range_temp2=//得到文档的范围P_MainDocument.Range(refG_missing,refG_missing);objectP_end=Word.WdCollapseDirection.wdCollapseEnd;//创建object对象P_Range_temp2.Collapse(refP_end);//折叠文档范围P_Range_temp2.Select();//选择档的最后位置P_Range_temp2.Paste();//粘贴文档内容((Word._Document)P_Document).Close(refG_missing,refG_missing,//关闭文档refG_missing);}objectP_SavePath=G_SaveFileDialog.FileName;//创建object对象P_MainDocument.SaveAs(//保存合并后的文档refP_SavePath,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表达式{Clipboard.Clear();//清空剪切板MessageBox.Show(//提示已经创建Word"成功合并Word文档!","提示!");btn_Merge.Enabled=true;//启用合并按钮}));});}