操作的前提:

1.要保证机器本身要安装OFFICE.

有时安装了Office,但是不能找到Microsoft Word 11.0(或者更高的版本) Object Library。那可能是因为在安装office的时候没有选在。net可编程性支持

此时只需要重新打开office安装文件-》添加或删除功能-》下一步-》在Microsoft Word下点选。net可编程性支持

以Office 2003为例

2.用VS打开一个asp.net网站,右键单击Bin ->添加引用,在COM中选择Microsoft Word 11.0(或者更高的版本) Object Library。点击确定后会在Bin文件夹中出现

Microsoft.Office.Interop.Word.dll

或者Interop.Word.dll

如果没有Bin文件夹,就右键根目录,选择添加ASP.NET文件->Bin

至此前提配置就完成了

编程部分:

添加引用

  1. using Microsoft.Office.Core;
  2. using Microsoft.Office.Interop;
  3. using Microsoft.Office.Interop.Word;

编程打开Word文件,和Word文件夹建立连接

  1. object oReadOnly = true;
  2. object oMissing = System.Reflection.Missing.Value;
  3. Word._Application oWord;
  4. Word._Document oDoc;
  5. oWord = new Word.Application();
  6. oWord.Visible = false;//只是为了方便观察,为true时,会显示一个打开的Word
  7. object fileName = System.Web.HttpContext.Current.Server.MapPath("~/Resultstemplate.doc").ToString();
  8. oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);

关闭和Word的连接

  1. oDoc.Close(ref missing, ref missing, ref missing);
  2. oWord.Quit(ref missing, ref missing, ref missing);

对Word中的表格的数据读取,并在网页中简单显示

  1. protected void PageFucword()
  2. {
  3. //object oMissing = System.Reflection.Missing.Value;
  4. //Word._Application oWord;
  5. //Word._Document oDoc;
  6. //oWord = new Word.Application();
  7. //oWord.Visible = true;
  8. //
  9. //oDoc = oWord.Documents.Add(ref fileName, ref oMissing,ref oMissing, ref oMissing);
  10. object oReadOnly = true;
  11. object oMissing = System.Reflection.Missing.Value;
  12. Word._Application oWord;
  13. Word._Document oDoc;
  14. oWord = new Word.Application();
  15. oWord.Visible = false;//只是为了方便观察
  16. object fileName = System.Web.HttpContext.Current.Server.MapPath("~/Resultstemplate.doc").ToString();
  17. oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);
  18. //MessageBox.Show(oDoc.Tables.Count.ToString());
  19. for (int tablePos = 1; tablePos <= oDoc.Tables.Count; tablePos++)
  20. {
  21. Word.Table nowTable = oDoc.Tables[tablePos];
  22. string tableMessage = string.Format("第{0}/{1}个表:\n", tablePos, oDoc.Tables.Count);
  23. // int i = 0;
  24. // foreach (Word.InlineShape shape in nowTable.Cell(28, 1).Range.InlineShapes)
  25. //{
  26. //直接读取图片
  27. //if (shape.Type == Word.WdInlineShapeType.wdInlineShapePicture)
  28. //{
  29. //    //获取Word中的图片
  30. //    byte[] img = (byte[])shape.Range.EnhMetaFileBits;
  31. //    Bitmap bmp = new Bitmap(new System.IO.MemoryStream(img));
  32. //    bmp.Save("c:\\pic" + i.ToString() + ".jpg ");
  33. //}
  34. //i++;
  35. //从剪切板上读取图片,还无法实现
  36. //if (shape.Type == Word.WdInlineShapeType.wdInlineShapePicture)
  37. //{
  38. //    shape.Select();
  39. //    oWord.Selection.Copy();
  40. //    Image image = Clipboard.GetImage();
  41. //    Bitmap bitmap = new Bitmap(image);
  42. //    bitmap.Save("c:\\pic " + i.ToString() + ".jpg ");
  43. //    i++;
  44. //}
  45. // }
  46. Table a = new Table();
  47. a.Attributes["border"] = "1";
  48. a.Attributes["width"] = "100%";
  49. for (int rowPos = 1; rowPos <= nowTable.Rows.Count; rowPos++)
  50. {
  51. TableRow rw = new TableRow();
  52. for (int columPos = 1; columPos <= nowTable.Rows[rowPos].Cells.Count; columPos++)
  53. {
  54. TableCell tc = new TableCell();
  55. if (nowTable.Columns.Count > nowTable.Rows[rowPos].Cells.Count)
  56. {
  57. tc.Attributes["colspan"] = nowTable.Columns.Count.ToString();
  58. tc.Attributes["align"] = "center";
  59. }
  60. string cell = nowTable.Cell(rowPos, columPos).Range.Text;
  61. if (cell.Contains(""))
  62. {
  63. int i = 0;
  64. foreach (Word.InlineShape shape in nowTable.Cell(rowPos, columPos).Range.InlineShapes)
  65. {
  66. //直接读取图片
  67. if (shape.Type == Word.WdInlineShapeType.wdInlineShapePicture)
  68. {
  69. System.Web.UI.WebControls.Image imgct = new System.Web.UI.WebControls.Image();
  70. //获取Word中的图片
  71. byte[] img = (byte[])shape.Range.EnhMetaFileBits;
  72. Bitmap bmp = new Bitmap(new System.IO.MemoryStream(img));
  73. string url = System.Web.HttpContext.Current.Server.MapPath("~/image/").ToString() + "pic" + i.ToString() + ".jpg ";//图片保存路径
  74. bmp.Save(url);
  75. imgct.ImageUrl = "~/image/" + "pic" + i.ToString() + ".jpg ";
  76. imgct.Attributes["width"] = "250px";
  77. tc.Controls.Add(imgct);
  78. tc.Wrap = true;
  79. }
  80. i++;
  81. }
  82. }
  83. else
  84. {
  85. cell = cell.Remove(cell.Length - 2, 2);
  86. if (cell == "") cell = "空";
  87. tc.Text = cell;
  88. //tableMessage += cell;
  89. //tableMessage = tableMessage.Remove(tableMessage.Length - 2, 2);//remove \r\a
  90. //tableMessage += "\t";
  91. }
  92. rw.Cells.Add(tc);
  93. }
  94. //tableMessage += "\n";
  95. a.Rows.Add(rw);
  96. }
  97. //Label1.Text = tableMessage;
  98. Page.Controls.Add(a);
  99. }
  100. }

通过标签Bookmark来读取数据

Bookmark bk;

bk.Range.Cells[1];//该值得到书签所在的单元格

    1. public bool FruitWordToSQL()
    2. {
    3. object oReadOnly = true;
    4. object oMissing = System.Reflection.Missing.Value;
    5. Word._Application oWord;
    6. Word._Document oDoc;
    7. oWord = new Word.Application();
    8. //oWord.Visible = false;//只是为了方便观察
    9. object fileName = System.Web.HttpContext.Current.Server.MapPath("~/Resultstemplate.doc").ToString();
    10. oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);
    11. System.Collections.IEnumerator enu = oWord.ActiveDocument.Bookmarks.GetEnumerator();
    12. Dictionary<string, string> map = new Dictionary<string, string>();//数据项(项名,项值)
    13. while (enu.MoveNext())
    14. {
    15. Word.Bookmark bk = (Word.Bookmark)enu.Current;
    16. string text = bk.Name.ToString();
    17. string value=bk.Range.Cells[1].Range.Text;//书签所在单元格的内容
    18. value = value.Remove(value.Length - 2);//去除单元格后的标记(/r/n)
    19. map.Add(text,value);
    20. }
    21. //对word的操作结束,关闭对word的控制
    22. oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
    23. oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
    24. //检验数据的格式及正确性
    25. KeyValuePair<bool,string> check=CheckData(map);
    26. if (check.Key == false)
    27. {
    28. //check.Value中所含的值为数据检验不符合标准的第一个数据的值
    29. return false;
    30. }
    31. //map中的数据插入到数据库的Fruit表中
    32. bool Inserted=InsertData(map);
    33. if (Inserted == false)//插入失败
    34. {
    35. //插入失败后的操作
    36. return false;
    37. }
    38. return true;
    39. }

C# asp.net 操作Word的前提配置和简单的方法的更多相关文章

  1. Asp.net操作Word文档,原来这么简单啊!

    引用Word对象库文件  具体做法是打开菜单栏中的项目>添加引用>浏览,在打开的“选择组件”对话框中找到MSWORD.OLB后按确定即可引入此对象库文件,vs.net将会自动将库文件转化为 ...

  2. asp.net操作word的表格

    近日开发中用户要求实现导出数据为Word,本来想使用html保存为word的实现,但因用户要求样式很高,使用html不好控制,并且导出中包括图片,使用页面导出时图片还是一个路径,不能把图片包括在wor ...

  3. ASP.NET操作Word的IIS权限配置

    ASP.NET账号在默认情况下是没有权限操作Microsoft Office对象的,如果不进行权限的配置,代码会抛出类似以下的异常: 检索 COM 类工厂中 CLSID 为 {00024500-000 ...

  4. asp.net 操作word 权限

    1.先安装office 2.在“DCOM配置”中,为IIS账号配置操作Word(其他Office对象也一样)的权限: 开始>运行>输入  dcomcnfg  >确定 具体操作:“组件 ...

  5. Asp.Net 操作word 第二篇[推荐]

    引言:前段时间有项目要用c#生成Word格式的计算报告,通过网络查找到很多内容,但是都很凌乱,于是自己决定将具体的步骤总结整理出来,以便于更好的交流和以后相似问题可以迅速的解决! 现通过具体的示例演示 ...

  6. asp.net 操作word

    参考一:点击这里 参考二:点击这里 参考三:点击这里 using System; using System.Web.Security; using Microsoft.Office.Interop.W ...

  7. asp.net操作word 配置在IIS上出现的问题

    异常: 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问. (异常来自 ...

  8. 关于linux asp.net MVC网站中 httpHandlers配置无效的处理方法

    近期有Jexus用户反映,在Linux ASP.NET MVC网站的Web.config中添加 httpHandlers 配置用于处理自定义类型,但是在运行中并没有产生预期的效果,服务器返回了404( ...

  9. asp.net操作GridView添删改查的两种方法 及 光棒效果

    这部份小内容很想写下来了,因为是基础中的基础,但是近来用的比较少,又温习了一篇,发现有点陌生了,所以,还是写一下吧. 方法一:使用Gridview本身自带的事件处理,代码如下(注意:每次操作完都得重新 ...

随机推荐

  1. [转载]浅析STL allocator

    本文转载自水目沾博客:http://www.cnblogs.com/zhuwbox/p/3699977.html   向大师致敬 一般而言,我们习惯的 C++ 内存配置操作和释放操作是这样的: 1 c ...

  2. thinkphp autoload 命名空间自定义 namespace

    使用thinkPHP过程中,一些自定义的类库和第三方类库需要找一个合适的位置放置,放到系统默认的org文件夹感觉不太好,破坏了thinkPHP的原生目录. 就看了一下官方手册,可以在模块或者应用的配置 ...

  3. PHP 类和继承

    //定义一个超类 //public 和 protectd属性和方法可以继承,private不可继承. class A{ public $a =0; private $b = 1; protected ...

  4. php 工作模式

    PHP运行模式 1.cgi通用网关接口 (少用)2.fast-cgi常驻型的 cgi [ngixn常用]3.cli命令运行 (命令行用得多)4.web模块模式(apache等web服务器的运行模式)[ ...

  5. apache 工作模式

    apache三种工作模式: prefork(2.4前默认)/worker/event(2.4默认)内容整理来自以下网站http://m.blog.csdn.net/article/details?id ...

  6. DZ升级到X3.2后,UCenter用户管理中心进不了了

    前天将DZ升级到X3.2后,UCenter用户管理中心进不了了,输入的密码也对,验证码也对,就是点登录后没反应,又回来输入前的状态.如果更换密码后,显示密码错误,证明密码是没错的.但就是进不了.大家看 ...

  7. 建站服务器的最优选择之Windows Or Linux

    转载于:http://www.0553114.com/news/detail-702287.html 不管是个人建站,还是中小型企业建站,选择一款合适的主机是站长朋友们共同的心愿.主机是选择Windo ...

  8. PHP — 用PHP实现一个双向队列

    1.简介 deque,全名double-ended queue,是一种具有队列和栈的性质的数据结构.双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行.双向队列(双端队列)就像是一个队 ...

  9. 【BZOJ】1088: [SCOI2005]扫雷Mine

    1088: [SCOI2005]扫雷Mine Description 相 信大家都玩过扫雷的游戏.那是在一个n*m的矩阵里面有一些雷,要你根据一些信息找出雷来.万圣节到了,“余”人国流行起了一种简单的 ...

  10. codeforces edu round3

    B. The Best Gift  传送门:http://codeforces.com/problemset/problem/609/B Emily's birthday is next week a ...