1

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

newValue cannot be null. 

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

docX ReplaceText 工作不正确

下面是我原来的例子: -

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 中的问题?

任何帮助将非常感激。

4

1 回答 1

0

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

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

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

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

于 2018-04-10T12:59:57.613 回答