当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

asp.net页面数据导入word模板

  首先添加应用"Microsoft Word 11.0 Object library"的COM组件,从而来实现读取word文档的对象。

  点击事件代码

  代码

  protected void LinkButton1_Click(object sender, EventArgs e)

  {

  MyWordClass test = new MyWordClass();

  LTP.Accounts.Bus.User user = JMWMgr2.Utility.GetCurrentUser();

  try

  {

  string SaveURL = HttpContext.Current.Server.MapPath("~/upFiles/" + user.DepartmentID + "/" + user.UserID);

  if (!System.IO.Directory.Exists(SaveURL))

  {

  System.IO.Directory.CreateDirectory(SaveURL);

  SaveURL = SaveURL + "/作业风险分析控制表.doc";

  ToWordFile(SaveURL,test);

  }

  else

  {

  SaveURL = SaveURL + "/作业风险分析控制表.doc";

  if (System.IO.File.Exists(SaveURL))

  {

  //if (rp.NextAudit == "")

  //{

  System.IO.File.Delete(SaveURL);

  ToWordFile(SaveURL,test);

  //}

  }

  else

  {

  ToWordFile(SaveURL,test);

  }

  }

  Response.Redirect("../upFiles/" + user.DepartmentID + "/" + user.UserID + "/作业风险分析控制表.doc");

  }

  catch (Exception ex)

  {

  Tools.ShowMess(ex.Message.Replace(@"\\", @"\\\\"), this);

  }

  finally

  {

  //test.Quit();

  }

  }

  protected void ToWordFile(string SaveURL, MyWordClass test)

  {

  string id = Request["id"];

  RC.Model.ZuoYeFXFXKZB model = RC.BLL.ZuoYeFXFXKZB.GetModelByID(int.Parse(id));

  if (model == null)

  {

  return;

  }

  else

  {

  try

  {

  FileInfo f = new FileInfo(HttpContext.Current.Server.MapPath("~/js/print.doc"));

  try

  {

  f.CopyTo(SaveURL);

  }

  catch(Exception ex)

  {

  Tools.ShowMess(ex.Message.Replace(@"\\", @"\\\\"), this);

  return;

  }

  test.Open(SaveURL);

  test.ReplaceText("$zyxm$", model.ZuoYeXM.ToString());

  test.ReplaceText("$jhgzsj$", model.JiHuaGZSJ.Value.ToString("yyyy-MM-dd"));

  test.ReplaceText("$fxr$", model.FenXiR.ToString());

  test.ReplaceText("$shr$", model.ShenHeR.ToString());

  test.ReplaceText("$fxwcsj$", model.FenXiWCSJ.Value.ToString("yyyy-MM-dd"));

  List<RC.Model.ZuoYeFXFXKZB2> zyfxlist = RC.BLL.ZuoYeFXFXKZB2.GetZuoYeFXFXKZB2ListByCondition(0, "ZuoYeFXFXKZB_ID='" + id + "'and TianJiaLX!=2");

  int ssss = 0;

  foreach (RC.Model.ZuoYeFXFXKZB2 r in zyfxlist)

  {

  if (r.ToString()!="")

  {

  test.ReplaceText("$wxys$", r.WeiXianYS.ToString());

  test.ReplaceText("$kzcs$", r.KongZhiCS.ToString());

  test.ReplaceText("$zrr$", r.ZeRenR.ToString());

  test.ReplaceText("$kzsd$", r.KongZhiSD.ToString());

  test.ReplaceText("$lssj$", r.LuoShiSJ.Value.ToString("yyyy-MM-dd"));

  }

  else

  {

  ssss += 1;

  }

  }

  test.Save();

  }

  catch (Exception ex)

  {

  Tools.ShowMess(ex.Message.Replace(@"\\", @"\\\\"), this);

  }

  finally

  {

  test.Save();

  test.Close();

  test.Quit();

  }

  }

  }

  2.操作类代码

  代码

  using System;

  using System.Data;

  using System.Configuration;

  using System.Web;

  using System.Web.Security;

  using System.Web.UI;

  using System.Web.UI.HtmlControls;

  using System.Web.UI.WebControls;

  using System.Web.UI.WebControls.WebParts;

  using Microsoft.Office.Interop;

  using Microsoft.Office.Interop.Word;

  /// <summary>

  ///MyWordClass 的摘要说明

  /// </summary>

  public class MyWordClass

  {

  private Microsoft.Office.Interop.Word.ApplicationClass oWordApplic;    //a reference to Word application

  private Microsoft.Office.Interop.Word.Document oDoc; //a reference to the document

  public MyWordClass()

  {

  // 构造与Microsoft Word的COM对象接口

  oWordApplic = new Microsoft.Office.Interop.Word.ApplicationClass();

  }

  /// <summary>

  /// 打开模板 ,打开一个文件(该文件必须存在),激活它

  /// </summary>

  /// <param name="strFileName"></param>

  public void Open(string strFileName)

  {

  object fileName = strFileName;

  object readOnly = false;

  object isVisible = false;

  object missing = System.Reflection.Missing.Value;

  oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly,

  ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,

  ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

  oDoc.Activate();

  }

  // Open a new document

  public void Open()

  {

  object missing = System.Reflection.Missing.Value;

  oDoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);

  oDoc.Activate();

  }

相关内容
赞助商链接