using System.Web.UI.WebControls;
using System.IO;
using Word = Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;没有引用好组件时,此处会报错,找不到Interop之类的,
这时需要在引用里面引用组件.net下的Microsoft.Office.Interop.Visio,Microsoft.Office.Interop.Word
后台代码如下:
protected void Page_Load(object sender, EventArgs e)
string relativePath = Request.QueryString["FilePath"]; //相对路径 ,从跳转页面得到文件相对路径。
if (relativePath == "" || relativePath==null) return;
string serverPath = Server.MapPath(relativePath); //相对转服务器对应路径
string html = serverPath.Replace(".doc", ".html");
if (!File.Exists(@html)) //html页面不存在,把word转换成html
string filename = WordToHtml(serverPath);
StreamReader fread = new StreamReader(filename, System.Text.Encoding.GetEncoding("gb2312"));
string ss = fread.ReadToEnd();
Response.Write(ss); //直接写字符串到网页会发现,文字可显示,图片、表格无法显示。因此在后面重跳转到html文件页面。
fread.Close();
fread.Dispose();
html = relativePath.Replace(".doc", ".html");
//html文件也存储在同样的路径下,
//只需要改了原路径的后缀即可得到html文件路径
Response.Redirect(html);
return;
/// word转成html
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();
最近在做word文档在线浏览,找了种种方法、控件之后,回归到word转HTML,在线浏览....一下是后台代码,前台html页面默认代码即可。因为用文件如下:using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI....
该源码主要是通过
Asp.net
2.0
实现
Word文档
上传并自动
转换
为
Html
文件,原理是将
word文档
上传至服务器然后再
转
存为
html
格式文件,再解析
html
文件修改其页面样式和css。
word
Tmp为上传是
word
暂存文件夹
html
为
转换
后
html
保存文件夹
基本思路:把
Word
文件上传到服务器,读取其内容存储为
Html
,然后加载
Html
内容1:使用Microsoft.Office.Interop.
Word
组件 这是比较常用的一种方式,代码就不贴出了,网上大把的例子 缺点:服务器需要装
Word
的组件,并且需要在服务器上设置Docm+对象的权限,如果一台服务器还好,如果项目应用到多台不同服务器,就比较繁琐了2: OpenXml API 可以将.docx(
word
97-2003 不适用)
转
化为XML,有了XML,想
转
成
HTML
或是其他格式都不再是问题了,此Api需要 .net FreamWork3.5+ Office2007
asp.net
实现
word
转
html
文件
为了解决此问题,这几天找了很多种方法,包括在网页
中
不用fileupload来打开电脑
中
的文件
(不得不说web还是存在一定弊端的)
包括在IIS管理器
中
设置目录
浏览
功能(虽然有一定的进展,但是目前还是没能攻破这一难处),话不多说先上主题。
首先可以看一下
word
中
显示的内容:
网页
中
呈现的图示:
可以看到文档
中
的文字、数学公式以及图片都能
转
化出来。格式还没有彻底的测试,但是可以肯定不会有大的变化。。
Web.config
中
的内容
<?xml ver
Microsoft.Office.Interop.
Word
.dll Microsoft.Office.Interop.Excel.dllInterop.Microsoft.Office.Core.dll三合一文件
如果在VS引用
中
找不到,可以用附件
中
的dll添加到项目
中
进行引用。
在附近
中
附有
word
转
HTML
DEMO
利用
Word
.Application提供的方法,可以很轻易地将
Word文档
转
化为
HTML
等其它格式,下面就是
实现
的全部的代码:
Visual C#
Word
To
Html
.aspx
<%@ Page language="c#" Codebehind="
Word
To
Html
.aspx.cs" AutoEven...
日常生活
中
,我们总是在
Word
中
进行文字的编辑,它不仅能够保存Text文本,还可以保存文本的格式等等。那么如果我要将一
Word文档
上的内容展示在网页上,该怎么做呢?这里我提供了一个小工具,你可以将
Word
转换
为
Html
,需要显示的话,可以直接访问该
Html
,废话不多说,下面看代码。
页面代码:
C#代码:
using System;
using System.Data;
ASP.NET
是一种用于构建 Web 应用程序的开发框架,而
html
2pdf.js 是一个 JavaScript 库,可以将
HTML
格式的内容
转换
为 PDF 格式的文档。
ASP.NET
可以与
html
2pdf.js 结合使用,
实现
将
ASP.NET
生成
的
HTML
页面
转换
为 PDF 文档。首先,我们需要在
ASP.NET
程序
中
引入
html
2pdf.js 的 JavaScript 文件。可以通过在
HTML
页面的头部添加以下代码
实现
:
```
html
<script src="
html
2pdf.js"></script>
然后,我们可以使用
html
2pdf.js 提供的 API 来进行
转换
。例如,我们可以在
ASP.NET
页面
中
添加一个按钮,当点击该按钮时,触发 JavaScript 代码将当前页面的
HTML
内容
转换
为 PDF 文档:
```
html
<button onclick="convert
Html
ToPdf()">保存为PDF</button>
<script>
function convert
Html
ToPdf() {
// 获取需要
转换
的
HTML
元素
var element = document.getElementById('content');
// 使用
html
2pdf.js 将
HTML
转换
为 PDF
html
2pdf().from(element).save();
</script>
上述代码
中
,我们使用 `getElementById` 方法获取了 id 为 "content" 的
HTML
元素,这是一个
ASP.NET
页面
中
的示例。你可以根据实际情况修改此处的代码,以适应你的
ASP.NET
页面。
当用户点击“保存为PDF”按钮时,`convert
Html
ToPdf` 函数会调用 `
html
2pdf` 方法将
HTML
元素
转换
为 PDF,并使用 `save` 方法保存成文件。
综上所述,
ASP.NET
可以与
html
2pdf.js 结合使用,
实现
将
ASP.NET
页面
转换
为 PDF 文档的功能。这样,用户就可以通过在
ASP.NET
页面上点击按钮,将页面内容以 PDF 格式保存到本地或进行其他操作。