首先添加应用"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();
}

/// <summary>
/// 关闭文档
/// </summary>
public void Close()
{
object saveChanges = WdSaveOptions.wdPromptToSaveChanges;
object originalFormat = WdOriginalFormat.wdWordDocument;
object routeDocument = true;
oWordApplic.Documents.Close(ref saveChanges, ref originalFormat, ref routeDocument);
}

/// <summary>
/// 退出wordapp组件对象
/// </summary>
public void Quit()
{
object missing = System.Reflection.Missing.Value;
oWordApplic.Application.Quit(ref missing, ref missing, ref missing);

}

public void Save()
{
oDoc.Save();
}

/// <summary>
/// 替代
/// </summary>
/// <param name="findStr">WORD变量</param>
/// <param name="replaceStr">字符串</param>
/// <returns></returns>
public bool ReplaceText(string findStr, string replaceStr)
{
object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
object missing = System.Reflection.Missing.Value;
oWordApplic.Selection.Find.ClearFormatting();
object findText = findStr;
oWordApplic.Selection.Find.Replacement.ClearFormatting();
oWordApplic.Selection.Find.Replacement.Text = replaceStr;
return oWordApplic.Selection.Find.Execute(ref findText, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing);
}

/// <summary>
/// 另存为
/// </summary>
/// <param name="strFileName">模板路经</param>
public void SaveAs(string strFileName)
{
object missing = System.Reflection.Missing.Value;
object fileName = strFileName;
oDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}

// 保存为HTML文件格式
public void SaveAsHtml(string strFileName)
{
object missing = System.Reflection.Missing.Value;
object fileName = strFileName;
object Format = (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
oDoc.SaveAs(ref fileName, ref Format, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}

public void InsertText(string strText)
{
oWordApplic.Selection.TypeText(strText);
}

public void InsertLineBreak()
{
oWordApplic.Selection.TypeParagraph();
}
public void InsertLineBreak(int nline)
{
for (int i = 0; i < nline; i++)
oWordApplic.Selection.TypeParagraph();
}

// Change the paragraph alignement
public void SetAlignment(string strType)
{
switch (strType)
{
case "Center":
oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
break;
case "Left":
oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
break;
case "Right":
oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
break;
case "Justify":
oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;
break;
}

}

//如果您使用thif函数来更改字体您应该再次调用它
//没有参数没有特定的格式,以设置字体
public void SetFont(string strType)
{
switch (strType)
{
case "Bold":
oWordApplic.Selection.Font.Bold = 1;
break;
case "Italic":
oWordApplic.Selection.Font.Italic = 1;
break;
case "Underlined":
oWordApplic.Selection.Font.Subscript = 0;
break;
}

}

// disable all the style
public void SetFont()
{
oWordApplic.Selection.Font.Bold = 0;
oWordApplic.Selection.Font.Italic = 0;
oWordApplic.Selection.Font.Subscript = 0;

}

public void SetFontName(string strType)
{
oWordApplic.Selection.Font.Name = strType;

}

public void SetFontSize(int nSize)
{
oWordApplic.Selection.Font.Size = nSize;

}

public void InsertPagebreak()
{
// VB : Selection.InsertBreak Type:=wdPageBreak
object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
oWordApplic.Selection.InsertBreak(ref pBreak);
}

// Go to a predefined bookmark, if the bookmark doesn't exists the application will raise an error

public void GotoBookMark(string strBookMarkName)
{
// VB : Selection.GoTo What:=wdGoToBookmark, Name:="nome"
object missing = System.Reflection.Missing.Value;

object Bookmark = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
object NameBookMark = strBookMarkName;
oWordApplic.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);
}

public void GoToTheEnd()
{
// VB : Selection.EndKey Unit:=wdStory
object missing = System.Reflection.Missing.Value;
object unit;
unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
oWordApplic.Selection.EndKey(ref unit, ref missing);

}
public void GoToTheBeginning()
{
// VB : Selection.HomeKey Unit:=wdStory
object missing = System.Reflection.Missing.Value;
object unit;
unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
oWordApplic.Selection.HomeKey(ref unit, ref missing);

}

public void GoToTheTable(int ntable)
{

object missing = System.Reflection.Missing.Value;
object what;
what = Microsoft.Office.Interop.Word.WdUnits.wdTable;
object which;
which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
object count;
count = ntable;
Range r = oWordApplic.Selection.GoTo(ref what, ref which, ref count, ref missing);
oWordApplic.Selection.Find.ClearFormatting();

oWordApplic.Selection.Text = "";

}

public void GoToRightCell()
{
// Selection.MoveRight Unit:=wdCell

object missing = System.Reflection.Missing.Value;
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdCell;
oWordApplic.Selection.MoveRight(ref direction, ref missing, ref missing);
}

public void GoToLeftCell()
{
// Selection.MoveRight Unit:=wdCell

object missing = System.Reflection.Missing.Value;
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdCell;
oWordApplic.Selection.MoveLeft(ref direction, ref missing, ref missing);
}

public void GoToDownCell()
{
// Selection.MoveRight Unit:=wdCell

object missing = System.Reflection.Missing.Value;
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;
oWordApplic.Selection.MoveDown(ref direction, ref missing, ref missing);
}

public void GoToUpCell()
{
// Selection.MoveRight Unit:=wdCell

object missing = System.Reflection.Missing.Value;
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;
oWordApplic.Selection.MoveUp(ref direction, ref missing, ref missing);
}

// this function doesn't work
public void InsertPageNumber(string strType, bool bHeader)
{
object missing = System.Reflection.Missing.Value;
object alignment;
object bFirstPage = false;
object bF = true;
//if (bHeader == true)
//WordApplic.Selection.HeaderFooter.PageNumbers.ShowFirstPageNumber = bF;
switch (strType)
{
case "Center":
alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
//WordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment,ref bFirstPage);
//Word.Selection objSelection = WordApplic.pSelection;

// oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
break;
case "Right":
alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberRight;
// oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
break;
case "Left":
alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberLeft;
oWordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment, ref bFirstPage);
break;
}

}

public void selectionTable()
{

Object Nothing = System.Reflection.Missing.Value;
//oDoc.Content.Tables[1].Rows.Add(ref Nothing);

//oDoc.Content.Tables[2].Rows.Add(ref Nothing);
//oDoc.Content.Tables[2].Rows.Add(ref Nothing);
//oDoc.Content.Tables[2].Rows.Add(ref Nothing);

object missing = System.Reflection.Missing.Value;
object what;
what = Microsoft.Office.Interop.Word.WdUnits.wdWord;
object which;
which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
object count;
count = 3;

//object s1=Microsoft.Office.Interop.Word.
oDoc.Content.Tables[2].Select();

object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;

//oWordApplic.Selection.Find.Text = "$ssss$";
//oWordApplic.Selection.GoTo(ref what, ref which, ref count, ref missing);
//oWordApplic.Selection.Find.ClearFormatting();

oWordApplic.Selection.MoveDown(ref WdLine, ref count, ref Nothing);
Microsoft.Office.Interop.Word.Table newTable = oDoc.Tables.Add(oWordApplic.Selection.Range, 12, 3, ref Nothing, ref Nothing);

}

需求虽然实现了,但个人能力太欠缺,实现的效果很差!

转载于:https://www.cnblogs.com/wulongwen/archive/2010/09/29/1838310.html

asp.net 页面数据导入word模板相关推荐

  1. matlab导入word数据,如何将Excel数据导入MATLAB中?/excel数据导入word模板

    如何将Excel数据导入MATLAB中? 从excel中导入,可以用xlsread()函数. 例如: A=xlsread('C:\Users\Administrator\Desktop\07-29预. ...

  2. Vue 自定义富文本编辑器 tinymce 支持导入 word 模板

    自定义富文本编辑器分为前端项目和后端项目两个部分,首先先说一下前端项目 前端 前端项目地址: https://github.com/haoxiaoyong1014/editor-ui 编辑器名称: t ...

  3. python 批量打印文档_使用python将Excel数据填充Word模板并生成Word

    [项目需求] Excel中有一万多条学生学平险数据,需要给每位学生打印购买回执单,回执单包括学生姓名,身份证号,学校等信息,目前只能从Excel拷贝数据到Word模板中,然后打印,效率及其低下,寻求帮 ...

  4. python excel模板 生成excel表格_python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图...

    python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图 #coding=utf-8 from openpyxl importload_workbookfro ...

  5. Java导出数据到Word模板中

    Java导出数据到Word模板. 前言 网上的方案 需求介绍 模板 简介 使用体验 poi-tl Freemarker 操作步骤 总结 前言 相信很多人都会遇到Java导出的业务,Java导出主要有导 ...

  6. 把Excel数据填充word模板生成多份word文档

    有些事情,你想记得的就会记得.有些事情,你想忘记的就会忘记,如果忘记不了,那就不要忘记了,因为忘记是不需要努力的. Model_Car.cs代码 public class Model_Car{publ ...

  7. java实现导入word模板导入试题

    最近有一个项目需要将一个word文档中的试题数据导入,并存储到数据库中.试题类型包括:单选题.多选题.判断题.填空题.简答题.支持图片导入(我的这篇是借鉴JAVA实现Excel.Word模板导入 - ...

  8. 转:[Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!...

    引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...

  9. 通用Excel数据导入功能模板

    最近做了一个excel导入功能,思路很简单,给一个上传excel文件的入口,然后获取excel文件的sheet签和签中的数据,可以对数据对象进行业务处理,当然也可以数据存入数据库中 代码如下:jsp页 ...

最新文章

  1. seaborn可视化绘制双变量分组条形图(Annotating Grouped Barplot: Side-by-side)、添加数值标签进行标记
  2. AI基础:正则表达式
  3. java array iterator_java数组遍历——iterator和for方法
  4. 【操作系统】进程调度(2b):STCF(最短完成时间优先) 算法 原理与实践
  5. Linux内核品读 /基础组件/ 模块机制快速入门
  6. 如何从命令行删除MongoDB数据库?
  7. vs2005 虚拟调试配置(转)
  8. python3----如何简单地理解Python中的if __name__ == '__main__'
  9. 2015.03.20使命
  10. 浅析高速公路网络数据集制作
  11. 【搜索/提问必备】如何正确的在Stack Overflow提问
  12. “荣光医院”急救中心 73 问答
  13. java holder详解_connection holder is null 异常详解
  14. iCheck 的简单了解
  15. 游戏帧同步和状态同步
  16. win10在不关闭防火墙的情况下实现无线投屏
  17. 下载android2.2源码(froyo)(1)
  18. 【水汐のC#】计一个Windows应用程序,在该程序中定义一个学生类和班级类,以处理学生的学号,姓名,语文,数学和英语3门课程的期末考试成绩。实现如下要求的功能:
  19. 计算机组成原理第五课,计算机组成原理第五章课件(白中英版).ppt
  20. Base128编码规范

热门文章

  1. 如何优雅的起个变量名?
  2. 数据库原理—数据模型(三)
  3. JavaScript学习(七十一)—call、apply、bind学习总结
  4. 自家院子里能不能种桃树呢?
  5. 人类历史上有哪些逆天的文物?
  6. 下面两种送礼模式会让你的生意兴隆
  7. 互联网产品哪个不火哪个就在红利期
  8. 做生意失败是一种什么体验?创业中有哪些雷区需要注意?
  9. 职场上不会“装傻”,才是真的傻!
  10. 人不能活在舒适区里,我要趁年轻出去闯闯