asp.net word内容读取到页面
2015-04-10 11:09
375 查看
1、添加Microsoft.Vbe.Interop.dll引用。
2、以下方法可以简单的读取到word文档文字内容,不包括图片、格式等。
3、把word文档正确展示出来,把word转换成html文档,然后读取出来。
2、以下方法可以简单的读取到word文档文字内容,不包括图片、格式等。
private string ReadWordFile(string file) { string filePath = Server.MapPath(file); if (System.IO.File.Exists(filePath)) { Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); object fileobj = filePath; object nullobj = System.Reflection.Missing.Value; //打开指定文件(不同版本的COM参数个数有差异,一般而言除第一个外都用nullobj就行了) Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj); //取得doc文件中的文本 string outText = doc.Content.Text; //关闭文件 doc.Close(ref nullobj, ref nullobj, ref nullobj); //关闭COM wordApp.Quit(ref nullobj, ref nullobj, ref nullobj); //返回 return outText; } return null; }
3、把word文档正确展示出来,把word转换成html文档,然后读取出来。
private void ChangeWordToHtml(string docFilePath, string htmlFilePath) { ApplicationClass word = new ApplicationClass(); Type wordType = word.GetType(); Documents docs = word.Documents; Type docsType = docs.GetType(); object file = docFilePath; Document doc = (Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, (object)docs, new Object[] { file, true, true }); object nullobject = System.Reflection.Missing.Value; //判断与文件转换相关的文件是否存在,存在则删除。(这里,最好还判断一下存放文件的目录是否存在,不存在则创建) if (File.Exists(htmlFilePath)) { File.Delete(htmlFilePath); } //每一个html文件,有一个对应的存放html相关元素的文件夹(html文件名.files) if (Directory.Exists(htmlFilePath.Replace(".html", ".files"))) { Directory.Delete(htmlFilePath.Replace(".html", ".files"), true); } Type docType = doc.GetType(); object saveFileName = htmlFilePath; docType.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, WdSaveFormat.wdFormatHTML }); doc.Close(ref nullobj, ref nullobj,ref nullobj); // 退出 Word wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, word, null); }
private string LoadFileContent(string fileName) { if (fileName != "") { string path ="~/path/"; string docFilePath = Server.MapPath(path + fileName); string htmlFilePath = Server.MapPath(path + fileName.Split('.')[0] + ".html"); ChangeWordToHtml(docFilePath, htmlFilePath); if (File.Exists(htmlFilePath)) { string content = File.ReadAllText(htmlFilePath, Encoding.Default); return content; } } return null; }
相关文章推荐
- ASP.net:读取Word内容(包含表格,不包含图片)并且显示在页面中
- ASP.net:读取Word内容(包含表格,不包含图片)并且显示在页面中
- [转]ASP.NET C#如何读取word,写入word,复制内容到另一个word文档,批量修改文件名
- ASP.NET导出页面内容至word文档
- asp.net 读取 (导入) CSV文件内容 到页面 (数据库)
- ASP.NET读取word到页面
- ASP.NET C#如何读取word,写入word,复制内容到另一个word文档,批量修改文件名
- ASP.NET导出页面内容至word文档
- asp.net页面显示word文档内容
- Asp.Net页面输出到WORD、EXCEL、TXT、HTM等类型的文档
- ASP.net在页面所有内容生成后、输出内容前对页面内容进行操作
- ASP 如何读取 Word 档案内容并显示于网页
- html中读取asp.net向Cookie中写入的中文内容
- 利用ASP.NET输出指定内容的WORD、EXCEL、TXT、HTM
- 利用ASP.NET输出指定内容的WORD、EXCEL、TXT、HTM等类型的文档
- ASP.NET 用读取二进制代码的方法在页面上显示指定图片。
- asp.net两个页面内容相互回传
- 将ASP.NET页面内的数据导出到Excel 或 Word中
- ASP.NET AJAX - 内容页面一定要使用 ScriptManagerProxy 控件吗?
- asp.net 读取word 文档的方法