乔山办公网我们一直在努力
您的位置:乔山办公网 > word文档 > C#怎样将<em>word</em>转换成<em>HTML</em>源码

C#怎样将<em>word</em>转换成<em>HTML</em>源码

作者:乔山办公网日期:

返回目录:word文档


打开 HTML 文件,点击菜单栏 文件→使用 Microsoft Office Word 编辑,之后系统会自动打开 Word 并显示HTML文件的内容,这是保存即可。

  如果找不到“使用 Microsoft Office Word 编辑”的话,点击单栏 工具→Internet 选项→程序→ HTML 编辑器 → Microsoft Office Word → 确定。

C#实现的word转html命令行工具,对情况进行了解和思考后,认为可以通过zd一个中间程序自动化的将word文档转换成为excel,遂决定写个程序来实现这个转换环节。对于这种纯Windows的需求,估计也就是JAVA或者是VS系列语言更加方便一些。最近的项目使用的语言主要是JAVA、VC++,C#还没有尝试过完成实际项目,于是打算用C#尝试实现一下。
http://blog.csdn.net/solomonlangrui/article/details/47168449
要使用Microsoft office Interop library库,首先得在电脑上安装Office,然后添加如下三个com组件的引用:
Microsoft Office Excel library.
Microsoft Office Word library
Microsoft Office object library
作者编写了两个类DocToHtml和XlsToHtml用以转换e799bee5baa6e79fa5e98193e58685e5aeb9330Word和Excel文档,
public static IConverter Converter(string fullFilePath, string fileToSave)
{
switch (Path.GetExtension(fullFilePath).ToLower())
{
case ".doc":
case ".docx":
case ".dot":
case ".dotx":
case ".rtf":
return new DocToHtml { FileToSave = fileToSave, FullFilePath = fullFilePath };
case ".xls":
case ".xlsx":
return new XlsToHtml { FileToSave = fileToSave, FullFilePath = fullFilePath };
default:
throw new NotSupportedException();
}
}
使用方法如下:
static void Main(string[] args)
{
var converter = ConverterLocator.Converter(@"r:\1.xlsx", @"r:\1.html");
var html = converter.Convert();
}
原文提供了代码的下载,使用了一下,觉得有两个问题:
1. 该代码转换后会删除原始文件,并且也不会保留转换后的文件,仅仅返回string类型的html。
转换后删除原始文件是不对的,并且string类型的html文件有并不能完全代表结果(很多时候还有一些图片或样式表之类的附加文件)。因此我把代码修改了一下,不删除原始文件和转换后的文件。
2. 转换带图片的文档时会报异常
分析了一下,转换带图片的文档后,会生成一个xxx.files的文件夹用以存放文档,但是作者的代码中是xxx_files文件夹(可能是操作系统或office的版本不一样所致),我这里改成了xxx.files后就不报异常了。

日常生活中,我们总是在Word中进行文字的7a64e59b9ee7ad94334编辑,它不仅能够保存Text文本,还可以保存文本的格式等等。那么如果我要将一Word文档上的内容展示在网页上,该怎么做呢?这里我提供了一个小工具,你可以将Word转换为Html,需要显示的话,可以直接访问该Html,废话不多说,下面看代码。
页面代码:

[html] view plaincopy
<span style="font-size:18px;"><div>
<input id="File1" type="file" runat="server"/>
<asp:Button ID="btnConvert" runat="server" Text="转换" OnClick="btnConvert_Click" />
</div></span>
C#代码:

[csharp] view plaincopy
<span style="font-size:18px;">using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

protected void Page_Load(object sender, EventArgs e)
{

}

/// <summary>
/// 将word转换为Html
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnConvert_Click(object sender, EventArgs e)
{
try
{

//上传
//uploadWord(File1);
//转换
wordToHtml(File1);
}
catch (Exception ex)
{
throw ex;
}
finally
{
Response.Write("恭喜,转换成功!");
}

}

//上传文件并转换为html wordToHtml(wordFilePath)
///<summary>
///上传文件并转存为html
///</summary>
///<param name="wordFilePath">word文档在客户机的位置</param>
///<returns>上传的html文件的地址</returns>
public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
{
Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
Type wordType = word.GetType();
Microsoft.Office.Interop.Word.Documents docs = word.Documents;

// 打开文件
Type docsType = docs.GetType();

//应当先把文件上传至服务器然后再解析文件为html
string filePath = uploadWord(wordFilePath);

//判断是否上传文件成功
if (filePath == "0")
return "0";
//判断是否为word文件
if (filePath == "1")
return "1";

object fileName = filePath;

Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });

// 转换格式,另存为html
Type docType = doc.GetType();

string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();

// 判断指定目录下是否存在文件夹,如果不存在,则创建
if (!Directory.Exists(Server.MapPath("~\\html")))
{
// 创建up文件夹
Directory.CreateDirectory(Server.MapPath("~\\html"));
}

//被转换的html文档保存的位置
string ConfigPath = HttpContext.Current.Server.MapPath("html/" + filename + ".html");
object saveFileName = ConfigPath;

/*下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
* docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
* null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
* 其它格式:
* wdFormatHTML
* wdFormatDocument
* wdFormatDOSText
* wdFormatDOSTextLineBreaks
* wdFormatEncodedText
* wdFormatRTF
* wdFormatTemplate
* wdFormatText
* wdFormatTextLineBreaks
* wdFormatUnicodeText
*/
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });

//关闭文档
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] { null, null, null });

// 退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
//转到新生成的页面
return ("/" + filename + ".html");

}

public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)
{
if (uploadFiles.PostedFile != null)
{
string fileName = uploadFiles.PostedFile.FileName;

int extendNameIndex = fileName.LastIndexOf(".");
string extendName = fileName.Substring(extendNameIndex);
string newName = "";
try
{
//验证是否为word格式
if (extendName == ".doc" || extendName == ".docx")
{

DateTime now = DateTime.Now;
newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString();

// 判断指定目录下是否存在文件夹,如果不存在,则创建
if (!Directory.Exists(Server.MapPath("~\\wordTmp")))
{
// 创建up文件夹
Directory.CreateDirectory(Server.MapPath("~\\wordTmp"));
}

//上传路径 指当前上传页面的同一级的目录下面的wordTmp路径
uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));
}
else
{
return "1";
}
}
catch
{
return "0";
}
//return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName;
return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);
}
else
{
return "0";
}
}</span>

相关阅读

  • C#怎样将<em>word</em>转换成<em>HTML</em>源码

  • 乔山办公网word文档
  • 知打开 HTML 文件,点击菜单栏 文件→使用 Microsoft Office Word 编辑,之后系统会自动打开 Word 并显示HTML文件的内容,这是保道存即可。  如果找不到“使用 Microsoft Office Word 编辑”的话
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网