Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   xml file erstellen (https://www.delphipraxis.net/185583-xml-file-erstellen.html)

khh 22. Jun 2015 07:58

xml file erstellen
 
Hallo zusammen,
gibt es eine brauchbare(Lazarus) Bibliothek mit der man xml Files erstellen kann, die dann auch von Excel gelesen werden können?

Ich danke euch

himitsu 22. Jun 2015 08:52

AW: xml file erstellen
 
XML? Du meinst jetzt aber nicht XLS, oder?

Und Für XML gibt es garantiert im Lazarus was ... nennt sich bestimmt TXMLDocument in einer Unit mit XML im Namen.

Perlsau 22. Jun 2015 09:11

AW: xml file erstellen
 
Zitat:

Zitat von khh (Beitrag 1306057)
... gibt es eine brauchbare(Lazarus) Bibliothek mit der man xml Files erstellen kann, die dann auch von Excel gelesen werden können?

Du kannst statt XML CSV-Dateien erstellen, die kann Excel direkt einlesen und verarbeiten. Das so erzeugte Excel-Sheet kannst du dann via Excel z.B. im XLS-Format speichern.

khh 22. Jun 2015 10:53

AW: xml file erstellen
 
nee, ich meine schon xml, keine csv die dann erst durch excel konvertiert werden müssen

so nach diesem schema:

Delphi-Quellcode:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
    <Author>ich</Author>
    <LastAuthor>ich</LastAuthor>
    <Created>2012-02-01T12:31:29Z</Created>
    <Version>15.00</Version>
  </DocumentProperties>


...
....
....

CCRDude 22. Jun 2015 10:59

AW: xml file erstellen
 
TXMLDocument ist in der Unit DOM, interessant ebenfall die Units xmlreader und xmlwrite.

himitsu 22. Jun 2015 11:00

AW: xml file erstellen
 
Da wirst du dir wohl eine der Excel-Komponenten kaufen müssen, welche dieses Ausgabeformat unterstützen.
Nur mit einer XML-Komponente kannst du das zwar machen, aber dann darfst du natürlich auch die komplette Excel-XML-Spreadsheet-Datenstruktur selber implementieren. :roll:

Alternativ verstecken sich in der DP mehrere Komponenten, welche Dateien im "alten" binären XLS-Format erstellen können.

[edit]
Oder wie Perlsau es nennen wird.
Excel "fernsteuern", wofür es natürlich installiert sein muß.

Perlsau 22. Jun 2015 11:03

AW: xml file erstellen
 
Zitat:

Zitat von khh (Beitrag 1306085)
nee, ich meine schon xml, keine csv die dann erst durch excel konvertiert werden müssen

Du könntest mit Ole-Automation arbeiten und Excel mit deinem Programm fernsteuern. Dazu gibt es zahlreiche Hinweise im Netz ...

CCRDude 22. Jun 2015 11:07

AW: xml file erstellen
 
Denke wenn man das nicht zu universell will, geht das mit nem reinem XML-Writer schon (Beispiel).

Das alte Excel-Format geht in Lazarus auch mit dem Package fpsreadsheet, habe ich auch schon eingesetzt, weil mit CSV keine Tabs etc. möglich sind. Unterstützt unter anderem Excel 5, Excel 8, OpenDocument, und OpenOffice XML.

khh 22. Jun 2015 11:10

AW: xml file erstellen
 
also fpspreadsheet-1.4.3 hab ich installiert, aber wie das funktioniert hab ich noch nicht durchschaut.

Kann das denn dann nur das alte Format?

khh 22. Jun 2015 11:12

AW: xml file erstellen
 
Zitat:

Zitat von CCRDude (Beitrag 1306092)
Denke wenn man das nicht zu universell will, geht das mit nem reinem XML-Writer schon .

was für ein XML-Writer denn ?

CCRDude 22. Jun 2015 12:28

AW: xml file erstellen
 
Zitat:

Zitat von khh (Beitrag 1306096)
also fpspreadsheet-1.4.3 hab ich installiert, aber wie das funktioniert hab ich noch nicht durchschaut.

Neues Objekt der Klasse TsWorkbook anlegen, mit AddWorksheet ein neues worksheet anlegen. TsWorksheet hat dann diverse Write...-Fukntionen für Text, Zahlen, Formeln.


Zitat:

Zitat von khh (Beitrag 1306096)
Kann das denn dann nur das alte Format?

Oben von mir genannte Formate. Sonst siehe Doku.

khh 22. Jun 2015 17:59

AW: xml file erstellen
 
alsooo

mit TXMLDocument habe ich jetzt eine Datei erzeugt.

so
Delphi-Quellcode:
  pfad := 'C:\temp2\test2.xml';
    Doc := TXMLDocument.Create;
    Doc.XMLVersion:= '1.0';
    WriteXMLFile(Doc, pfad);
bekomme ich eine Zeile in das File:
Delphi-Quellcode:
<?xml version="1.0" encoding="UTF-8"?>
immerhin etwas ;-)


aber wie bekomme ich meine Excelstruktur:

Delphi-Quellcode:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
    <Author>ich</Author>
    <LastAuthor>ich</LastAuthor>
    <Created>2012-02-01T12:31:29Z</Created>
    <Version>15.00</Version>
  </DocumentProperties>
  <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
    <AllowPNG />
  </OfficeDocumentSettings>
  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
    <WindowHeight>12435</WindowHeight>
    <WindowWidth>28800</WindowWidth>
    <WindowTopX>0</WindowTopX>
    <WindowTopY>0</WindowTopY>
    <ProtectStructure>False</ProtectStructure>
    <ProtectWindows>False</ProtectWindows>
  </ExcelWorkbook>
  <Styles>
...

...
....
Die Beispiele die ich gefunden habe, erzeugen mir so was:
Delphi-Quellcode:
<?xml version="1.0"?>
<Root>
  <Element id="1">
    <Item1 Attr1="1" Attr2="1">Item1Value is 1</Item1>
    <Item2 Attr1="1" Attr2="1">Item2Value is 1</Item2>
  </Element>
  <Element id="2">
    <Item1 Attr1="2" Attr2="2">Item1Value is 2</Item1>
    <Item2 Attr1="2" Attr2="2">Item2Value is 2</Item2>
  </Element>
  <Element id="3">
    <Item1 Attr1="3" Attr2="3">Item1Value is 3</Item1>
    <Item2 Attr1="3" Attr2="3">Item2Value is 3</Item2>
  </Element>
</Root>
das sieht zwar schon mal wie ein xml-File aus, aber noch lange nicht so, wie ich meine (Excel-) Datei haben möchte. Gibt es irgendwo brauchbare Beispiele ?

Ich danke euch

khh 23. Jun 2015 07:10

AW: xml file erstellen
 
keiner ne idee? push

vagtler 23. Jun 2015 07:23

AW: xml file erstellen
 
Das musst Du schon "von Hand" machen.

khh 23. Jun 2015 07:46

AW: xml file erstellen
 
wie meinst du das "von Hand" ?

Back2Code 23. Jun 2015 07:53

AW: xml file erstellen
 
Zitat:

Zitat von khh (Beitrag 1306207)
wie meinst du das "von Hand" ?

Schreib dir ne eigene Klasse die dir deine Xml generiert oder schau dir ma den Xml Wizard an (Ist aber nur bedingt zu gebrauchen, 1a++ Entwicklungsarbeit:lol: )

vagtler 23. Jun 2015 07:54

AW: xml file erstellen
 
Na, die Elemente und Attribute musst Du schon selbst implementieren, wenn Du es mit einer generischen XML-Komponente machst.

CCRDude 23. Jun 2015 12:01

AW: xml file erstellen
 
Auch wenn ich der Meinung bin, dass man aus einem Beispiel, das das eine erzeugt, auch was hinkriegen sollte - weil's nur fünf Minuten waren hier ein Beispiel:

Delphi-Quellcode:
// https://en.wikipedia.org/wiki/Microsoft_Office_XML_formats
// http://www.delphipraxis.net/185583-xml-file-erstellen-2.html

program ExcelXMLOutputDemo;

uses
   SysUtils,
   DOM,
   XMLWrite;

   procedure CreateXML(const AFilename: string);
   var
      doc: TXMLDocument;

      function AddTextNode(AParentNode: TDOMNode; ANodeName, ATextValue: DOMString): TDOMNode;
      begin
         Result := doc.CreateElement(ANodeName);
         AParentNode.AppendChild(Result);
         Result.AppendChild(doc.CreateTextNode(ATextValue));
      end;

   var
      nRoot, nDocProperties, nDocSettings: TDOMNode;
   begin
      doc := TXMLDocument.Create;
      try
         doc.XMLVersion := '1.0';
         doc.AppendChild(doc.CreateProcessingInstruction('mso-application', 'progid="Excel.Sheet"'));

         nRoot := doc.CreateElement('Workbook');
         TDOMElement(nRoot).SetAttribute('xmlns', 'urn:schemas-microsoft-com:office:spreadsheet');
         TDOMElement(nRoot).SetAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
         TDOMElement(nRoot).SetAttribute('xmlns:x', 'urn:schemas-microsoft-com:office:excel');
         TDOMElement(nRoot).SetAttribute('xmlns:ss', 'urn:schemas-microsoft-com:office:spreadsheet');
         TDOMElement(nRoot).SetAttribute('xmlns:html', 'http://www.w3.org/TR/REC-html40');
         doc.AppendChild(nRoot);

         nDocProperties := doc.CreateElement('DocumentProperties');
         TDOMElement(nDocProperties).SetAttribute('xmlns', 'urn:schemas-microsoft-com:office:office');
         nRoot.AppendChild(nDocProperties);

         AddTextNode(nDocProperties, 'Author', 'CCRDude');
         AddTextNode(nDocProperties, 'LastAuthor', 'CCRDude');
         AddTextNode(nDocProperties, 'Created', FormatDateTime('yyyy-mm-dd', Now) + 'T' + FormatDateTime('hh:nn:ss', Now) + 'Z');
         AddTextNode(nDocProperties, 'Version', '15.0');

         nDocSettings := doc.CreateElement('OfficeDocumentSettings');
         TDOMElement(nDocSettings).SetAttribute('xmlns', 'urn:schemas-microsoft-com:office:office');
         nRoot.AppendChild(nDocSettings);

         nDocSettings.AppendChild(doc.CreateElement('AllowPNG'));

         WriteXML(doc, AFilename);
      finally
         doc.Free;
      end;
   end;

begin
   CreateXML('C:\Tests\excel.xml');
end.
Ergibt:

Code:
<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
    <Author>CCRDude</Author>
    <LastAuthor>CCRDude</LastAuthor>
    <Created>2015-06-23T12:57:52Z</Created>
    <Version>15.0</Version>
  </DocumentProperties>
  <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
    <AllowPNG/>
  </OfficeDocumentSettings>
</Workbook>
In dem Beispiel dürfte alles drin sein - von Processing Instructions über Nodes (auch leeren), Text Nodes und Attributen.

khh 23. Jun 2015 12:30

AW: xml file erstellen
 
ich danke dir herzlich

khh 24. Jun 2015 09:34

AW: xml file erstellen
 
mh, jetzt fehlen mir noch nodes die als parent nen Node haben.


vergesst den Post,
ich habs, war wohl zu früh am Tag ;-)

khh 24. Jun 2015 12:27

AW: xml file erstellen
 
doch noch ne Frage ;-)
Delphi-Quellcode:
TDOMElement(nDocSettings).SetAttribute('ss:ID', 'Default');
erstellt die Atttribute des Notes "Styles".
Folgendermassen hänge ich ein Note als Kind ein.

Delphi-Quellcode:

 nDocSettings := doc.CreateElement('Styles');
 nDocSettings.AppendChild(doc.CreateElement('Style'));
aber wie bekomme ich die Attribute des Notes "Style" erstellt?

erledigt, danke ;-)
so:
Delphi-Quellcode:
  nDocSettings := doc.CreateElement('Styles');
  nChild:=   nDocSettings.AppendChild(doc.CreateElement('Style'));
  TDOMElement(nChild).SetAttribute('ss:ID', 'Default');
hab nachgedacht ;-)


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:33 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz