获取数据集,打开word模板
public string Export(DataSet ds,System.Web.UI.Page page,string FileName)
{
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();
string strP="";
                        strP="Template_temp/"+FileName;
object FileNa=page.Server.MapPath(strP);
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);
try
{
if(Doc.Books.Exists("A"))
{
object b1="A";
Word.Book bk1=Doc.Books.Item(ref b1);
bk1.Range.Text=ds.Tables[0].Rows[0]["A"].ToString();
} if(Doc.Books.Exists("B"))
{
object b2="B";
Word.Book bk2=Doc.Books.Item(ref b2);
bk2.Range.Text=ds.Tables[0].Rows[0]["B"].ToString();
}
 strP=@"../Template_temp/YJ.doc";
object strFi=page.Server.MapPath(strP);
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 NOTsaveChanges, ref originalFormat, ref routeDocument);
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 "";
}

解决方案 »

  1.   

    还可获取List<T>实体类,通过属性赋值给标签再输出
    public void FileToWord(System.Web.UI.Page page,string strP,string File) {
    string Pa=page.Server.MapPath(@"Template_temp/"+File+"");
    page.Response.AppendHeader("Content-Disposition","attachment;filename="+File+"");
    page.Response.ContentType ="application/ms-word"; 
    page.Response.WriteFile(Pa);
    page.Response.End();
    }
      

  2.   

    替换word中设置的关键字就行了
      

  3.   

    1、建立书签与值的字典
    2、打开word,替换书签。
        判断书签值如果是文本range.Text = "";
                 如果是DataTable,Tables.Add
                 一行一行写数据
    3、保存关闭
      

  4.   

    你的word模板呢贴张图看看,这么说太笼统啦