一直以来想在网站里实现直接将Word文件上传并自动转换成Html文件(适当过滤比较废话的代码),在网上搜了,有不少结果,但真正能用的去没有几个,也不知道是哪位能人的代码,被到处Copy了。
网上找来的基本上都是这个样子:
//创建word实例 |
02 | Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass(); |
03 | Type wordType = word.GetType(); |
04 | //文档s |
05 | Microsoft.Office.Interop.Word.Documents docs = word.Documents; |
06 | Type docsType = docs.GetType(); |
07 | //打开文档 |
08 | Microsoft.Office.Interop.Word.Document doc = (Document)docsType.InvokeMember( "Open" ,System.Reflection.BindingFlags.InvokeMethod, null , docs, new Object[] {fileName, true , true }); |
09 | Type doctype = doc.GetType(); |
10 | //另存为 |
11 | doctype.InvokeMember( "SaveAs" , System.Reflection.BindingFlags.InvokeMethod, null , doc, new object [] {savefilename, WdSaveFormat.wdFormatHTML}); //另存为Html格式 |
12 | //关闭 |
13 | wordType.InvokeMember( "Quit" , System.Reflection.BindingFlags.InvokeMethod, null , word, null ); |
经本人测试基本没什么用。后来几经摸索,终于将基本功能实现了,请大家看看还有什么问题不??
1、到
http://www.dllzj.com/下载Microsoft.Office.Interop.Word.dll,有各种版本的,我用的是11.0(Word2003)的;
2、添加引用,VS会自动拷贝一份到bin文件夹;
3、实现html代码;
<html xmlns="
http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET Word 文件上传于转换 Demo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="File1" type="file" runat="server" />
<br />
<br />
<asp:Button ID="btnUpload" runat="server" Text="上传转换" οnclick="btnUpload_Click" />
<head runat="server">
<title>ASP.NET Word 文件上传于转换 Demo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="File1" type="file" runat="server" />
<br />
<br />
<asp:Button ID="btnUpload" runat="server" Text="上传转换" οnclick="btnUpload_Click" />
</div>
</form>
</body>
</html>
</form>
</body>
</html>
4、C#的代码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// 上传文件并转存为htm
public string Doc2Html(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;
public string Doc2Html(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();
Type docsType = docs.GetType();
//应当先把文件上传至服务器然后再解析文件为html
string filePath = uploadDoc(wordFilePath);
string filePath = uploadDoc(wordFilePath);
//判断是否上传文件成功
if (filePath == "0")
return "0";
//判断是否为word文件
if (filePath == "1")
return "1";
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 });
System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });
// 转换格式,另存为html
Type docType = doc.GetType();
string strfileName = fileName.ToString();
Type docType = doc.GetType();
string strfileName = fileName.ToString();
//获取转换文件的文件名
int fullNameIndex = strfileName.LastIndexOf("//");
string filename = strfileName.Substring(fullNameIndex);
string pattern = @"(.doc)";
filename = System.Text.RegularExpressions.Regex.Replace(filename,pattern,String.Empty);
//被转换的html文档保存的位置
string ConfigPath = HttpContext.Current.Server.MapPath("Html/" + filename + ".html");
object saveFileName = ConfigPath;
int fullNameIndex = strfileName.LastIndexOf("//");
string filename = strfileName.Substring(fullNameIndex);
string pattern = @"(.doc)";
filename = System.Text.RegularExpressions.Regex.Replace(filename,pattern,String.Empty);
//被转换的html文档保存的位置
string ConfigPath = HttpContext.Current.Server.MapPath("Html/" + filename + ".html");
object saveFileName = ConfigPath;
/*其它格式:
* wdFormatHTML
* wdFormatDocument
* wdFormatDOSText
* wdFormatDOSTextLineBreaks
* wdFormatEncodedText
* wdFormatRTF
* wdFormatTemplate
* wdFormatText
* wdFormatTextLineBreaks
* wdFormatUnicodeText
*/
* 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 });
null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
//转到新生成的页面
return ("Html/" + filename + ".html");
}
//转到新生成的页面
return ("Html/" + filename + ".html");
}
public string uploadDoc(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)
{
if (uploadFiles.PostedFile != null)
{
string fileName = uploadFiles.PostedFile.FileName;
string fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
string newfileName = "";
{
if (uploadFiles.PostedFile != null)
{
string fileName = uploadFiles.PostedFile.FileName;
string fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
string newfileName = "";
try
{
//验证是否为word格式
if (fileExtension == ".doc")
{
DateTime now = DateTime.Now;
newfileName = now.Year.ToString() + now.Month.ToString() + now.Day.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString();
//上传路径 指当前上传页面的同一级的目录下面的Doc路径
uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("Doc/" + newfileName + fileExtension));
}
else
{
return "1";
}
}
catch
{
return "0";
}
return System.Web.HttpContext.Current.Server.MapPath("Doc/" + newfileName + fileExtension);
}
{
//验证是否为word格式
if (fileExtension == ".doc")
{
DateTime now = DateTime.Now;
newfileName = now.Year.ToString() + now.Month.ToString() + now.Day.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString();
//上传路径 指当前上传页面的同一级的目录下面的Doc路径
uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("Doc/" + newfileName + fileExtension));
}
else
{
return "1";
}
}
catch
{
return "0";
}
return System.Web.HttpContext.Current.Server.MapPath("Doc/" + newfileName + fileExtension);
}
else
{
return "0";
}
}
{
return "0";
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
Doc2Html(File1);
}
catch (Exception ex)
{
throw ex;
}
finally
{
Response.Write("恭喜,转换成功!");
}
}
}
{
try
{
Doc2Html(File1);
}
catch (Exception ex)
{
throw ex;
}
finally
{
Response.Write("恭喜,转换成功!");
}
}
}
可以参考的一些文章: