asp.net 读取Word

简介: 本例下载:http://files.cnblogs.com/scottckt/ReadWord.rar   方法1:使用Js读取。代码: DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.

本例下载:http://files.cnblogs.com/scottckt/ReadWord.rar

 

方法1:使用Js读取。代码:

复制代码
<%@ Page Language= " C# " AutoEventWireup= " true " CodeFile= " js读取word.aspx.cs " Inherits= " js读取word " %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head  runat ="server" >

     < script  language ='javascript' >
         // 默认word转化文件放于C:\\下
         var os__localPath = "C:\\";
         // 保存的文件名
            var os__localFile = "defaultFileWord.htm";
            var os__xmlDom =  new ActiveXObject("MSXML2.DOMDocument");
            var os__xmlFSO ;

            // 保存数据到当前客户端(可以传入一个要保存的文件名).
            function os_SaveToLocal()
           {
             var _saveAs = "";
             if(arguments.length > 0)
             _saveAs = arguments[0] + "";
             else
             _saveAs = os__localFile;
            
             try
            {       
              if(os__xmlFSO ==  null)
              os__xmlFSO =  new ActiveXObject("Scripting.FileSystemObject");  
          
            }
             catch(e){window.alert(e);}
           } 
          
            // Word转化为Html文件
            function WorcChangeHtml()
           {
                 var os_xmlFSO;
                 // 获得上传控件对象
                 var objUpFile = document.getElementById("updFile");
                 // 获得客户端Word文件路径和文件
                 var UpFileValue = objUpFile.value;
                 if(os__xmlFSO ==  null)
                  os__xmlFSO =  new ActiveXObject("Scripting.FileSystemObject");     
                
                 try
                {
                      if(UpFileValue == "")
                     {
                          alert('请选择对应的Word文件');
                          objUpFile.focus();
                     }
                      else  if(UpFileValue.indexOf(".doc") == -1)
                     {
                          alert('您选择的不是Word文件 \r\n请选择正确的Word文件');
                          objUpFile.focus();
                     }   
                      else  if(!os__xmlFSO.FileExists(objUpFile.value))
                     {
                          alert('对应的Word文件不存在');
                          objUpFile.focus();    
                     }
                      else
                     {
                           var wdFormatHTML = 8;
                           var objWord =  new ActiveXObject("Word.Application");
                          objWord.Application.Visible =  false;
                           var objDoc = objWord.Documents.Open(UpFileValue);
                          objDoc.SaveAs(os__localPath+os__localFile, wdFormatHTML);
                          
                          UpFileValue = "";
                          objDoc.Close();
                          objWord.Quit();
                           var GetHtml = GetLine();
                           var iBeginIndex = GetHtml.indexOf("<body");
                           var iEndIndex = GetHtml.lastIndexOf("</body>");
                         
                          GetHtml =  GetHtml.substring(iBeginIndex,iEndIndex+7).replace("<body","<div");
                          GetHtml = GetHtml.replace("</body>","</div>");
                          
                          GetHtml = GetHtml.replace("lang=ZH-CN","");                          
                          GetHtml = GetHtml.replace("lang=EN-US","");
                          GetHtml = GetHtml.replace("style='mso-spacerun:yes'","");
                          GetHtml = GetHtml.replace("class=MsoNormal","");
                                                   

                           var vTxtIdea= document.getElementById("txtIdea");
                          document.write(GetHtml)
                 }
                }            
                 catch(e)
                {
                    window.alert(e);
                 }
           }
          
            // 读取文本文件
            function GetLine()
           {
                 var fso, txtfile, strValue;
                 var ForReading = 1, ForWriting = 2;
                fso =  new ActiveXObject("Scripting.FileSystemObject");
                txtfile = fso.OpenTextFile(os__localPath+os__localFile, ForReading);
                 while(!txtfile.AtEndOfStream)
                {
                 strValue = strValue + txtfile.ReadLine();
                }
                txtfile.Close();
                 return(strValue);
           }
     </ script >

     < title >无标题页 </ title >
</ head >
< body >
     < form  id ="form1"  runat ="server" >
         < input  id ="updFile"  type ="file"  style ="border-right: 1px solid; border-top: 1px solid;
            border-left: 1px solid; width: 77.46%; border-bottom: 1px solid; height: 26px"

            size
="71" >
         < input  style ="border-right: #999999 1px solid; border-top: #999999 1px solid; font-size: 15pt;
            border-left: #999999 1px solid; width: 103px; border-bottom: #999999 1px solid;
            height: 28px"
 onclick ="WorcChangeHtml()"  runat ="server"  id ="btnUpLoad"  type ="submit"
            value
="导入"  name ="btnUpLoad" >
         < textarea  style ="width: 88%; height: 412px"  id ="txtIdea"  runat ="server" ></ textarea >
         < br  />
         < br  />

     </ form >
</ body >
</ html >
复制代码

 

 方法2,将Word文件上传到服务器端后,在读取。 

复制代码
  private  void ReadWord()
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType =  " Application/msword ";
         string s = Server.MapPath( " test.doc ");
        Response.WriteFile( " test.doc ");
        Response.Write(s);
        Response.Flush();
        Response.Close();
    }

     private  void ReadWord2()
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType =  " Application/msword ";
         string strFilePath =  "";
        strFilePath = Server.MapPath( " test.doc ");
        FileStream fs =  new FileStream(strFilePath, FileMode.Open, FileAccess.Read,FileShare.Read);
        Response.WriteFile(strFilePath,  0, fs.Length);
        fs.Close(); 
    }

     private  void ReadWord3()
    {
         string path = Server.MapPath( " test.doc ");
        FileInfo file =  new FileInfo(path);
        FileStream myfileStream =  new FileStream(path, FileMode.Open, FileAccess.Read);
         byte[] filedata =  new Byte[file.Length];
        myfileStream.Read(filedata,  0, ( int)(file.Length));
        myfileStream.Close();
        Response.Clear();
        Response.ContentType =  " application/msword ";
        Response.AddHeader( " Content-Disposition "" attachment;filename=文件名.doc ");
        Response.Flush();
        Response.BinaryWrite(filedata);
        Response.End(); 
    }
复制代码

 

 其它说明:

       我原本想后台读取word后,在页面的某一页面中显示,但没找到方法。如果有谁知道,烦请告知我。 

 

http://www.cnblogs.com/scottckt/archive/2009/06/24/1510253.html

目录
相关文章
|
.NET 开发框架
|
.NET
Asp.Net 操作word 第二篇[推荐]
引言:前段时间有项目要用c#生成Word格式的计算报告,通过网络查找到很多内容,但是都很凌乱,于是自己决定将具体的步骤总结整理出来,以便于更好的交流和以后相似问题可以迅速的解决! 现通过具体的示例演示具体的步骤: 第一步,制作模板 1,新建一个文档,文档内容如下: 图1 2,在相应位置...
987 0
asp.net 操作word
参考一:点击这里 参考二:点击这里 参考三:点击这里 using System; using System.Web.Security; using Microsoft.Office.Interop.
821 0
|
.NET 开发框架 数据库
asp.net与word文档在线
1.通过javascript打开\编辑\根据模板新建word文档                       //"SharePoint.OpenDocuments.1"可与Office XP兼容             var openDocObj = new ActiveXObject("SharePoint.
861 0
|
.NET C# 开发框架
asp.net 读取word 文档的方法新随笔 1. net 学习线路图,csdn真给力啊!
第一种方法:    Response.ClearContent();   Response.ClearHeaders();   Response.ContentType = "Application/msword";   string s=Server.
935 0
|
JavaScript .NET 开发框架
asp.net将word另存为html并输出到网页
http://hi.baidu.com/xuejianxiyang/item/3add1c366b97f4483175a1e3 添加引用:com/Microsoft office 11.
1054 0
|
.NET 开发框架
asp 使用word模板生成word文件
需求: 根据word模板生成对应内容的word文档可供下载 解决: 打开word组件的权限。 步骤: 控制面板》管理工具》组件服务》计算机》我的电脑》DCOM配置》Microsoft Word 97 - 2003 文档》属性》 标识》下列用户》administrator》确定 ...
1014 0