游戏技术文章

asp.net 读取word 文档的方法

时间:2017-1-9 11:34:53  作者:棋牌资源网  来源:棋牌资源网  查看:6750  评论:0
内容摘要:资料一:适合读取并显示(简单而明了)第一种方法:   Response.ClearContent();  Response.ClearHeaders();  Response.ContentType = &qu...
资料一:适合读取并显示(简单而明了)

第一种方法:
   Response.ClearContent();
  Response.ClearHeaders();
  Response.ContentType = "Application/msword";
  string s=Server.MapPath("C#语言参考.doc");
   Response.WriteFile("C#语言参考.doc");
  Response.Write(s);
  Response.Flush();
    Response.Close();
第二种方法:

   Response.ClearContent();
  
   Response.ClearHeaders();
   
   Response.ContentType   =   "Application/msword";  

   string   strFilePath="";  

   strFilePath   =Server.MapPath("C#语言参考.doc");
 
   FileStream   fs   =   new   FileStream(strFilePath,FileMode.OpenOrCreate,FileAccess.Read);
      
   Response.WriteFile(strFilePath,0,fs.Length);

   fs.Close();

第三种方法:

string path=Server.MapPath("C#语言参考.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并显示与保存

 

//保存数据到当前客户端(可以传入一个要保存的文件名).
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 = window.document.Form1.updFile;
//获得客户端Word文件路径和文件
var UpFilue = window.document.Form1.updFile.value;
if(os__xmlFSO == null)
os__xmlFSO = new ActiveXObject("Scripting.FileSystemObject");

try
{
if(window.document.Form1.updFile.value == "")
{
alert('请选择对应的Word文件');
objUpFile.focus();
}
else if(UpFilue.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(UpFilue);
objDoc.SaveAs(os__localPath os__localFile, wdFormatHTML);
window.document.Form1.updFile.value = "";
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>");
//将转化后的值赋给页面控件txtIdea的值,我为了将Word值保存进数据库所以用<input type = "hidden" ..... 如果将Word内容显示可以考虑 window.document.Form1."你的显示控件ID".innerText = GetHtml;
window.document.Form1.txtIdea.value = 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>

注意页面上需要添加以下2个控件和对应的客户端事件


<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: 15.25%; HEIGHT: 23px" rows="50" cols="16" id="txtIdea"
runat="server">


其中txtIdea中的值就是客户端Word中的内容了,注意:需要调整IE的安全性设置,否则将无效.

标签:asp.net读取word文档的方法 

欢迎加入VIP,【VIP售价:只要288元永久VIP会员】畅享商业棋牌游戏程序下载,点击开通!

下载说明


☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,

☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!

☉唯一站长QQ:1004003180  [人格担保-本站注重诚信!]

☉购买建议E-mail:1004003180@qq.com   源码收购 E-mail:1004003180@qq.com    

☉本站文件解压密码  【文章内都自带解压密码,每个密码不同!】


本站提供的所有源码,均来源站长提供,仅学习交流 浙ICP备09009969号

由此产生不良后果和法律责任与本站无关,如果侵犯了您的版权,请来信告知 1004003180@qq.com 将及时更正和删除! 

Copyright © 2008-2024 棋牌资源网,你身边的棋牌资源下载站    All Rights Reserved