巧用数据流让 Word 文档在线阅读
常常写博客或空间日记的朋友,对网络编辑器(如图1,是CSDN的博客编辑器)并不陌生。也比較easy做出非常绚烂的排版。但这次在做一个BS的项目,客户一直在用Office的软件中的Word来编辑,并没用过这样的工具。非常陌生,“这里怎么设置行间距?”——让我瞬时摸不到头脑。在原版的系统中,客户公布信息时。索性用屏幕截图工具从Word中截图,然后粘到编辑器中,效果可想而知。
后来细想,既然习惯了用Word。何不把Word上传,然后在线预览呢?
查了一下资料,发现并不难。主要就是把Word转成Html格式。然后通过数据流读取。并显示出来!
注意:要先加入对 “Microsoft.Office.Interop.Word” 的引用
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhbmdsaWFuaGFpNTU1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
以下看一下代码实现:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Word = Microsoft.Office.Interop.Word; namespace TestWordOnline
{
public partial class demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string filename = WordToHtml("f:\\測试.doc");
//以gb2312格式的编码从字节流中读取字符
StreamReader fread = new StreamReader(filename, System.Text.Encoding.GetEncoding("gb2312"));
string ss = fread.ReadToEnd();
//打印数据
Response.Write(ss);
fread.Close();
fread.Dispose();
} /// <summary>
/// word转成html
/// </summary>
/// <param name="wordFileName"></param>
private string WordToHtml(object wordFileName)
{
//在此处放置用户代码以初始化页面
Word.Application word = new Word.Application();
Type wordType = word.GetType();
Word.Documents docs = word.Documents;
//打开文件
Type docsType = docs.GetType();
Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
//转换格式。另存为
Type docType = doc.GetType();
string wordSaveFileName = wordFileName.ToString();
string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html";
object saveFileName = (object)strSaveFileName;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
//退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
return saveFileName.ToString();
}
}
}
再看一下效果:
图3-Word版
图4-网页版
功能实现蛮简单的吧?我们用惯了这样的网络编程器。无形中把这样的习惯强加给了用户,孰不知恰好相反。程序是面向用户使用的,仅仅有让用户切身感觉到用得舒服。这才是每一个IT人士的出发点。
巧用数据流让 Word 文档在线阅读的更多相关文章
- asp.net如何实现word文档在线预览
原文:asp.net如何实现word文档在线预览 实现方式:office文档转html,再在浏览器里面在线浏览 1.首先引入com组件中office库,然后在程序集扩展中引入word的dll 2.将M ...
- word文档在线预览解决方案
花了一整天在网上翻关于 “word文档在线预览解决方案” 相关的资料,感觉实现难度比较大还是用PDF来解决好了.. 下面列一下比较好的参考资料吧 参考资料 前端实现在线预览pdf.word.xls.p ...
- Java实现word文档在线预览,读取office文件
想要实现word或者其他office文件的在线预览,大部分都是用的两种方式,一种是使用openoffice转换之后再通过其他插件预览,还有一种方式就是通过POI读取内容然后预览. 一.使用openof ...
- java 实现word文档在线预览
一.准备工具 1.通过第三方工具openoffice,将word.excel.ppt.txt等文件转换为pdf文件 下载地址:http://www.openoffice.org/download/in ...
- word文档在线预览地址
文档网址 http://www.officeweb365.com/Default/Docview 对接 http://ow365.cn/?i=19604&furl=http:://www.ba ...
- PDF/WORD/EXCEL/PPT 文档在线阅读
查资料看了2种解决方法: 1.通过办公软件dll转换,用flans去看 2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js) 今天我解决的就是WORD/EXCEL/PPT ...
- 在线预览-Java 使用 Print2Flash 实现Office文档在线阅读
近期项目上遇到一个需求是用户上传的文档进行在线浏览,之前有过一篇使用 OpenOffice 将 word 转换成 html 页面进行展示的.现在介绍一个新的工具那就是 Print2Flash . ...
- flexPaper +swftools实现文档在线阅读
网上已有很多FlexPaper仿百度文库的一些文章,园子里也有很多大牛的详细教程. 结合这次做的例子,在这里详细记录一下使用Flexpaper实现仿百度文库的效果,及自己在跟着园子里的教程做的时候,遇 ...
- pageoffice实现网页打开编辑保存word文档(基于SSM框架)
pageoffice是一款网页集成word.excel...等office工具 并不免费,但可以试用练习 SSM框架搭建过程就省略了 注意:由于谷歌/火狐升级,不支持插件嵌入,建议使用POBrowse ...
随机推荐
- msiexec
msiexec: runCmd = new String[]{ "msiexec", "/i", exeName, "/quiet", &q ...
- SQL 查找存在某内容的存储过程
--查找存在某表名的存储过程 SELECT distinct b.name from syscomments a,sysobjects b WHERE a.id=b.id and a.TEXT LIK ...
- ES6学习笔记(九)Set和Map数据结构
1.set 基本等于Java的Set集合类型,无序不可重复集,常被用来去重. 基本用法 const s = new Set();//通过Set()构造函数创建 [2, 3, 5, 4, 5, 2, 2 ...
- 把书《CUDA By Example an Introduction to General Purpose GPU Programming》读薄
鉴于自己的毕设需要使用GPU CUDA这项技术,想找一本入门的教材,选择了Jason Sanders等所著的书<CUDA By Example an Introduction to Genera ...
- 51nod 最大子矩阵和
一个M*N的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值. 我们可以降维,枚举矩形的长,然后算出一个一维数组,然后就转化成了最大字段和问题 #include< ...
- PHP保留两位小数
1.不四舍五入 $number = 23.43453;$english_format_number = number_format($number, 2, '.', '');echo $english ...
- SQL Server 函数的使用 Function
create table student ( id varchar2(5) primary key, name varchar2(20) not null, sex char(2) check(sex ...
- Quartus II sof文件转 jic文件
选择File->Convert Programming Files... Programming File Type选择JTAG Indirect ConfigurationFile(.jic) ...
- js---05 自定义属性
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 深入理解Android(1)——理解Android中的JNI(上)
我参加了CSDN博客之星评选,如果在过去的一段时间里阳光小强的博客对你有所帮助,在这里希望能投上您宝贵的一票,每天都可以投一次:http://vote.blog.csdn.net/blogstar20 ...