asp.net - 无法将字节转换为字符串?
问题描述
我正在使用执行邮件合并,但Syncfusion.DocIO
我的文档模板在 SQL Server wordDocument
DB中流的方法以及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();
}
解决方案
根据给定的详细信息,我们怀疑您尝试将 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);
}
如果您认为我们误解了您的任何要求,请与我们分享您的最终要求的以下详细信息,这将有助于我们提供准确的解决方案。
- 您是否已将 byte 或 byte[] 数组作为参数传递给 WordDocument。
- 无论您是在编译时还是运行时遇到此错误。基于以上细节,我们将进一步分析,尽早为您提供合适的解决方案。
推荐阅读
- java - 来自java的休息服务调用密钥长度不是128/192/256位
- c# - Entity Framework Core 3.0 中的拥有类型问题
- django - 如何在 Django 的本地主机上运行 Facebook 身份验证?
- javascript - 如何限制每个函数的数量?
- date - 将日期和时间合并为日期时间
- javascript - Ajax 书签:加载正确的页面
- c - 消除前导零,同时仍保持正确对齐
- javascript - 错误:需要使用事件参数调用云函数。Firebase 控制台错误,
- node.js - 与 Node JS 应用程序链接的 Dialogflow 帐户
- android - 如何在android studio中的项目之间共享模块