Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
| |
| // ouvrir un package WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(@"C:/Documents and Settings/romagny/Mes documents/test.docx",true);// document word SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(@"C:/Documents and Settings/romagny/Mes documents/test.xlsx", true);// classeur excel PresentationDocument presentationDocument = PresentationDocument.Open(@"C:/Documents and Settings/romagny/Mes documents/test.pptx", true);// presentation powerpoint |
| // lister les parts (attention : ne liste pas les fichiers de relations .rels) IEnumerator<IdPartPair> enumerator = wordprocessingDocument.Parts.GetEnumerator(); while (enumerator.MoveNext()) { TreeNode t = new TreeNode(enumerator.Current.OpenXmlPart.Uri.ToString()); treeView1.Nodes.Add(t); IEnumerator<IdPartPair> enumerator2 = enumerator.Current.OpenXmlPart.Parts.GetEnumerator(); while (enumerator2.MoveNext()) { t.Nodes.Add(enumerator2.Current.OpenXmlPart.Uri.ToString()); } } |
| // obtenir toutes les parts du package PackagePartCollection parts = wordprocessingDocument.Package.GetParts(); // lister toutes les parts du package(avec windowbase liste egalement les fichiers de relations .rels) IEnumerator<PackagePart> enumerator = wordprocessingDocument.Package.GetParts().GetEnumerator();// package.Parts.GetEnumerator(); while (enumerator.MoveNext()) { treeView1.Nodes.Add(enumerator.Current.Uri.ToString()); } |
| MainDocumentPart mainDocumentPart = wordprocessingDocument.MainDocumentPart;// obtient /word/document.xml // accéder à une part OpenXmlPart part = wordprocessingDocument.GetPartById("rId1"); //par id |
| PackagePart part = wordprocessingDocument.Package.GetPart(new Uri("/word/document.xml", UriKind.Relative)); |
| // récupérer les properiétés du package (Creator,Keywords,LastModified,etc.) PackageProperties properties = wordprocessingDocument.PackageProperties; |
| private void button1_Click(object sender, EventArgs e) { // validation du package OpenXmlPackageValidationSettings validationSettings = new OpenXmlPackageValidationSettings(); validationSettings.EventHandler += new EventHandler<OpenXmlPackageValidationEventArgs>(validationSettings_EventHandler); wordprocessingDocument.Validate(validationSettings); // validation d'une part mainDocumentPart.ValidateXml(@"C:/Program Files/romagny13/Cs3OpenXmlManager v 1.0/schemas/wml.xsd", new System.Xml.Schema.ValidationEventHandler(validationPart_EventHandler)); } void validationSettings_EventHandler(object sender, OpenXmlPackageValidationEventArgs e) { MessageBox.Show(e.Message); } void validationPart_EventHandler(object sender, System.Xml.Schema.ValidationEventArgs e) { MessageBox.Show(e.Message); } |
| using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Create(@"C:/new.docx", WordprocessingDocumentType.Document)) { // à ce niveau aucune part n'est présente dans le package (seul [Content_Types].xml est présent) wordprocessingDocument.AddCoreFilePropertiesPart(); // création /docProps/Core.xml wordprocessingDocument.AddMainDocumentPart(); // création de /word/document.xml using (Stream stream = wordprocessingDocument.MainDocumentPart.GetStream()) { byte[] buffer = (new UTF8Encoding()).GetBytes(@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> <w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""> <w:body><w:p><w:r><w:t>Hello world!</w:t></w:r></w:p></w:body> </w:document>"); stream.Write(buffer, 0, buffer.Length); } // ajout d'une part(ici par exemple une ImagePart) ,il faut passer par MainDocumentPart ici ImagePart imagePart = wordprocessingDocument.MainDocumentPart.AddImagePart(ImagePartType.Jpeg); // ou content type using (FileStream stream = new FileStream(@"C:/Documents and Settings/romagny/Mes documents/Mes images/monimage.jpg", FileMode.Open)) { imagePart.FeedData(stream); } } |