【问题标题】:Xceed Docx ReplaceText always throwing newValue cannot be null.Xceed Docx ReplaceText 总是抛出 newValue 不能为空。
【发布时间】:2018-09-18 21:02:49
【问题描述】:

我尝试了许多不同的选项,但无论我做什么,它要么不会做任何事情,要么总是返回 newValue 错误。

newValue cannot be null. 

看来我不是唯一一个,但自从下面的链接以来,它已经有了更新。

docX ReplaceText works incorrect

下面是我原来的例子:-

if (sur.RequestType)
        {
            templateDoc.ReplaceText("[#1]", "x");
            templateDoc.ReplaceText("[#2]", "");
        }
        else
        {
            templateDoc.ReplaceText("[#1]", "");
            templateDoc.ReplaceText("[#2]", "x");
        }

当调试它时,它会到达第 4 行,然后跳转到第 9 行,它会在下一步返回 newValue cannot be null 错误。

所以我尝试了:-

string temp1 = "temp1";

        if (sur.RequestType)
        {
            templateDoc.ReplaceText("[#1]", "x");
            templateDoc.ReplaceText("[#2]", temp1, false, RegexOptions.IgnoreCase, paraFormat, paraFormat, MatchFormattingOptions.SubsetMatch);
        }
        else
        {
            templateDoc.ReplaceText("[#1]", "x.x");
            templateDoc.ReplaceText("[#2]", "x", false, RegexOptions.IgnoreCase, paraFormat, paraFormat, MatchFormattingOptions.SubsetMatch);
        }

以及其他一些调整,但都返回相同的错误。

在使用 ReplaceText 之前,我使用了示例项目中的示例:-

templateDoc.AddCustomProperty( new CustomProperty( "CompanySlogan", "Always with you" ) );
  templateDoc.AddCustomProperty( new CustomProperty( "ClientName", "James Doh" ) );

在这里它会逐步遍历每一行,但生成的文档不会替换任何内容。

最后更多离题,但如果有人有更好的解决方案,我会被卡在来回尝试输出文件而不保存它,但在将其从 Xceed DocX 类型转换为 HttpResponseMessage 时遇到问题。

以下是我最不喜欢的实现,例如我想将其保存到数据库或跳过保存文件,直接将其提供给用户以保存他们想要的位置,而不是拥有服务器端副本。

[HttpGet]
    public HttpResponseMessage DownloadRecord(int id)
    {
        SURequest sur = _sURequestsService.GetRequestData(id);
        var fullPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/Content/RequestForm.docx");
        var fullPath2 = System.Web.Hosting.HostingEnvironment.MapPath(@"~/Content/RequestFormUpdated.docx");
        var templateDoc = DocX.Load(fullPath);
        var template = CreateRequestFromTemplate(templateDoc, sur);
        template.SaveAs(fullPath2);
        //using (FileStream fs2 = new FileStream(@"~/Content/RequestFormUpdated.docx", FileMode.Create))
        //{
        //    template.SaveAs(fs2);

        //}
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);

        var stream = new FileStream(fullPath2, FileMode.Open);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(fullPath2);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        result.Content.Headers.ContentLength = stream.Length;
        return result;
        //return fs2;
    }

我不知道如何进一步使用 Xceed,所以我将分支我当前的代码并尝试使用 OpenXML 看看我是否有更好的运气,或者其他人是否可以发现我做错了什么或如何解决 Xceed 中的问题?

任何帮助将不胜感激。

【问题讨论】:

    标签: asp.net-web-api download docx xceed novacode-docx


    【解决方案1】:

    原来是 VS17 的问题,它的行为与 replacetext 很奇怪,并且似乎在其编译器中缓存了一个较早的问题。

    这表现得好像问题出在某个地方,只能通过手动停止编译器进程来解决。

    仍然没有解决 AddCustomProperty 或跳过生成本地文件的问题。

    我将努力让它不生成本地文件,但可能需要打开一个特定于该问题的新问题或设置其他东西来清理旧文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-06
      • 2016-08-17
      • 1970-01-01
      • 2017-09-16
      • 2017-11-11
      • 1970-01-01
      • 2010-12-11
      相关资源
      最近更新 更多