以下是我的代码。如果我将该块从object fName
注释掉到adoc.Close
,则代码可以在IIS上运行,但如果没有注释掉它会给我这个错误
NullReferenceException:对象引用未设置为对象的实例。] QuoteProject030117.Controllers.Quote1Controller.Create(Quote1 quote1)+1653
我认为iis修改单词doc有问题,但我无法找到修复它的方法。当我运行它时,所有代码都可以在Visual Studio上完美运行。如果有人能给我一只手,将不胜感激。谢谢!
public ActionResult Create([Bind(Include = "id,quotenumber,date,value,won,lost,client,projectdescription,contact")] Quote1 quote1)
{
if (ModelState.IsValid)
{
db.Quote1.Add(quote1);
db.SaveChanges();
string QNsql = @"SELECT quotenumber FROM Quote1 ORDER BY id DESC offset 0 rows fetch next 1 rows only;";
string quoteNum = db.Database.SqlQuery<string>(QNsql).Single();
string Dsql = @"SELECT date FROM Quote1 ORDER BY id DESC offset 0 rows fetch next 1 rows only;";
string date = db.Database.SqlQuery<string>(Dsql).Single();
string Clientsql = @"SELECT client FROM Quote1 ORDER BY id DESC offset 0 rows fetch next 1 rows only;";
string client = db.Database.SqlQuery<string>(Clientsql).Single();
string Contsql = @"SELECT contact FROM Quote1 ORDER BY id DESC offset 0 rows fetch next 1 rows only;";
string contact = db.Database.SqlQuery<string>(Contsql).Single();
string PDessql = @"SELECT projectdescription FROM Quote1 ORDER BY id DESC offset 0 rows fetch next 1 rows only;";
string proDes = db.Database.SqlQuery<string>(PDessql).Single();
System.IO.Directory.CreateDirectory(@"C:\Users\alanf\Documents\CopyTo\" + quoteNum);
System.IO.Directory.CreateDirectory(System.IO.Path.Combine(@"C:\Users\alanf\Documents\CopyTo\" + quoteNum, "Information Recieved From Customer"));
System.IO.Directory.CreateDirectory(System.IO.Path.Combine(@"C:\Users\alanf\Documents\CopyTo\" + quoteNum, "PO Recieved From Customer"));
System.IO.Directory.CreateDirectory(System.IO.Path.Combine(@"C:\Users\alanf\Documents\CopyTo\" + quoteNum, "Project Costing Sheet"));
System.IO.Directory.CreateDirectory(System.IO.Path.Combine(@"C:\Users\alanf\Documents\CopyTo\" + quoteNum, "Quotation Sent To Customer"));
System.IO.Directory.CreateDirectory(System.IO.Path.Combine(@"C:\Users\alanf\Documents\CopyTo\" + quoteNum, "Quotations Recieved From Vendors"));
string fileName = "Quote.docx";
string sourcePath = @"C:\inetpub\wwwroot";
string targetPath = @"C:\Users\alanf\Documents\CopyTo\" + quoteNum + @"\Quotation Sent To Customer";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(sourceFile, destFile, true);
object fName = System.IO.Path.Combine(targetPath, fileName);
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = false };
Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(fName, ReadOnly: false, Visible: false);
aDoc.Activate();
FindAndReplace(wordApp, "<quote>", "" + quoteNum + "");
FindAndReplace(wordApp, "<date>", "" + date + "");
FindAndReplace(wordApp, "<client>", "" + client + "");
FindAndReplace(wordApp, "<contact>", "" + contact + "");
FindAndReplace(wordApp, "<projectdescription>", "" + proDes + "");
aDoc.Close();
Process.Start("explorer.exe", @"C:\Users\alanf\Documents\CopyTo\" + quoteNum);
return RedirectToAction("Index");
}
答案 0 :(得分:1)
Microsoft不支持IIS的Office互操作 - 请参阅https://social.msdn.microsoft.com/Forums/en-US/1d3923a5-6720-4743-8fa0-c919ff90e4ef/i-cant-open-word-office-on-iis7?forum=worddev和https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office
如同@OmegaMan所问,知道1653是什么行会很好。
对于您的场景,我建议您查看支持读取和编写Word文档或进行邮件合并的.NET库。我之前使用过Aspose,但也有其他人使用GemBox(虽然我还没有使用过它们)。
另请参阅此帖子Reading doc and docx files using C# without having MS Office installed on server