首先声明文章不是原创,摘抄网络的。
增加
Microsoft.Office.Interop.Word.dll引用。前台页面代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>页面上传</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="File1" type="file" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="上传转换"
onclick="btnUpload_Click" />
</div>
</form>
</body>
</html>
后台代码:
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//public WordToHTML() { }
//上传文件并转换为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();
//被转换的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")
{
DateTime now = DateTime.Now;
newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString();
//上传路径 指当前上传页面的同一级的目录下面的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";
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
//上传
//uploadWord(File1);
//转换
wordToHtml(File1);
}
catch (Exception ex)
{
throw ex;
}
finally
{
Response.Write("恭喜,转换成功!");
}
}
}
Word大小限制,限制Word文件大小为300k
<httpRuntime maxRequestLength="300" executionTimeout="3600" />
</system.web>
增加Global.asax文件,进行文件大小限制
<%@ Import Namespace="System.Web.Configuration" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
//本代码的功能是检查页面请求的大小,如果超过了配置文件maxRequestLength的设定值,就提示用户超过了所允许的文件大小。
//从配置文件里得到配置的允许上传的文件大小
HttpRuntimeSection runTime = (HttpRuntimeSection)WebConfigurationManager.GetSection("system.web/httpRuntime");
//maxRequestLength 为整个页面的大小,不仅仅是上传文件的大小,所以扣除 100KB 的大小,
//maxRequestLength单位为KB
int maxRequestLength = (runTime.MaxRequestLength - 100) * 1024;
//当前请求上下文的HttpApplication实例
HttpContext context = ((HttpApplication)sender).Context;
//判断请求的内容长度是否超过了设置的字节数
if (context.Request.ContentLength > maxRequestLength)
{
//得到服务对象
IServiceProvider provider = (IServiceProvider)context;
HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
//检查请求是否包含正文数据
if (workerRequest.HasEntityBody())
{
//请求正文数据的长度
int requestLength = workerRequest.GetTotalEntityBodyLength();
//得到加载的初始字节数
int initialBytes = 0;
if (workerRequest.GetPreloadedEntityBody() != null)
initialBytes = workerRequest.GetPreloadedEntityBody().Length;
//检查是否所有请求数据可用
if (!workerRequest.IsEntireEntityBodyIsPreloaded())
{
byte[] buffer = new byte[512000];
//设置要接收的字节数为初始字节数
int receivedBytes = initialBytes;
//读取数据,并把所有读取的字节数加起来,判断总的大小
while (requestLength - receivedBytes >= initialBytes)
{
//读取下一块字节
initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);
//更新接收到的字节数
receivedBytes += initialBytes;
}
initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);
}
}
//请求重定向到上载页面,并给用户提示信息。
context.Response.Redirect(this.Request.Url.LocalPath + "?error=" + Server.UrlEncode("您上传的文件超过了允许的大小。"));
}
}
void Application_End(object sender, EventArgs e)
{
//在应用程序关闭时运行的代码
}
void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
//在新会话启动时运行的代码
}
void Session_End(object sender, EventArgs e)
{
//在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式
//设置为 StateServer 或 SQLServer,则不会引发该事件。
}
</script>