C# asp.net 操作Word的前提配置和简单的方法
操作的前提:
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
至此前提配置就完成了
编程部分:
添加引用
- using Microsoft.Office.Core;
- using Microsoft.Office.Interop;
- using Microsoft.Office.Interop.Word;
编程打开Word文件,和Word文件夹建立连接
- object oReadOnly = true;
- object oMissing = System.Reflection.Missing.Value;
- Word._Application oWord;
- Word._Document oDoc;
- oWord = new Word.Application();
- oWord.Visible = false;//只是为了方便观察,为true时,会显示一个打开的Word
- object fileName = System.Web.HttpContext.Current.Server.MapPath("~/Resultstemplate.doc").ToString();
- oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);
关闭和Word的连接
- oDoc.Close(ref missing, ref missing, ref missing);
- oWord.Quit(ref missing, ref missing, ref missing);
对Word中的表格的数据读取,并在网页中简单显示
- protected void PageFucword()
- {
- //object oMissing = System.Reflection.Missing.Value;
- //Word._Application oWord;
- //Word._Document oDoc;
- //oWord = new Word.Application();
- //oWord.Visible = true;
- //
- //oDoc = oWord.Documents.Add(ref fileName, ref oMissing,ref oMissing, ref oMissing);
- object oReadOnly = true;
- object oMissing = System.Reflection.Missing.Value;
- Word._Application oWord;
- Word._Document oDoc;
- oWord = new Word.Application();
- oWord.Visible = false;//只是为了方便观察
- object fileName = System.Web.HttpContext.Current.Server.MapPath("~/Resultstemplate.doc").ToString();
- oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);
- //MessageBox.Show(oDoc.Tables.Count.ToString());
- for (int tablePos = 1; tablePos <= oDoc.Tables.Count; tablePos++)
- {
- Word.Table nowTable = oDoc.Tables[tablePos];
- string tableMessage = string.Format("第{0}/{1}个表:\n", tablePos, oDoc.Tables.Count);
- // int i = 0;
- // foreach (Word.InlineShape shape in nowTable.Cell(28, 1).Range.InlineShapes)
- //{
- //直接读取图片
- //if (shape.Type == Word.WdInlineShapeType.wdInlineShapePicture)
- //{
- // //获取Word中的图片
- // byte[] img = (byte[])shape.Range.EnhMetaFileBits;
- // Bitmap bmp = new Bitmap(new System.IO.MemoryStream(img));
- // bmp.Save("c:\\pic" + i.ToString() + ".jpg ");
- //}
- //i++;
- //从剪切板上读取图片,还无法实现
- //if (shape.Type == Word.WdInlineShapeType.wdInlineShapePicture)
- //{
- // shape.Select();
- // oWord.Selection.Copy();
- // Image image = Clipboard.GetImage();
- // Bitmap bitmap = new Bitmap(image);
- // bitmap.Save("c:\\pic " + i.ToString() + ".jpg ");
- // i++;
- //}
- // }
- Table a = new Table();
- a.Attributes["border"] = "1";
- a.Attributes["width"] = "100%";
- for (int rowPos = 1; rowPos <= nowTable.Rows.Count; rowPos++)
- {
- TableRow rw = new TableRow();
- for (int columPos = 1; columPos <= nowTable.Rows[rowPos].Cells.Count; columPos++)
- {
- TableCell tc = new TableCell();
- if (nowTable.Columns.Count > nowTable.Rows[rowPos].Cells.Count)
- {
- tc.Attributes["colspan"] = nowTable.Columns.Count.ToString();
- tc.Attributes["align"] = "center";
- }
- string cell = nowTable.Cell(rowPos, columPos).Range.Text;
- if (cell.Contains(""))
- {
- int i = 0;
- foreach (Word.InlineShape shape in nowTable.Cell(rowPos, columPos).Range.InlineShapes)
- {
- //直接读取图片
- if (shape.Type == Word.WdInlineShapeType.wdInlineShapePicture)
- {
- System.Web.UI.WebControls.Image imgct = new System.Web.UI.WebControls.Image();
- //获取Word中的图片
- byte[] img = (byte[])shape.Range.EnhMetaFileBits;
- Bitmap bmp = new Bitmap(new System.IO.MemoryStream(img));
- string url = System.Web.HttpContext.Current.Server.MapPath("~/image/").ToString() + "pic" + i.ToString() + ".jpg ";//图片保存路径
- bmp.Save(url);
- imgct.ImageUrl = "~/image/" + "pic" + i.ToString() + ".jpg ";
- imgct.Attributes["width"] = "250px";
- tc.Controls.Add(imgct);
- tc.Wrap = true;
- }
- i++;
- }
- }
- else
- {
- cell = cell.Remove(cell.Length - 2, 2);
- if (cell == "") cell = "空";
- tc.Text = cell;
- //tableMessage += cell;
- //tableMessage = tableMessage.Remove(tableMessage.Length - 2, 2);//remove \r\a
- //tableMessage += "\t";
- }
- rw.Cells.Add(tc);
- }
- //tableMessage += "\n";
- a.Rows.Add(rw);
- }
- //Label1.Text = tableMessage;
- Page.Controls.Add(a);
- }
- }
通过标签Bookmark来读取数据
Bookmark bk;
bk.Range.Cells[1];//该值得到书签所在的单元格
- public bool FruitWordToSQL()
- {
- object oReadOnly = true;
- object oMissing = System.Reflection.Missing.Value;
- Word._Application oWord;
- Word._Document oDoc;
- oWord = new Word.Application();
- //oWord.Visible = false;//只是为了方便观察
- object fileName = System.Web.HttpContext.Current.Server.MapPath("~/Resultstemplate.doc").ToString();
- oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);
- System.Collections.IEnumerator enu = oWord.ActiveDocument.Bookmarks.GetEnumerator();
- Dictionary<string, string> map = new Dictionary<string, string>();//数据项(项名,项值)
- while (enu.MoveNext())
- {
- Word.Bookmark bk = (Word.Bookmark)enu.Current;
- string text = bk.Name.ToString();
- string value=bk.Range.Cells[1].Range.Text;//书签所在单元格的内容
- value = value.Remove(value.Length - 2);//去除单元格后的标记(/r/n)
- map.Add(text,value);
- }
- //对word的操作结束,关闭对word的控制
- oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
- oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
- //检验数据的格式及正确性
- KeyValuePair<bool,string> check=CheckData(map);
- if (check.Key == false)
- {
- //check.Value中所含的值为数据检验不符合标准的第一个数据的值
- return false;
- }
- //map中的数据插入到数据库的Fruit表中
- bool Inserted=InsertData(map);
- if (Inserted == false)//插入失败
- {
- //插入失败后的操作
- return false;
- }
- return true;
- }
C# asp.net 操作Word的前提配置和简单的方法的更多相关文章
- Asp.net操作Word文档,原来这么简单啊!
引用Word对象库文件 具体做法是打开菜单栏中的项目>添加引用>浏览,在打开的“选择组件”对话框中找到MSWORD.OLB后按确定即可引入此对象库文件,vs.net将会自动将库文件转化为 ...
- asp.net操作word的表格
近日开发中用户要求实现导出数据为Word,本来想使用html保存为word的实现,但因用户要求样式很高,使用html不好控制,并且导出中包括图片,使用页面导出时图片还是一个路径,不能把图片包括在wor ...
- ASP.NET操作Word的IIS权限配置
ASP.NET账号在默认情况下是没有权限操作Microsoft Office对象的,如果不进行权限的配置,代码会抛出类似以下的异常: 检索 COM 类工厂中 CLSID 为 {00024500-000 ...
- asp.net 操作word 权限
1.先安装office 2.在“DCOM配置”中,为IIS账号配置操作Word(其他Office对象也一样)的权限: 开始>运行>输入 dcomcnfg >确定 具体操作:“组件 ...
- Asp.Net 操作word 第二篇[推荐]
引言:前段时间有项目要用c#生成Word格式的计算报告,通过网络查找到很多内容,但是都很凌乱,于是自己决定将具体的步骤总结整理出来,以便于更好的交流和以后相似问题可以迅速的解决! 现通过具体的示例演示 ...
- asp.net 操作word
参考一:点击这里 参考二:点击这里 参考三:点击这里 using System; using System.Web.Security; using Microsoft.Office.Interop.W ...
- asp.net操作word 配置在IIS上出现的问题
异常: 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问. (异常来自 ...
- 关于linux asp.net MVC网站中 httpHandlers配置无效的处理方法
近期有Jexus用户反映,在Linux ASP.NET MVC网站的Web.config中添加 httpHandlers 配置用于处理自定义类型,但是在运行中并没有产生预期的效果,服务器返回了404( ...
- asp.net操作GridView添删改查的两种方法 及 光棒效果
这部份小内容很想写下来了,因为是基础中的基础,但是近来用的比较少,又温习了一篇,发现有点陌生了,所以,还是写一下吧. 方法一:使用Gridview本身自带的事件处理,代码如下(注意:每次操作完都得重新 ...
随机推荐
- [转]优化PHP程序的方法
1. If a method c++an be static, declare it static. Speed improvement is by a factor of 4. 如果一个方法可静态化 ...
- 我爱工程化 之 gulp 使用(一)
一.简介 gulp是前端自动化工具,压缩.合并.预编译检查等等,它与grunt频繁IO操作来消耗内存相比,它是使用的流的方式,我们可以简称为管道流(一端入,一端出,3通水,一个大桶,第一通在管道里面流 ...
- 解决jquery mobile的遇到高版本Chrome一直转圈,页面加载不出来的情况。
把这么一段代码,加到jquery.mobile.js中后问题解决了. $(document).on('mobileinit',function(){ $.mobile.changePage.defau ...
- opcache运行时配置参数详解
PHP的opcode缓存又出 了新成员(说新不新,也有一段日子了),那就是opcache.新浪微博等都在使用,惠新宸老师强力推荐.本人最近根据官网地址 (http://www.php.net/manu ...
- 将TIBCO Host 实例注册为Windows服务
安装了TIBCO ActiveMatrix BPM及成功创建了ActiveMatrix Administrator 和 BPM Server后,每次都要手动启动tibcohost,比较麻烦,实际上TI ...
- cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行)转载请保留作者信息:1.cin1 ...
- visualvm 监控 远程 机器上的 Java 程序
JDK里面本身就带了很多的监控工具,如JConsole等. 我们今天要讲的这款工具visualvm,就是其中的一款.但是这款工具是在JDK1.6.07及以上才有的.它能够对JAVA程序的JVM堆.线程 ...
- js 字符串扩展
<script type="text/javascript" language="javascript"> String.prototype.tri ...
- BZOJ 3971 Матрёшка 解题报告
很自然想到区间 DP. 设 $Dp[i][j]$ 表示把区间 $[i, j]$ 内的套娃合并成一个所需要的代价,那么有: $Dp[i][i] = 0$ $Dp[i][j] = min\{Dp[i][k ...
- hdu 4267
一个很不错的题: 刚刚看到这个题目就感觉要用线段树或者树状数组,但是有感觉有点不同: 敲了一发简单的线段树之后果断的T了: 网上一搜题解,发现要用55颗线段树或者树状数组: 一共有k种树,然后每种树根 ...