當我在本地機器上使用調試模式運行時,沒關係,但在服務器上出現了這樣的錯誤。用於報告的Microsoft Word模板使用ASP.NET C#
Retrieving the COM class factory for component with CLSID {00020906-0000-0000-C000-000000000046} failed due to the following error: 80080005.
我的服務器是Windows 2008 64位,Office 2007和我的代碼是這樣的
private void GenerateWords(string sPO, string sSup)
{
Object oMissing = System.Reflection.Missing.Value;
Object oTrue = true;
Object oFalse = false;
Object savechanges = true;
Word.ApplicationClass oWord = new Word.ApplicationClass();
Word.Document oWordDoc = new Word.Document();
oWord.Visible = true;
Object oTemplatePath = Server.MapPath("Reports/Word/PurchaseOrder.docx");
oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
oWordDoc.Activate();
foreach (Word.Field myMergeField in oWordDoc.Fields)
{
iTotalFields++;
Word.Range rngFieldCode = myMergeField.Code;
String fieldText = rngFieldCode.Text;
// Start filling information in Word file
if (fieldText.StartsWith(" MERGEFIELD"))
{
Int32 endMerge = fieldText.IndexOf("\\");
Int32 fieldNameLength = fieldText.Length - endMerge;
String fieldName = fieldText.Substring(11, endMerge - 11);
fieldName = fieldName.Trim();
if (fieldName == "PONo")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(sPO);
}
if (fieldName == "SupNo")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(sSup);
}
if (fieldName == "VendorID")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["VendorID"].ToString().Trim());
}
if (fieldName == "VName")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Name"].ToString().Trim());
}
if (fieldName == "Contact")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Contact"].ToString().Trim());
}
if (fieldName == "Designation")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Designation"].ToString().Trim());
}
if (fieldName == "Tel")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Tel"].ToString().Trim());
}
if (fieldName == "Fax")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Fax"].ToString().Trim());
}
if (fieldName == "PODate")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["PODate"].ToString().Trim());
}
if (fieldName == "ClientName")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["ClientName"].ToString().Trim());
}
if (fieldName == "JobDescription")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["JobDescription"].ToString().Trim());
}
if (fieldName == "JobNo")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["JobNo"].ToString().Trim());
}
if (fieldName == "CostCode")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["CostCode"].ToString().Trim());
}
if (fieldName == "SchDlvy")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["SchDlvy"].ToString().Trim());
}
if (fieldName == "DlvyPoint")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["DlvyPoint"].ToString().Trim());
}
if (fieldName == "Amount")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Amount"].ToString().Trim());
}
if (fieldName == "tbl")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeParagraph();
Word.Table tbl = oWordDoc.Tables.Add(rngFieldCode, 1, 5, ref oMissing, ref oMissing);
//oWordDoc.Tables.Add(rngFieldCode, dtItems(sPO, sSup).Rows.Count, 5, ref oMissing, ref oMissing);
//SET HEADER
SetHeadings(tbl.Cell(1, 1), "Item No.");
SetHeadings(tbl.Cell(1, 2), "Description");
SetHeadings(tbl.Cell(1, 3), "Unit");
SetHeadings(tbl.Cell(1, 4), "Unit Price");
SetHeadings(tbl.Cell(1, 5), "Amount");
//END SET HEADER
//Add Row
for (int i = 0; i < dtItems(sPO, sSup).Rows.Count; i++)
{
Word.Row newRow = tbl.Rows.Add(ref oMissing);
newRow.Range.Font.Bold = 0;
newRow.Range.Underline = 0;
newRow.Range.ParagraphFormat.Alignment =
Word.WdParagraphAlignment.wdAlignParagraphCenter;
newRow.Cells[1].Range.Text = dtItems(sPO, sSup).Rows[i][3].ToString();
newRow.Cells[2].Range.Text = dtItems(sPO, sSup).Rows[i][4].ToString();
newRow.Cells[3].Range.Text = dtItems(sPO, sSup).Rows[i][8].ToString();
newRow.Cells[4].Range.Text = dtItems(sPO, sSup).Rows[i][10].ToString();
newRow.Cells[5].Range.Text = dtItems(sPO, sSup).Rows[i][11].ToString();
}
//END ROW
oWord.Selection.TypeParagraph();
}
if (fieldName == "TItems")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Unit"].ToString().Trim());
}
if (fieldName == "Discount")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Discount"].ToString().Trim());
}
if (fieldName == "TAmount")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Amount"].ToString().Trim());
}
if (fieldName == "Summary")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Amount"].ToString().Trim());
}
if (fieldName == "ReqNo")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["ReqNo"].ToString().Trim());
}
if (fieldName == "RevNo")
{
myMergeField.Select();
oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["RevNo"].ToString().Trim());
}
}
}
// End filling information in Word file
Object oSaveAsFile = (Object)Server.MapPath("Reports/Word/tmp2.docx");
oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
oWordDoc.Close(ref savechanges, ref oMissing, ref oMissing);
oWord.Application.Quit(ref savechanges, ref oMissing, ref oMissing);
//foreach (Process p in System.Diagnostics.Process.GetProcessesByName("winword"))
//{
// try
// {
// if (p.ProcessName == "WINWORD")
// {
// if (!p.HasExited)
// {
// p.Kill();
// p.WaitForExit(); // possibly with a timeout
// }
// }
// else
// {
// lblMessage.Text = "cannot kill. try again!";
// }
// }
// catch (Win32Exception winException)
// {
// //process was terminating or can't be terminated - deal with it
// Session["error"] = winException.Message;
// Response.Redirect("MessageBoard.aspx");
// }
// catch (InvalidOperationException invalidException)
// {
// //process has already exited - might be able to let this one go
// Session["error"] = invalidException.Message;
// Response.Redirect("MessageBoard.aspx");
// }
//}
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/msword";
Response.WriteFile(Server.MapPath("Reports/Word/tmp2.docx"), false);
Response.Flush();
Response.Close();
}
而且我也跟着從Blog.Crowe.co.nz 賦予權限,但仍然有問題,我解決不了這是自上個月以來的一次。如果可能的話,請幫助我。謝謝
如果我可能會問,你如何生成文檔? – TweeZz
so!單詞報告不適合web應用程序?所以有時我們需要使用單詞報告,我們應該怎麼做?以及爲什麼有些開發人員顯示他們的教程和示例代碼,如[Codeproject](http://www.codeproject.com/KB/aspnet/wordapplication.aspx)[C#Corner](http://www.c-sharpcorner.com/ UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx#ReadAndPostComment) – CMMaung
@CMMaung - 它可以工作,這就是代碼示例所顯示的內容,但是在部署和解決方案的整體可靠性方面會非常令人沮喪。讓它在一個開發箱上運行是一回事,而在生產環境中運行並穩定運行它是一回事。 – jfrankcarr