C# system.invalidoperationexception序列不包含openxml元素
我使用OpenXML在内容控件上从SQL Server填充数据时遇到一些异常错误。我想做一个简单的漫游,让数据从数据库中的表中填充并打印到MS Word文档上,我正在使用OpenXMLSDK 编译程序并运行以查看发生了什么,我得到了以下异常错误: system.invalidoperationexception序列不包含任何元素 我的代码看起来有点像这样C# system.invalidoperationexception序列不包含openxml元素,c#,sql-server,openxml,C#,Sql Server,Openxml,我使用OpenXML在内容控件上从SQL Server填充数据时遇到一些异常错误。我想做一个简单的漫游,让数据从数据库中的表中填充并打印到MS Word文档上,我正在使用OpenXMLSDK 编译程序并运行以查看发生了什么,我得到了以下异常错误: system.invalidoperationexception序列不包含任何元素 我的代码看起来有点像这样 using System; using System.Collections.Generic; using System.ComponentM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Drawing;
using System.Data.SqlClient;
using System.IO;
namespace DesktopSignaturetest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string constring = @"Data Source=DESKTOP-9CM4N5S\SQLEXPRESS;Initial Catalog=SignatureBox2;User ID=sa;Password=123456;";
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
string q = "select * from SignatureBox_DB where StaffID = @StaffID";
using (SqlCommand cmd = new SqlCommand(q, con))
{
cmd.Parameters.AddWithValue("@StaffID", textBox1.Text);
using (SqlDataReader rd = cmd.ExecuteReader())
{
try
{
if (rd.Read())
{
string fileName = @"C:\Users\emi\Desktop\test.jpg";
byte[] imageBytes = Convert.FromBase64String(rd["SignatureBase64"].ToString());
string fullname = rd["FullName"].ToString();
string designation = rd["Designation"].ToString();
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true, true);
image.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
using (WordprocessingDocument doc = WordprocessingDocument.Open(@"C:\Users\emi\Desktop\MSWordTest.docx", true))
{
MainDocumentPart mainPart = doc.MainDocumentPart;
SdtBlock block = mainPart.Document.Body.Descendants<SdtBlock>().Where
(r => r.SdtProperties.GetFirstChild<Tag>().Val == "Name").Single();
SdtBlock desg = mainPart.Document.Body.Descendants<SdtBlock>().Where
(r => r.SdtProperties.GetFirstChild<Tag>().Val == "Designation").Single();
SdtBlock cc = doc.MainDocumentPart.Document.Body.Descendants<SdtBlock>()
.FirstOrDefault(c =>
{
SdtProperties p = c.Elements<SdtProperties>().FirstOrDefault();
if (p != null)
{
// Is it a picture content control?
SdtContentPicture pict =
p.Elements<SdtContentPicture>().FirstOrDefault();
// Get the alias.
SdtAlias a = p.Elements<SdtAlias>().FirstOrDefault();
if (pict != null && a.Val == "Signature")
return true;
}
return false;
});
string embed = null;
if (cc != null)
{
Drawing dr = cc.Descendants<Drawing>().FirstOrDefault();
if (dr != null)
{
Blip blip = dr.Descendants<Blip>().FirstOrDefault();
if (blip != null)
embed = blip.Embed;
}
}
if (embed != null)
{
IdPartPair idpp = doc.MainDocumentPart.Parts
.Where(pa => pa.RelationshipId == embed).FirstOrDefault();
if (idpp != null)
{
DocumentFormat.OpenXml.Drawing.Text name = block.Descendants<DocumentFormat.OpenXml.Drawing.Text>().Single();
DocumentFormat.OpenXml.Drawing.Text desgx = desg.Descendants<DocumentFormat.OpenXml.Drawing.Text>().Single();
name.Text = fullname;
desgx.Text = designation;
ImagePart ip = (ImagePart)idpp.OpenXmlPart;
using (FileStream fileStream =
File.Open(fileName, FileMode.Open))
ip.FeedData(fileStream);
MessageBox.Show("Done!");
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
con.Close();
}
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用DocumentFormat.OpenXml.Packaging;
使用DocumentFormat.OpenXml.Wordprocessing;
使用DocumentFormat.OpenXml.Drawing;
使用System.Data.SqlClient;
使用System.IO;
命名空间DesktopSignaturetest
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
字符串结构=@“数据源=DESKTOP-9CM4N5S\SQLEXPRESS;初始目录=SignatureBox2;用户ID=sa;密码=123456;”;
使用(SqlConnection con=新的SqlConnection(consting))
{
con.Open();
string q=“从SignatureBox\u DB中选择*,其中StaffID=@StaffID”;
使用(SqlCommand cmd=newsqlcommand(q,con))
{
cmd.Parameters.AddWithValue(“@StaffID”,textBox1.Text);
使用(SqlDataReader rd=cmd.ExecuteReader())
{
尝试
{
if(rd.Read())
{
字符串文件名=@“C:\Users\emi\Desktop\test.jpg”;
byte[]imageBytes=Convert.FromBase64String(rd[“SignatureBase64”].ToString());
字符串fullname=rd[“fullname”].ToString();
字符串名称=rd[“名称”]。ToString();
MemoryStream ms=新的MemoryStream(imageBytes,0,imageBytes.Length);
ms.Write(imageBytes,0,imageBytes.Length);
Image=Image.FromStream(ms,true,true);
image.Save(文件名,System.Drawing.Imaging.ImageFormat.Jpeg);
使用(WordprocessingDocument doc=WordprocessingDocument.Open(@“C:\Users\emi\Desktop\MSWordTest.docx”,true))
{
MainDocumentPart mainPart=doc.MainDocumentPart;
SdtBlock block=mainPart.Document.Body.subjections()。其中
(r=>r.SdtProperties.GetFirstChild().Val==“Name”).Single();
SdtBlock desg=mainPart.Document.Body.subjections()。其中
(r=>r.SdtProperties.GetFirstChild().Val==“指定”).Single();
SdtBlock cc=doc.MainDocumentPart.Document.Body.subjects()
.FirstOrDefault(c=>
{
SdtProperties p=c.Elements().FirstOrDefault();
如果(p!=null)
{
//它是一个图片内容控件吗?
SDT内容图片=
p、 元素().FirstOrDefault();
//获取别名。
SdtAlias a=p.Elements().FirstOrDefault();
if(pict!=null&&a.Val==“签名”)
返回true;
}
返回false;
});
字符串嵌入=null;
如果(cc!=null)
{
Drawing dr=cc.subjects().FirstOrDefault();
如果(dr!=null)
{
Blip-Blip=dr.substands().FirstOrDefault();
如果(blip!=null)
嵌入=blip.embed;
}
}
如果(嵌入!=null)
{
IdPartPair idpp=doc.main documentpart.Parts
.Where(pa=>pa.RelationshipId==embed).FirstOrDefault();
如果(idpp!=null)
{
DocumentFormat.OpenXml.Drawing.Text name=block.subjects().Single();
DocumentFormat.OpenXml.Drawing.Text desgx=desg.subjects().Single();
name.Text=全名;
设计文本=名称;
ImagePart ip=(ImagePart)idpp.OpenXmlPart;
使用(文件流文件流)=
File.Open(文件名,FileMode.Open))
ip.FeedData(文件流);