1. 程式人生 > >如何將word文檔內容在網頁顯示方法

如何將word文檔內容在網頁顯示方法

選擇 ted ace image alt real pdf esp baidu

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文檔內容在網頁顯示方法