这个是之前写的,用线程操作<%@ WebHandler Language="C#" Class="ajaxWordToHtml" %>using System; using System.Web; using System.Threading; public class ajaxWordToHtml : IHttpHandler { private static object _lock = new object(); string docPath = string.Empty; string htmlPath = string.Empty; bool Flang = false; string Message = string.Empty; [STAThread] private void _Import() { object o = Type.Missing; try { Word.Application app = new Word.Application(); app.Visible = false; object docFile = docPath; object readOnly = true; object fileName = htmlPath; /*office 2003 begin Word.Document doc = app.Documents.Open(ref docFile, ref o, ref readOnly, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o); object format = Word.WdSaveFormat.wdFormatFilteredHTML; doc.SaveAs(ref fileName, ref format, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o); //office 2003 end*/ //* offic 2000 begin Word.Document doc = app.Documents.Open(ref docFile, ref o, ref readOnly, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o); object format = Word.WdSaveFormat.wdFormatHTML; doc.SaveAs(ref fileName, ref format, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o); doc.Close(ref o, ref o, ref o); app.Quit(ref o, ref o, ref o); //*/ //WordToHtml.ConvertToHtml(docPath, htmlPath); Flang = true; Message = "已成功生成!"; return; } catch (Exception error) { Flang = false; Message = error.Message; return; } finally { GC.Collect(); } } public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; docPath = HttpContext.Current.Request["docPath"]; htmlPath = HttpContext.Current.Request["htmlPath"]; if (!string.IsNullOrEmpty(docPath) && !string.IsNullOrEmpty(htmlPath)) { lock (_lock) { docPath = HttpContext.Current.Server.MapPath("/docfile/" + docPath); htmlPath = HttpContext.Current.Server.MapPath("/htmlfile/" + htmlPath); Thread _thread = new Thread(new ThreadStart(_Import)); _thread.Name = "ajaxWordToHtml"; _thread.SetApartmentState(ApartmentState.STA); _thread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; _thread.Start(); TimeSpan ts = new TimeSpan(0, 0, 0, 30); _thread.Join(); context.Response.Write(Flang); context.Response.Write(Message); } } else { Flang = false; Message = "提交的参数不正确"; } } public bool IsReusable { get { return false; } }}
using System.Web;
using System.Threading;
public class ajaxWordToHtml : IHttpHandler
{ private static object _lock = new object();
string docPath = string.Empty;
string htmlPath = string.Empty;
bool Flang = false;
string Message = string.Empty; [STAThread]
private void _Import()
{ object o = Type.Missing; try
{ Word.Application app = new Word.Application();
app.Visible = false;
object docFile = docPath;
object readOnly = true;
object fileName = htmlPath;
/*office 2003 begin
Word.Document doc = app.Documents.Open(ref docFile, ref o, ref readOnly, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
object format = Word.WdSaveFormat.wdFormatFilteredHTML;
doc.SaveAs(ref fileName, ref format, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
//office 2003 end*/ //* offic 2000 begin
Word.Document doc = app.Documents.Open(ref docFile, ref o, ref readOnly, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
object format = Word.WdSaveFormat.wdFormatHTML;
doc.SaveAs(ref fileName, ref format, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
doc.Close(ref o, ref o, ref o);
app.Quit(ref o, ref o, ref o); //*/ //WordToHtml.ConvertToHtml(docPath, htmlPath);
Flang = true;
Message = "已成功生成!";
return;
}
catch (Exception error)
{
Flang = false;
Message = error.Message;
return;
}
finally
{
GC.Collect();
}
} public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
docPath = HttpContext.Current.Request["docPath"];
htmlPath = HttpContext.Current.Request["htmlPath"];
if (!string.IsNullOrEmpty(docPath) && !string.IsNullOrEmpty(htmlPath))
{
lock (_lock)
{
docPath = HttpContext.Current.Server.MapPath("/docfile/" + docPath);
htmlPath = HttpContext.Current.Server.MapPath("/htmlfile/" + htmlPath);
Thread _thread = new Thread(new ThreadStart(_Import));
_thread.Name = "ajaxWordToHtml";
_thread.SetApartmentState(ApartmentState.STA);
_thread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
_thread.Start();
TimeSpan ts = new TimeSpan(0, 0, 0, 30);
_thread.Join();
context.Response.Write(Flang);
context.Response.Write(Message);
}
}
else
{
Flang = false;
Message = "提交的参数不正确";
}
} public bool IsReusable
{
get
{
return false;
}
}}