分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

如何将word文档内容在网页显示方法

发布时间:2023-09-06 02:02责任编辑:苏小强关键词:word

doc和docx格式是无法直接在浏览器中显示的,需要转换为浏览器支持的格式,方法如下:

1、打开需要转换的文件,点击文件选择另存为;

2、选择保存的格式为单个网页(*.mht ,*.mhtl)、网页(*.hmt ,*.hmtl )或者筛选过的网页(*.hmt ,*.hmtl )即可。

把word文件读到byte[]中,再Response.OutputStream.Write(bytes)到客户端去 

Page_Load事件中写:        

  //FileStream   fs   =   new   FileStream("c:\\1.doc",   FileMode.Open,   FileAccess.Read);  FileStream fs = new FileStream(Server.MapPath("files\\1.doc"),FileMode.Open,   FileAccess.Read);      

  byte[]   bytes   =   new   byte[(int)fs.Length];  

  fs.Read(bytes,   0,   bytes.Length);  

  fs.Close();  

  Response.ContentType   =   "application/msword";  

  //Response.OutputStream(bytes);   

Response.OutputStream.Write(bytes,0, bytes.Length);

  Response.End();

在网页中显示.txt文件的内容:

直接就可以显示,如果想放到表格中,可以<iframe   src="XXX.txt"></iframe>

word文件也可以照此。

为了正确处理word等格式,你需要在HTML文件中设置好该文件类型,比如:

<meta http-equiv="Content-Type" content="Application/msword">

还有其它经常设置的文件类型:

Application/msword Microsoft Word Document

application/pdf PDF Document

application/wordperfect6.0 WordPerfect 6.0 Document

application/zip ZIP archive

audio/x-wav WAV audio format

audio/midi MIDI audio format

audio/x-pn-realaudio RealAudio

image/gif GIF image format

image/jpeg JPEG image format

image/png PNG image format

text/html HTML document

text/plain Plain text

video/mpeg MPEG video format

video/quicktime QuickTime video format

video/x-msvideo AVI video format

文章来源于易贤网http://www.ynpxrz.com/n765461c2023.aspx

还有一种,做参考

response.setContentType("application/vnd.ms-excel"); 
File file =new File("D:/test.xls"); 
FileInputStream in=new FileInputStream(file); 
byte[] buffer=new byte[in.available()]; 
in.read(buffer); 
response.getOutputStream().write(buffer); 
response.getOutputStream().flush();

如何将word文档内容在网页显示方法

原文地址:https://www.cnblogs.com/sunshinezjb/p/9248237.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved