C#open xml uripart exceel文件 [英] C# open xml uripart exceel file
问题描述
我正在尝试打开.xlsx文件进行保存和更新。我根据模板制作了xml。但是当我有多张工作表时。如何连接工作表数量。所有床单不是必需的
public void copyCustomXml(string path)
{
试试
{
ZipFile zip = new ZipFile(path);
Stream documentXml = readDocumentxml(zip);
XmlDocument xmlDoc_document = new XmlDocument();
xmlDoc_document.Load(documentXml);
zip .Close();
string customXmlpath;
XmlDocument xmlDoc = new XmlDocument();
//将document.xml,beefAuditSummary.xml保存为自定义xml
使用(Package package = Package.Open(path,FileMode.Open,FileAccess.ReadWrite))
{
System.Uri uriPartTarget = new System.Uri(/ docProps / mcdonald.xml,System.UriKind.Relative);
customXmlpath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly()。Location);
customXmlpath = customXmlpath + @\ mcdonald.xml;
xmlDoc.Load(customXmlpath);
PackagePart customXml = package.CreatePart(uriPartTarget,application / vnd.openxmlformats-officedocument.spreadsheetml。 styles + xml);
//在.xslx文件中复制xml
使用(Stream partStream = customXml.GetStream(FileMode.Create) ,FileAccess.ReadWrite))
{
xmlDoc.Save(partStream);
}
System.Uri uriPartTargetdocxml = new System.Uri(/ worksheets / sheet2.xml $ /worksheets/sheet3.xml $ /worksheets/sheet4.xml& amp;& ; /worksheets/sheet5.xml $ /worksheets/sheet6.xml $ /worksheets/sheet7.xml $ /worksheetssheet8.xml,System.UriKind.Relative);
if(!package.PartExists(uriPartTargetdocxml) ))
{
PackagePart customXmldoc = package.CreatePart(uriPartTargetdocxml,application / vnd.openxmlformats-officedocument.spreadsheetml.styles + xml);
//在.xslx文件中复制document.xml
使用(Stream partStream = customXmldoc.GetStream(FileMode.Create,FileAccess.ReadWrite))
{
xmlDoc_document.Save(partStream);
}
}
else
{
package.DeletePart(uriPartTargetdocxml);
PackagePart自定义Xmldoc = package.CreatePart(uriPartTargetdocxml,application / vnd.openxmlformats-officedocument.spreadsheetml.styles + xml);
//复制document.xml里面的.xslx文件
使用(Stream partStream = customXmldoc.GetStream(FileMode.Create,FileAccess.ReadWrite))
{
xmlDoc_document.Save(partStream) ;
}
}
}
//返回frtDetails;
}
catch(exception ex)
{
throw ex;
}
但它会在uripart部分给出错误,说规则不是按照opoenxml的规定line
System.Uri uriPartTargetdocxml = new System.Uri(/ worksheets / sheet2.xml $ / work sheets / sheet3.xml $ /worksheets/sheet4.xml&& /worksheets/sheet5.xml $ /worksheets/sheet6.xml $ /worksheets/sheet7.xml $ /worksheetssheet8.xml,System.UriKind.Relative);
if(!package.PartExists(uriPartTargetdocxml) ))
我必须让这些表格连接以获得任何人请帮助的实际路径[
/worksheets/sheet3.xml
/worksheets/sheet4.xml&& /worksheets/sheet5.xml
/worksheets/sheet6.xml
I am trying to open .xlsx file for save and update.I have made xml as according to template.But when I have number of sheets .How to concatenate the number of sheets.All sheets are not necessary
public void copyCustomXml(string path)
{
try
{
ZipFile zip = new ZipFile(path);
Stream documentXml = readDocumentxml(zip);
XmlDocument xmlDoc_document = new XmlDocument();
xmlDoc_document.Load(documentXml);
zip.Close();
string customXmlpath;
XmlDocument xmlDoc = new XmlDocument();
//save document.xml,beefAuditSummary.xml as custom xml
using (Package package = Package.Open(path, FileMode.Open, FileAccess.ReadWrite))
{
System.Uri uriPartTarget = new System.Uri("/docProps/mcdonald.xml", System.UriKind.Relative);
customXmlpath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
customXmlpath = customXmlpath + @"\mcdonald.xml";
xmlDoc.Load(customXmlpath);
PackagePart customXml = package.CreatePart(uriPartTarget, "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml");
//copy xml inside .xslx file
using (Stream partStream = customXml.GetStream(FileMode.Create, FileAccess.ReadWrite))
{
xmlDoc.Save(partStream);
}
System.Uri uriPartTargetdocxml = new System.Uri("/worksheets/sheet2.xml $ /worksheets/sheet3.xml $ /worksheets/sheet4.xml && /worksheets/sheet5.xml $ /worksheets/sheet6.xml $ /worksheets/sheet7.xml $ /worksheetssheet8.xml", System.UriKind.Relative);
if (!package.PartExists(uriPartTargetdocxml))
{
PackagePart customXmldoc = package.CreatePart(uriPartTargetdocxml, "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml");
//copy document.xml inside .xslx file
using (Stream partStream = customXmldoc.GetStream(FileMode.Create, FileAccess.ReadWrite))
{
xmlDoc_document.Save(partStream);
}
}
else
{
package.DeletePart(uriPartTargetdocxml);
PackagePart customXmldoc = package.CreatePart(uriPartTargetdocxml, "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml");
//copy document.xml inside .xslx file
using (Stream partStream = customXmldoc.GetStream(FileMode.Create, FileAccess.ReadWrite))
{
xmlDoc_document.Save(partStream);
}
}
}
//return frtDetails;
}
catch (Exception ex)
{
throw ex;
}
But it will give error at uripart part saying the rule are not as per opoenxml at the line
System.Uri uriPartTargetdocxml = new System.Uri("/worksheets/sheet2.xml $ /worksheets/sheet3.xml $ /worksheets/sheet4.xml && /worksheets/sheet5.xml $ /worksheets/sheet6.xml $ /worksheets/sheet7.xml $ /worksheetssheet8.xml", System.UriKind.Relative);
if (!package.PartExists(uriPartTargetdocxml))
i have to make these sheets concate to get the actual path anyone please help[
/worksheets/sheet3.xml
/worksheets/sheet4.xml && /worksheets/sheet5.xml
/worksheets/sheet6.xml
这篇关于C#open xml uripart exceel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!