首页 > 解决方案 > 无法将字节转换为字符串?

问题描述

我正在使用执行邮件合并,但Syncfusion.DocIO我的文档模板在 SQL Server wordDocumentDB流的方法以及WordDocument. 我如何解决这个问题,请帮助我如何使用此代码执行邮件合并。谢谢

  public ActionResult CreateDocument(GenerateTemp generateDoc)
        {
            var FileById = (from doc in db.Documents
                            where doc.D_Id.Equals(generateDoc.D_Id)
                            select new {doc.D_File}).ToList().FirstOrDefault();

            using (WordDocument document = new WordDocument(FileById.D_File))
            {
                string[] fieldNames = { "ContactName", "CompanyName", "Address", "City", "Country", "Phone" };
                string[] fieldValues = { "Nancy Davolio", "Syncfusion", "507 - 20th Ave. E.Apt. 2A", "Seattle, WA", "USA", "(206) 555-9857-x5467" };
                //Performs the mail merge
                document.MailMerge.Execute(fieldNames, fieldValues);
                //Saves the Word document to disk in DOCX format
                document.Save("Result.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment);
            }
            return View();
        }

标签: asp.netasp.net-mvcsyncfusionmailmerge

解决方案


根据给定的详细信息,我们怀疑您尝试将 byte 或 byte[] 数组作为参数而不是字符串传递给 WordDocument API。因此,您最后遇到了编译错误(无法将字节转换为字符串)。为了达到您的最终要求,我们建议您必须将字节数组转换为流,然后将流作为参数传递给 WordDocument API。请参考下面突出显示的代码示例以在您的最后实现相同的目的,如果这对您有帮助,请告诉我们。

//Read all bytes from the file

byte[] byteArray = File.ReadAllBytes(@"../../Letter Formatting.docx");

//Converts the byte array into a stream

Stream stream = new MemoryStream(byteArray);

//Opens a template document from stream through constructor of WordDocument class

using (WordDocument document = new WordDocument(stream))

{

   string[] fieldNames = { "ContactName", "CompanyName", "Address", "City", "Country", "Phone" };

   string[] fieldValues = { "Nancy Davolio", "Syncfusion", "507 - 20th Ave. E.Apt. 2A", "Seattle, WA", "USA", "(206) 555-9857-x5467" };

   //Performs the mail merge

   document.MailMerge.Execute(fieldNames, fieldValues);

   //Saves the Word document to disk in DOCX format

   document.Save("Result.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment);

} 

如果您认为我们误解了您的任何要求,请与我们分享您的最终要求的以下详细信息,这将有助于我们提供准确的解决方案。

  1. 您是否已将 byte 或 byte[] 数组作为参数传递给 WordDocument。
  2. 无论您是在编译时还是运行时遇到此错误。基于以上细节,我们将进一步分析,尽早为您提供合适的解决方案。

推荐阅读