Aller au contenu

Messages recommandés

Posté(e)

Salut à tous,

je bute sérieusement sur la création de fichiers excel XML

Ce format est peu connu, mais il a pour mois certains avantage, tous mes fichiers paramètres sont de ce format.

 

Je génère du xml avec lisp depuis déjà pas mal de temps (format KML, Gpx etc)

mais la j'ai un problème d'espace de nom que je n'arrive pas à comprendre.

 

ma routine TryXml génère un petit fichier excel, dont j'ai joint le modèle:

un tableau avec 2 nombre et deux lettres.

celui généré ne marche pas à cause d'un attribut xmlns vide , généré par défaut lors de la sauvegarde du fichier.

 

il empêche l'ouverture par excel.

le supprimer manuellement suffit à faire marcher le xml, mais le supprimer par XML DOM ne sert à rien, car il se recrée lors de la sauvegarde

 

ce sujet décrit mon problème, mais la réponse apportée ne marche pas chez moi.

 

En fait, je n'arrive pas à comprendre le fonctionnement des espaces de nom dans xml, et je n'ai pas trouvé de doc intéressante à ce sujet : on dit "voilà c'est comme ça" mais je n'arrive pas à comprendre mon erreur par la documentation.

 

Toute aide sur le sujet est bienvenue ...

 

merci d'avance,

Gégé

 

 

(defun c:TryXml (/ nchem fich elast sel lsel curproj)<BR>  <BR>  (setq dwgname (cadr (pw_scie_fich (getvar "dwgname"))))<BR>  (setq dwgprefix (car (pw_scie_fich (getvar "dwgname"))))<BR>  (setq pw-xmldoc (vlax-create-object "MSXML2.DOMDocument"))<BR>  (vlax-put-property pw-xmldoc 'async :vlax-False)<BR>  ;;dom1.async = :vlax-False<BR>  <BR>   ;; dom1.resolveExternals = False<BR>  (vlax-put-property pw-xmldoc 'resolveExternals :vlax-False)<BR>  ;;ValidateOnParse <BR>  (vlax-put-property pw-xmldoc 'ValidateOnParse :vlax-False)<BR>  ;;UseInlineSchema <BR>  ;;(vlax-put-property pw-xmldoc 'UseInlineSchema :vlax-False)<BR>  <BR>  (setq    fich (getfiled "Fichier MsXml à écrire : "<BR>           	(strcat dwgprefix dwgname)<BR>           	"xml"<BR>           	1<BR>     	)<BR>  )<BR>  (xml_header pw-xmldoc)<BR>  (setq workbook (xml_workbook pw-xmldoc))<BR>  (setq Worksheet (xml_Worksheet WorkBook "onglet" '((1 2) ("A" "B"))))<BR>  <BR><BR>  (vlax-invoke-method pw-xmldoc 'save fich)<BR>  <BR>  <BR>  (vlax-release-object pw-xmldoc)<BR>  (setq pw-xmldoc nil)<BR>  (princ)<BR>)<BR><BR><BR><BR><BR><BR>;;**************************************************************************<BR>;;§/xml/Crée le header d'un fichier excel xml  /xmldoc<BR><BR>(defun xml_header (xmldoc / tmp)<BR>  (setq    tmp (vlax-invoke-method<BR>          xmldoc<BR>          'createProcessingInstruction<BR>          "xml"<BR>          "version='1.0' encoding='utf-8'"<BR>        )<BR>  )<BR>  (vlax-invoke-method xmldoc 'appendChild tmp)<BR>  (setq    tmp (vlax-invoke-method<BR>          xmldoc<BR>          'createProcessingInstruction<BR>          "mso-application"<BR>          "progid=\"Excel.Sheet\""<BR>        )<BR>  )<BR>  (vlax-invoke-method xmldoc 'appendChild tmp)<BR><BR>)<BR><BR><BR>;;**************************************************************************<BR>;;§/xml/Crée le workbook d'un fichier excel xml  / xmldoc<BR>;;retourne le worbook<BR><BR>(defun xml_workbook (xmldoc / workbook )<BR>  <BR>   (setq workbook (vlax-invoke-method xmldoc 'createElement "Workbook"))<BR>  (setq workbook (vlax-invoke-method xmldoc 'appendChild workbook))<BR>    ;;créé l'entête<BR>  (workbook_header  WorkBook)<BR><BR>   <BR>  WorkBook<BR>  <BR>)<BR><BR>;;**************************************************************************<BR>;;§/xml/Crée le header d'un fichier excel xml  /xmldoc<BR><BR>(defun workbook_header ( WorkBook /  )<BR>;;;  xmlns=http://schemas.microsoft.com/office/excel/2003/xml<BR>  (pw_xml_createTextAttribute WorkBook  "xmlns" "http://schemas.microsoft.com/office/excel/2003/xml")<BR>;;;xmlns:ss=urn:schemas-microsoft-com:office:spreadsheet<BR>  (pw_xml_createTextAttribute WorkBook "xmlns:ss" "urn:schemas-microsoft-com:office:spreadsheet")<BR>;;;xmlns:udc=http://schemas.microsoft.com/data/udc<BR>  (pw_xml_createTextAttribute WorkBook  "xmlns:udc" "http://schemas.microsoft.com/data/udc")<BR>;;;xmlns:x=urn:schemas-microsoft-com:office:excel<BR>  (pw_xml_createTextAttribute WorkBook   "xmlns:x" "urn:schemas-microsoft-com:office:excel")<BR>;;;xmlns:xsd=http://www.w3.org/2001/XMLSchema<BR>  (pw_xml_createTextAttribute WorkBook   "xmlns:xsd" "http://www.w3.org/2001/XMLSchema")<BR>  ;;(pw_xml_createTextAttribute WorkBook "exclude-result-prefixes" "ss o x")<BR>  <BR>  <BR><BR>)  <BR><BR><BR><BR><BR>;;**************************************************************************<BR>;;§/xml/Crée une feuille d'un fichier excel xml  / WorkBook name ldata<BR>;; retourne le Worksheet<BR><BR>(defun xml_Worksheet (WorkBook name ldata / tmp cdata rdata table Worksheet)<BR>   (setq Worksheet (vlax-invoke-method pw-xmldoc 'createElement "Worksheet"))<BR>  (setq Worksheet (vlax-invoke-method workbook 'appendChild Worksheet))<BR>  ;;créé l'entête<BR>  ;;(Worksheet_header  Worksheet)<BR>  <BR>  ;(pw_xml_createTextAttribute Worksheet "xmlns" "urn:schemas-microsoft-com:office:spreadsheet")<BR>  ;;(pw_xml_createTextAttribute Worksheet "xmlns:ss" "urn:schemas-microsoft-com:office:spreadsheet")<BR>    (pw_xml_createTextAttribute Worksheet "ss:Name" name)<BR>  ;;table<BR>   (setq table (xml_table Worksheet ldata))<BR>  ;;remplissage de la table<BR>  (foreach rdata ldata<BR>  (setq row (xml_Row table))<BR>    (foreach cdata rdata<BR>      ( xml_cell row cdata)<BR>    )<BR>  )<BR>  ;;<BR><BR>  (setq pw-worksheet Worksheet)<BR><BR>)<BR>;;**************************************************************************<BR>;;§/xml/Crée une table dans une feuille excel xml  / Worksheet<BR>;;retourne la table<BR><BR>(defun xml_table (Worksheet ldata  / tmp nbraw nbcol yx table)<BR>   (setq table (vlax-invoke-method pw-xmldoc 'createElement "Table"))<BR>  (vlax-invoke-method Worksheet 'appendChild table)<BR>   ;;taille de la table :<BR>  (setq yx (pw_taille_liste ldata))<BR>  (setq nbraw (car yx) nbcol (cadr yx))<BR>  ;;(pw_xml_createTextAttribute Worksheet "xmlns" "urn:schemas-microsoft-com:office:spreadsheet")<BR>  ;;ss:ExpandedColumnCount="2"<BR>  (pw_xml_createTextAttribute table "ss:ExpandedColumnCount" (itoa nbcol))<BR>  ;;ss:ExpandedRowCount="2"<BR>  (pw_xml_createTextAttribute table "ss:ExpandedRowCount" (itoa nbraw))<BR>  table<BR>)<BR><BR><BR>;;**************************************************************************<BR>;;§/xml/Crée une row dans une feuille excel xml  / Worksheet<BR>;;retourne la row<BR><BR>(defun xml_Row (Worksheet  / tmp row)<BR>   (setq row (vlax-invoke-method pw-xmldoc 'createElement "Row"))<BR>  (vlax-invoke-method Worksheet 'appendChild row)<BR>  (pw_xml_createTextAttribute row "ss:Height" "12.5")<BR>  row<BR>)<BR><BR>;;**************************************************************************<BR>;;§/xml/Crée une cell dans une feuille excel xml  / Worksheet<BR>;;retourne la cell<BR><BR>(defun xml_cell (row dataValue  / tmp cell data)<BR>   (setq cell (vlax-invoke-method pw-xmldoc 'createElement "Cell"))<BR>  (vlax-invoke-method row 'appendChild cell)<BR>  (setq data (vlax-invoke-method pw-xmldoc 'createElement "Data"))<BR>  (vlax-invoke-method cell 'appendChild data)<BR>  <BR>  (if (numberp dataValue)<BR>    <BR>    (pw_xml_createTextAttribute data "ss:Type" "Number")<BR>    (pw_xml_createTextAttribute data "ss:Type" "String")<BR>   )<BR>  (setq dataValue (pw_guil_make dataValue))<BR>  (vlax-put-property data 'text dataValue)<BR>  cell<BR>)<BR><BR><BR><BR>;;**************************************************************************<BR>;;§/xml/Crée un attribut de type text / parent nom textval<BR>;;Un attribut peut être Text ou EntityReference : ne traite que le cas text<BR><BR>(defun pw_xml_createTextAttribute (parent nom textval / att )<BR>  (setq att (vlax-invoke-method pw-xmldoc 'createAttribute nom))<BR>  (vlax-put-property att 'text textval)<BR>  (vlax-invoke-method parent 'setAttributeNode att)<BR>)<BR>

 

Et le fichier modèle xml :

<?xml version="1.0" encoding="utf-8"?><BR><?mso-application progid="Excel.Sheet"?><BR><Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"><BR>  <Worksheet ss:Name="Feuil1" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"><BR>    <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2"><BR>      <Row><BR>        <Cell><BR>          <Data ss:Type="Number">1</Data><BR>        </Cell><BR>        <Cell><BR>          <Data ss:Type="Number">2</Data><BR>        </Cell><BR>      </Row><BR>      <Row><BR>        <Cell><BR>          <Data ss:Type="String">A</Data><BR>        </Cell><BR>        <Cell><BR>          <Data ss:Type="String">B</Data><BR>        </Cell><BR>      </Row><BR>    </Table><BR>  </Worksheet><BR></Workbook><BR>

----------------------------------------------------------------------

Site: https://www.g-eaux.fr

Blog: http://g-eaux.over-blog.com

Posté(e)

Bon, après une sérieuse prise de tête, j'ai trouvé:

Tous les xml que j'avais créé jusqu'a maintenant, c'était du 1.1

La methode createlement marchait très bien, sans le besoin de spécifier un espace de nom.

 

Les fichier excelXml, c'est du 1.0, il faut utiliser 'createNode, en précisant le type, et l'espace de Nom.

 

Je pensais que c'était l'attribut xmlns qui déterminait l'espace de nom, c'est vrai une fois le xml créé et sauvegardé , mais en phase de construction, les attributs ne comptent pas encore.

 

donc la bonne methode est:

   
(setq ExcelWorkbook (vlax-invoke-method pw-xmldoc 'createNode 1 "ExcelWorkbook" "urn:schemas-microsoft-com:office:excel"))
 (vlax-invoke-method WorkBook 'appendChild ExcelWorkbook)

avec 1 = nodetype : NODE_ELEMENT (1) et "urn:schemas-microsoft-com:office:excel" l'espace de nom

 

 

Merci à Yahoo, qui m'a amené sur cette page : Méthode DOMDocument.CreateNode() ajoute une déclaration d'espace de noms vide

Comme quoi Google n'est pas toujours le meilleur ...

----------------------------------------------------------------------

Site: https://www.g-eaux.fr

Blog: http://g-eaux.over-blog.com

Créer un compte ou se connecter pour commenter

Vous devez être membre afin de pouvoir déposer un commentaire

Créer un compte

Créez un compte sur notre communauté. C’est facile !

Créer un nouveau compte

Se connecter

Vous avez déjà un compte ? Connectez-vous ici.

Connectez-vous maintenant
×
×
  • Créer...

Information importante

Nous avons placé des cookies sur votre appareil pour aider à améliorer ce site. Vous pouvez choisir d’ajuster vos paramètres de cookie, sinon nous supposerons que vous êtes d’accord pour continuer. Politique de confidentialité