【发布时间】:2016-01-08 11:28:37
【问题描述】:
我在 Windows 2003 Server 32 位操作系统上部署了一个 Web 应用程序(在 VS 2008 中开发)。 我的应用程序使用 Microsoft Office Word(我正在使用它来生成 PDF)。 应用程序运行良好,正在生成 .Doc 和 .Pdf。 但是,一旦我重新启动服务器,我只会第一次收到上述错误 (8007007E),在我刷新页面后,应用程序再次开始正常工作。 我查找了错误的各种原因,但没有一个与我的匹配。 通常,当此错误发生时,它会导致 COM 使用完全崩溃。 代码示例如下。
string htmlContent = div_Data.InnerHtml;
string filename = strLoanNo + strTime + ".doc";
if (!Directory.Exists(Server.MapPath("~/Doc/")))
{
Directory.CreateDirectory(Server.MapPath("~/Doc/"));
}
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
div_Data.RenderControl(hw);
FileStream fs = new FileStream(Server.MapPath("~/Doc/") + filename,FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.Write(tw.ToString());
sw.Flush();
sw.Close();
fs.Close();
object fileName = Server.MapPath("~/Doc/") + filename;
object missing = System.Reflection.Missing.Value;
object readOnly = false;
object isVisible = true;
object SaveChanges = true;
Microsoft.Office.Interop.Word.ApplicationClass appWord = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document oWordDoc = appWord.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing, ref missing);
oWordDoc.ExportAsFixedFormat(Server.MapPath("~/Doc/") + strLoanNo + "_" + strTime + ".pdf", WdExportFormat.wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 1, 1, WdExportItem.wdExportDocumentWithMarkup, true, false, WdExportCreateBookmarks.wdExportCreateNoBookmarks, false, true, true, ref missing);
((_Document)oWordDoc).Close(ref SaveChanges, ref missing, ref missing);
appWord.Quit(ref SaveChanges, ref missing, ref missing);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(appWord);
Response.Clear();
Response.Charset = "";
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + strLoanNo + "_" + strTime + ".pdf");
Response.WriteFile(Server.MapPath("~/Doc/") + strLoanNo + "_" + strTime + ".pdf");
Response.Flush();
Response.Close();
if (File.Exists(Server.MapPath("~/Doc/") + strLoanNo + strTime + ".doc"))
{
File.Delete(Server.MapPath("~/Doc/") + strLoanNo + strTime + ".doc");
}
if (File.Exists(Server.MapPath("~/Doc/") + strLoanNo + strTime + ".pdf"))
{
File.Delete(Server.MapPath("~/Doc/") + strLoanNo + strTime + ".pdf");
}
【问题讨论】:
标签: asp.net windows iis-6 com+ dcom