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

2013 年 11 月 17 日3950

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 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();
  }
  }
  }

相关文章

关键词:asp.net,页面,word,微软技术,.NET

责任编辑:valen

专题推荐

原创文章

微博互动

白皮书

0 0