示例代码

void Widget::ExportXML()
{
    const int TB_MAX = 3;   //工作表张数

    QFile file("./export.xml");
    if(!file.open(QFile::WriteOnly | QFile::Text))
    {
        return;
    }
    QXmlStreamWriter xml(&file);
    xml.setCodec("UTF-8");            //设置UTF-8编码
    xml.setAutoFormatting(true);      //标签缩进
    xml.setAutoFormattingIndent(1);   //标签缩进空格数
    xml.writeStartDocument();
    xml.writeProcessingInstruction("mso-application progid=\"Excel.Sheet\"");
    xml.writeStartElement("Workbook");
    xml.writeAttribute("xmlns", "urn:schemas-microsoft-com:office:spreadsheet");
    xml.writeAttribute("xmlns:x", "urn:schemas-microsoft-com:office:excel");
    xml.writeAttribute("xmlns:ss", "urn:schemas-microsoft-com:office:spreadsheet");
    xml.writeAttribute("xmlns:html", "http://www.w3.org/TR/REC-html40");

    for(int i = 0; i < TB_MAX; i++)
    {
        xml.writeStartElement("Worksheet");   //工作表
        xml.writeAttribute("ss:Name", tr("表 ")+QString::number(i+1));   //工作表名
        xml.writeStartElement("Table");
        xml.writeStartElement("Column");
        xml.writeAttribute("ss:AutoFitWidth", "0");
        xml.writeEndElement();  ///【"Column"】
        QStringList slData;
        slData << tr("姓名") << tr("年龄") << tr("身高");
        xml.writeStartElement("Row");
        foreach(QString str, slData)
        {
            xml.writeStartElement("Cell");
            xml.writeStartElement("Data");
            xml.writeAttribute("ss:Type", "String");
            xml.writeCharacters(str);
            xml.writeEndElement();  ///【"Data"】
            xml.writeEndElement();  ///【"Cell"】
        }
        xml.writeEndElement();  ///【"Row"】
        xml.writeStartElement("Row");
        slData.clear();
        slData << "博丽灵梦" << "18" << "163";
        foreach(QString str, slData)
        {
            xml.writeStartElement("Cell");
            xml.writeStartElement("Data");
            xml.writeAttribute("ss:Type", "String");
            xml.writeCharacters(str);
            xml.writeEndElement();  ///【"Data"】
            xml.writeEndElement();  ///【"Cell"】
        }
        xml.writeEndElement();  ///【"Row"】

        xml.writeEndElement();  ///【"Table"】
        //START <-- 这部分是为了冻结前1行的表头
        xml.writeStartElement("WorksheetOptions");
        xml.writeAttribute("xmlns", "urn:schemas-microsoft-com:office:excel");
        xml.writeStartElement("FreezePanes");
        xml.writeEndElement();  ///【"FreezePanes"】
        xml.writeStartElement("FrozenNoSplit");
        xml.writeEndElement();  ///【"FrozenNoSplit"】
        xml.writeStartElement("SplitHorizontal");
        xml.writeCharacters("1");
        xml.writeEndElement();  ///【"SplitHorizontal"】
        xml.writeStartElement("TopRowBottomPane");
        xml.writeCharacters("1");
        xml.writeEndElement();  ///【"TopRowBottomPane"】
        xml.writeStartElement("ActivePane");
        xml.writeCharacters("2");
        xml.writeEndElement();  ///【"ActivePane"】
        xml.writeStartElement("Panes");
        xml.writeStartElement("Pane");
        xml.writeStartElement("Number");
        xml.writeCharacters("3");
        xml.writeEndElement();  ///【"Number"】
        xml.writeEndElement();  ///【"Pane"】
        xml.writeStartElement("Pane");
        xml.writeStartElement("Number");
        xml.writeCharacters("2");
        xml.writeEndElement();  ///【"Number"】
        xml.writeEndElement();  ///【"Pane"】
        xml.writeEndElement();  ///【"Panes"】
        xml.writeStartElement("ProtectObjects");
        xml.writeCharacters("False");
        xml.writeEndElement();  ///【"ProtectObjects"】
        xml.writeStartElement("ProtectScenarios");
        xml.writeCharacters("False");
        xml.writeEndElement();  ///【"ProtectScenarios"】
        xml.writeEndElement();  ///【"WorksheetOptions"】
        //这部分是为了冻结前1行的表头 --> END
        xml.writeEndElement();  ///【"Worksheet"】
    }
    xml.writeEndElement();  ///【"Workbook"】
    xml.writeEndDocument();
    file.close();
}

写入类型问题

如果写入类型是数字,则需要将Cell => Data标签的ss:Type属性设置为Number,如下图代码:

//...
QString strNum = "1994";
xml.writeStartElement("Cell");
xml.writeStartElement("Data");
xml.writeAttribute("ss:Type", "Number");
xml.writeCharacters(strNum );
xml.writeEndElement();  ///【"Data"】
xml.writeEndElement();  ///【"Cell"】
//...

注意
如果写入类型为Number,但是writeCharacters写入的又是空文本,则xml实际打开会是显示为”0″的数字单元格。
所以如果想写入为空的单元格,一定不要设置为Number类型。