{"id":63,"date":"2020-07-24T11:32:08","date_gmt":"2020-07-24T03:32:08","guid":{"rendered":"https:\/\/blog.linsakura.com\/?p=63"},"modified":"2023-02-14T15:49:35","modified_gmt":"2023-02-14T07:49:35","slug":"qt-export-xml-for-excel","status":"publish","type":"post","link":"https:\/\/blog.linsakura.com\/index.php\/2020\/07\/24\/qt-export-xml-for-excel\/","title":{"rendered":"Qt\u5bfc\u51faXML\u683c\u5f0f\u7684Excel\u6587\u4ef6"},"content":{"rendered":"<h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<div class=\"link-title wow rollIn\">\u5185\u5bb9\u6bb5 \u8bd5\u4e00\u4e0b<\/div>\n<pre class=\"code\">void Widget::ExportXML()\r\n{\r\n    const int TB_MAX = 3;   \/\/\u5de5\u4f5c\u8868\u5f20\u6570\r\n\r\n    QFile file(\".\/export.xml\");\r\n    if(!file.open(QFile::WriteOnly | QFile::Text))\r\n    {\r\n        return;\r\n    }\r\n    QXmlStreamWriter xml(&amp;file);\r\n    xml.setCodec(\"UTF-8\");            \/\/\u8bbe\u7f6eUTF-8\u7f16\u7801\r\n    xml.setAutoFormatting(true);      \/\/\u6807\u7b7e\u7f29\u8fdb\r\n    xml.setAutoFormattingIndent(1);   \/\/\u6807\u7b7e\u7f29\u8fdb\u7a7a\u683c\u6570\r\n    xml.writeStartDocument();\r\n    xml.writeProcessingInstruction(\"mso-application progid=\\\"Excel.Sheet\\\"\");\r\n    xml.writeStartElement(\"Workbook\");\r\n    xml.writeAttribute(\"xmlns\", \"urn:schemas-microsoft-com:office:spreadsheet\");\r\n    xml.writeAttribute(\"xmlns:x\", \"urn:schemas-microsoft-com:office:excel\");\r\n    xml.writeAttribute(\"xmlns:ss\", \"urn:schemas-microsoft-com:office:spreadsheet\");\r\n    xml.writeAttribute(\"xmlns:html\", \"http:\/\/www.w3.org\/TR\/REC-html40\");\r\n\r\n    for(int i = 0; i &lt; TB_MAX; i++)\r\n    {\r\n        xml.writeStartElement(\"Worksheet\");   \/\/\u5de5\u4f5c\u8868\r\n        xml.writeAttribute(\"ss:Name\", tr(\"\u8868 \")+QString::number(i+1));   \/\/\u5de5\u4f5c\u8868\u540d\r\n        xml.writeStartElement(\"Table\");\r\n        xml.writeStartElement(\"Column\");\r\n        xml.writeAttribute(\"ss:AutoFitWidth\", \"0\");\r\n        xml.writeEndElement();  \/\/\/\u3010\"Column\"\u3011\r\n        QStringList slData;\r\n        slData &lt;&lt; tr(\"\u59d3\u540d\") &lt;&lt; tr(\"\u5e74\u9f84\") &lt;&lt; tr(\"\u8eab\u9ad8\");\r\n        xml.writeStartElement(\"Row\");\r\n        foreach(QString str, slData)\r\n        {\r\n            xml.writeStartElement(\"Cell\");\r\n            xml.writeStartElement(\"Data\");\r\n            xml.writeAttribute(\"ss:Type\", \"String\");\r\n            xml.writeCharacters(str);\r\n            xml.writeEndElement();  \/\/\/\u3010\"Data\"\u3011\r\n            xml.writeEndElement();  \/\/\/\u3010\"Cell\"\u3011\r\n        }\r\n        xml.writeEndElement();  \/\/\/\u3010\"Row\"\u3011\r\n        xml.writeStartElement(\"Row\");\r\n        slData.clear();\r\n        slData &lt;&lt; \"\u535a\u4e3d\u7075\u68a6\" &lt;&lt; \"18\" &lt;&lt; \"163\";\r\n        foreach(QString str, slData)\r\n        {\r\n            xml.writeStartElement(\"Cell\");\r\n            xml.writeStartElement(\"Data\");\r\n            xml.writeAttribute(\"ss:Type\", \"String\");\r\n            xml.writeCharacters(str);\r\n            xml.writeEndElement();  \/\/\/\u3010\"Data\"\u3011\r\n            xml.writeEndElement();  \/\/\/\u3010\"Cell\"\u3011\r\n        }\r\n        xml.writeEndElement();  \/\/\/\u3010\"Row\"\u3011\r\n\r\n        xml.writeEndElement();  \/\/\/\u3010\"Table\"\u3011\r\n        \/\/START &lt;-- \u8fd9\u90e8\u5206\u662f\u4e3a\u4e86\u51bb\u7ed3\u524d1\u884c\u7684\u8868\u5934\r\n        xml.writeStartElement(\"WorksheetOptions\");\r\n        xml.writeAttribute(\"xmlns\", \"urn:schemas-microsoft-com:office:excel\");\r\n        xml.writeStartElement(\"FreezePanes\");\r\n        xml.writeEndElement();  \/\/\/\u3010\"FreezePanes\"\u3011\r\n        xml.writeStartElement(\"FrozenNoSplit\");\r\n        xml.writeEndElement();  \/\/\/\u3010\"FrozenNoSplit\"\u3011\r\n        xml.writeStartElement(\"SplitHorizontal\");\r\n        xml.writeCharacters(\"1\");\r\n        xml.writeEndElement();  \/\/\/\u3010\"SplitHorizontal\"\u3011\r\n        xml.writeStartElement(\"TopRowBottomPane\");\r\n        xml.writeCharacters(\"1\");\r\n        xml.writeEndElement();  \/\/\/\u3010\"TopRowBottomPane\"\u3011\r\n        xml.writeStartElement(\"ActivePane\");\r\n        xml.writeCharacters(\"2\");\r\n        xml.writeEndElement();  \/\/\/\u3010\"ActivePane\"\u3011\r\n        xml.writeStartElement(\"Panes\");\r\n        xml.writeStartElement(\"Pane\");\r\n        xml.writeStartElement(\"Number\");\r\n        xml.writeCharacters(\"3\");\r\n        xml.writeEndElement();  \/\/\/\u3010\"Number\"\u3011\r\n        xml.writeEndElement();  \/\/\/\u3010\"Pane\"\u3011\r\n        xml.writeStartElement(\"Pane\");\r\n        xml.writeStartElement(\"Number\");\r\n        xml.writeCharacters(\"2\");\r\n        xml.writeEndElement();  \/\/\/\u3010\"Number\"\u3011\r\n        xml.writeEndElement();  \/\/\/\u3010\"Pane\"\u3011\r\n        xml.writeEndElement();  \/\/\/\u3010\"Panes\"\u3011\r\n        xml.writeStartElement(\"ProtectObjects\");\r\n        xml.writeCharacters(\"False\");\r\n        xml.writeEndElement();  \/\/\/\u3010\"ProtectObjects\"\u3011\r\n        xml.writeStartElement(\"ProtectScenarios\");\r\n        xml.writeCharacters(\"False\");\r\n        xml.writeEndElement();  \/\/\/\u3010\"ProtectScenarios\"\u3011\r\n        xml.writeEndElement();  \/\/\/\u3010\"WorksheetOptions\"\u3011\r\n        \/\/\u8fd9\u90e8\u5206\u662f\u4e3a\u4e86\u51bb\u7ed3\u524d1\u884c\u7684\u8868\u5934 --&gt; END\r\n        xml.writeEndElement();  \/\/\/\u3010\"Worksheet\"\u3011\r\n    }\r\n    xml.writeEndElement();  \/\/\/\u3010\"Workbook\"\u3011\r\n    xml.writeEndDocument();\r\n    file.close();\r\n}\r\n<\/pre>\n<h3>\u5199\u5165\u7c7b\u578b\u95ee\u9898<\/h3>\n<p>\u5982\u679c\u5199\u5165\u7c7b\u578b\u662f\u6570\u5b57\uff0c\u5219\u9700\u8981\u5c06<code>Cell<\/code> =&gt; <code>Data<\/code>\u6807\u7b7e\u7684<code>ss:Type<\/code>\u5c5e\u6027\u8bbe\u7f6e\u4e3a<strong>Number<\/strong>\uff0c\u5982\u4e0b\u56fe\u4ee3\u7801\uff1a<\/p>\n<pre class=\"code\">\/\/...\r\nQString strNum = \"1994\";\r\nxml.writeStartElement(\"Cell\");\r\nxml.writeStartElement(\"Data\");\r\nxml.writeAttribute(\"ss:Type\", \"Number\");\r\nxml.writeCharacters(strNum );\r\nxml.writeEndElement();  \/\/\/\u3010\"Data\"\u3011\r\nxml.writeEndElement();  \/\/\/\u3010\"Cell\"\u3011\r\n\/\/...\r\n<\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a<br \/>\n\u5982\u679c\u5199\u5165\u7c7b\u578b\u4e3a<strong>Number<\/strong>\uff0c\u4f46\u662f<code>writeCharacters<\/code>\u5199\u5165\u7684\u53c8\u662f\u7a7a\u6587\u672c\uff0c\u5219xml\u5b9e\u9645\u6253\u5f00\u4f1a\u662f\u663e\u793a\u4e3a&#8221;0&#8243;\u7684\u6570\u5b57\u5355\u5143\u683c\u3002<br \/>\n\u6240\u4ee5\u5982\u679c\u60f3\u5199\u5165\u4e3a\u7a7a\u7684\u5355\u5143\u683c\uff0c\u4e00\u5b9a<strong>\u4e0d\u8981<\/strong>\u8bbe\u7f6e\u4e3aNumber\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u793a\u4f8b\u4ee3\u7801 \u5185\u5bb9\u6bb5 \u8bd5\u4e00\u4e0b void Widget::ExportXML() { const int TB_MAX = 3; \/\/\u5de5\u4f5c\u8868\u5f20\u6570 QFile file(&#8220;.\/export.xml&#8221;); if(!file.open(QFile::WriteOnly | QFile::Text)) { return; } QXmlStreamWriter xml(&amp;file); xml.setCod [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[23,16,22],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-dev","category-qt","tag-excel","tag-qt","tag-xml"],"_links":{"self":[{"href":"https:\/\/blog.linsakura.com\/index.php\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.linsakura.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.linsakura.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.linsakura.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.linsakura.com\/index.php\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":5,"href":"https:\/\/blog.linsakura.com\/index.php\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":201,"href":"https:\/\/blog.linsakura.com\/index.php\/wp-json\/wp\/v2\/posts\/63\/revisions\/201"}],"wp:attachment":[{"href":"https:\/\/blog.linsakura.com\/index.php\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.linsakura.com\/index.php\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.linsakura.com\/index.php\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}