【C# word多页应用单页模板问题】 我现在有一个一页的dot模板,需要生成一份word文档,文档的页数和内容根据数据库记录填充,每页都需要应用这个dot模板,这个如何实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Microsoft.Office.Interop.Word.dll找找关于它的资料看看. Microsoft.Office.Interop.Word.dll 每次將模板讀取來new 一次,然后設定好后保存為其他你需要的文件名不過WebForm中New Word實例.全部都是在服務器端的內存中生成,多人使用的時候,服務器負荷不起哦考慮把模板存放在DB中,每次使用從DB讀取? 我是用Microsoft.Office.Interop.Word.dll可是一般都只是打开模板,进行编辑,保存为doc。一个dot对应一个doc,无法将一个dot反复应用到每一页。 复制模板到另一文件夹,再操作public string JBHJ(System.Web.UI.Page page,DataSet ds,object FileName,string JIE,string CI) { string strM=""; object nothing=System.Reflection.Missing.Value; object optional=System.Reflection.Missing.Value; object visible=true; object saveChanges = true; object NOTsaveChanges = false; object docreadonly=true; object originalFormat = System.Reflection.Missing.Value; object routeDocument =System.Reflection.Missing.Value; Word.ApplicationClass app=new Word.ApplicationClass(); object FileNa=page.Server.MapPath("../Template_temp/"+FileName); Word.Document Doc=app.Documents.Open(ref FileNa,ref optional,ref docreadonly,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional, ref visible); int index=1; try { Doc.Paragraphs.First.Range.Text=""; Doc.Paragraphs.First.Range.Font.Size=18; Doc.Paragraphs.First.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter; Doc.Paragraphs.First.Range.Font.Bold=5; Doc.Paragraphs.Add(ref nothing); Doc.Paragraphs.Item(2).Range.Text=""; Doc.Paragraphs.Item(2).Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter; Doc.Paragraphs.Item(2).Range.Font.Size=17; Word.Paragraph ph=Doc.Paragraphs.Add(ref nothing); ph.Range.ParagraphFormat.FirstLineIndent-=1; ph.Range.Font.Size=15; ph.Range.Font.Bold=2; ph.Alignment=Word.WdParagraphAlignment.wdAlignParagraphLeft; Word.Table tb=ph.Range.Tables.AddOld(ph.Range ,1,1); tb.Rows.Alignment=Word.WdRowAlignment.wdAlignRowCenter; tb.Range.Font.Size=15; tb.Range.Font.Bold=2; tb.Cell(1,1).Range.Text=""; if(ds.Tables[0].Rows.Count >0) { for(int i=0;i<ds.Tables[0].Rows.Count ;i++) { Word.Row row=tb.Rows.Add(ref nothing); row.Alignment=Word.WdRowAlignment.wdAlignRowCenter; row.Range.Font.Size=13; row.Range.Font.Bold=0; index++; tb.Cell(index,1).Range.Text=ds.Tables[0].Rows[i]["MC"].ToString(); } } object strFi=page.Server.MapPath(@"../Template_temp/A.doc"); Doc.SaveAs(ref strFi,ref optional, ref optional, ref optional,ref optional, ref optional, ref optional,ref optional, ref optional, ref optional, ref optional); Doc.Close(ref nothing,ref nothing,ref nothing); app.Quit(ref NOTsaveChanges, ref originalFormat, ref routeDocument); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); System.Runtime.InteropServices.Marshal.ReleaseComObject(Doc); app=null; Doc=null; GC.Collect(); GC.Collect(); } catch(Exception err) { strM=err.Message+",导出失败!"; return strM; } return ""; } 感谢wuyq11。和我的想法一样,循环复制过去之后插入一个回车。不过有一个问题,我的模板里面有书签,为了在某些地方去加入数据内容,单纯复制过去书签是不过去的(就算过去书签名也是个问题呵呵),后来我只好每个循环内打开模板,插入书签内容,复制,关闭模板,再打开模板,插入书签...不过效率有点低。不知有无更好的办法? see see 。maybe has useful 一个windowservice中有多个service怎么同时调用 通过月份判断出这月的天数? 有關發短信問題 怎么表示一个公路网的道路信息?如果路都是直线就直接用坐标,可是曲线怎么办? SQL查询时有些字段不能被识别?为什么? DataGridView问题 ActiveX打包成CAB问题! C#微信接口信息回复 请问在C#中如何将当前系统时间化为UNIX时间? 关于VS.NET的安装!!! FileUpload如何显示原来保存的路径? Windows Media Player播放流媒体如何进行下载?
找找关于它的资料看看.
每次將模板讀取來new 一次,然后設定好后保存為其他你需要的文件名
不過WebForm中New Word實例.全部都是在服務器端的內存中生成,多人使用的時候,服務器負荷不起哦
考慮把模板存放在DB中,每次使用從DB讀取?
public string JBHJ(System.Web.UI.Page page,DataSet ds,object FileName,string JIE,string CI)
{
string strM="";
object nothing=System.Reflection.Missing.Value;
object optional=System.Reflection.Missing.Value;
object visible=true;
object saveChanges = true;
object NOTsaveChanges = false;
object docreadonly=true;
object originalFormat = System.Reflection.Missing.Value;
object routeDocument =System.Reflection.Missing.Value;
Word.ApplicationClass app=new Word.ApplicationClass();
object FileNa=page.Server.MapPath("../Template_temp/"+FileName);
Word.Document Doc=app.Documents.Open(ref FileNa,ref optional,ref docreadonly,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional, ref visible);
int index=1;
try
{
Doc.Paragraphs.First.Range.Text="";
Doc.Paragraphs.First.Range.Font.Size=18;
Doc.Paragraphs.First.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter;
Doc.Paragraphs.First.Range.Font.Bold=5;
Doc.Paragraphs.Add(ref nothing);
Doc.Paragraphs.Item(2).Range.Text="";
Doc.Paragraphs.Item(2).Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter;
Doc.Paragraphs.Item(2).Range.Font.Size=17; Word.Paragraph ph=Doc.Paragraphs.Add(ref nothing);
ph.Range.ParagraphFormat.FirstLineIndent-=1;
ph.Range.Font.Size=15;
ph.Range.Font.Bold=2;
ph.Alignment=Word.WdParagraphAlignment.wdAlignParagraphLeft;
Word.Table tb=ph.Range.Tables.AddOld(ph.Range ,1,1);
tb.Rows.Alignment=Word.WdRowAlignment.wdAlignRowCenter;
tb.Range.Font.Size=15;
tb.Range.Font.Bold=2;
tb.Cell(1,1).Range.Text="";
if(ds.Tables[0].Rows.Count >0)
{
for(int i=0;i<ds.Tables[0].Rows.Count ;i++)
{
Word.Row row=tb.Rows.Add(ref nothing);
row.Alignment=Word.WdRowAlignment.wdAlignRowCenter;
row.Range.Font.Size=13;
row.Range.Font.Bold=0;
index++;
tb.Cell(index,1).Range.Text=ds.Tables[0].Rows[i]["MC"].ToString();
}
}
object strFi=page.Server.MapPath(@"../Template_temp/A.doc");
Doc.SaveAs(ref strFi,ref optional, ref optional, ref optional,ref optional, ref optional, ref optional,ref optional, ref optional, ref optional, ref optional);
Doc.Close(ref nothing,ref nothing,ref nothing);
app.Quit(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(Doc);
app=null;
Doc=null;
GC.Collect();
GC.Collect();
}
catch(Exception err)
{
strM=err.Message+",导出失败!";
return strM;
}
return "";
}