【问题标题】:cannot convert byte to string?无法将字节转换为字符串?
【发布时间】:2020-07-06 08:09:37
【问题描述】:

我正在使用Syncfusion.DocIO 执行邮件合并,但我的文档模板位于 SQL Server DB 中strong>无法将字节转换为字符串,尽管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();
        }

【问题讨论】:

  • 响应可以是任一页面文件,不能同时是两者。您可以保存到 MemoryStream 并将底层字节数组作为 ContentResult 返回吗?

标签: asp.net asp.net-mvc syncfusion mailmerge


【解决方案1】:

根据给定的详细信息,我们怀疑您尝试将 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. 您是否在编译时或运行时遇到此错误。 基于以上细节,我们将进一步分析,尽早为您提供合适的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 1970-01-01
    相关资源
    最近更新 更多