Qt xml:DOM写可以直接用EXCEL打开的xml文件
来源:互联网 发布:网络教育没有按时毕业 编辑:程序博客网 时间:2024/06/05 04:16
前一篇写了如何用DOM方式生成XML文件,生成的文件可以直接用浏览器打开,可以选择EXCEL打开,将数据导入到EXCEL中,不能直接用EXCEL打开。但是有时候有需求需要不看到xml文件源码,而只希望看到其中的内容,比如直接双击就可以在EXCEL中显示,此篇就是实现将table列表中的内容直接导出到可以用EXCEL打开的xml文件中。其实生成过程是一样的 ,就是需要按照EXCEL的XML源码方式式样去写文件。
其实我个人对XML并不是很了解,现用现学的去了解了一些内容,可以到 http://www.w3school.com.cn/xml/ 中学习XML的相关知识。对EXECL那些更不懂了,老大教了一个简单方法,直接将excel文件保存为xml格式,用ultraEdit打开,然后可以照着写预处理指令了,我只采用了最简单的,把很多指令都去掉,只剩下基本的EXCEL页面。保留一个worksheet。具体实现如下:
QDomDocument modelDoc;
其实我个人对XML并不是很了解,现用现学的去了解了一些内容,可以到 http://www.w3school.com.cn/xml/ 中学习XML的相关知识。对EXECL那些更不懂了,老大教了一个简单方法,直接将excel文件保存为xml格式,用ultraEdit打开,然后可以照着写预处理指令了,我只采用了最简单的,把很多指令都去掉,只剩下基本的EXCEL页面。保留一个worksheet。具体实现如下:
QDomDocument modelDoc;
QDomProcessingInstruction instruction;
instruction = modelDoc.createProcessingInstruction("xml version=\"1.0\"", "encoding=\"utf-8\"");
modelDoc.appendChild(instruction);
instruction = modelDoc.createProcessingInstruction("mso-application progid=\"Excel.Sheet\"", "");
modelDoc.appendChild(instruction);
//生成根节点
QDomElement root = modelDoc.createElement("Workbook"); //添加预处理指令,有了这些,才能用excel直接打开xml文件
root.setAttribute("xmlns", "urn:schemas-microsoft-com:office:spreadsheet");
root.setAttribute("\nxmlns:o", "urn:schemas-microsoft-com:office:office");
root.setAttribute("\nxmlns:x", "urn:schemas-microsoft-com:office:excel");
modelDoc.appendChild(root); //添加根元素
QDomElement workSheet = modelDoc.createElement("Worksheet");
QDomAttr rootAttr = modelDoc.createAttribute("ss:Name");
rootAttr.setValue("Sheet1");
workSheet.setAttributeNode(rootAttr);
root.appendChild(workSheet);
QDomElement table = modelDoc.createElement("Table");
workSheet.appendChild(table);
QString count;
count = count.setNum(pModel->columnCount());
table.setAttribute("ss:ExpandedColumnCount", count);
count.clear(); count = count.setNum(pModel->rowCount() + 1); //加上列头1行 table.setAttribute("ss:ExpandedRowCount", count); table.setAttribute("x:FullColumns", "1"); table.setAttribute("x:FullRows", "1"); table.setAttribute("ss:DefaultColumnWidth", "54"); table.setAttribute("ss:DefaultRowHeight", "13"); //定义xml树的节点 QDomElement modelElement; QDomElement dataElement; QDomText modelElementNodeText; //写列头 QDomElement row = modelDoc.createElement(tr("Row")); table.appendChild(row); for(int columnIndex = 0; columnIndex < pModel->columnCount(); columnIndex++) { modelElement = modelDoc.createElement("Cell"); dataElement = modelDoc.createElement("Data"); dataElement.setAttribute("ss:Type", "String"); modelElement.appendChild(dataElement); modelElementNodeText = modelDoc.createTextNode(pModel->horizontalHeaderItem(columnIndex)->data(Qt::DisplayRole).toString()); dataElement.appendChild(modelElementNodeText); row.appendChild(modelElement); }//生成xml树 for(int rowIndex = 0; rowIndex < pModel->rowCount(); rowIndex++) { QDomElement row = modelDoc.createElement(tr("Row")); table.appendChild(row); for(int columnIndex = 0; columnIndex < pModel->columnCount(); columnIndex++) { modelElement = modelDoc.createElement("Cell"); dataElement = modelDoc.createElement("Data"); dataElement.setAttribute("ss:Type", "String"); modelElement.appendChild(dataElement); if(pModel->item(rowIndex, columnIndex) != NULL) { modelElementNodeText = modelDoc.createTextNode(pModel->item(rowIndex, columnIndex)->data(Qt::EditRole).toString()); } else { modelElementNodeText = modelDoc.createTextNode(""); } dataElement.appendChild(modelElementNodeText); row.appendChild(modelElement); } table.appendChild(row); } QTextStream outFile(&file); modelDoc.save(outFile, 4); file.close();
生成的xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Excel.Sheet" ?>
<Workbook
xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office">
<Worksheet ss:Name="Sheet1">
<Table ss:DefaultRowHeight="13" x:FullRows="1" x:FullColumns="1" ss:ExpandedRowCount="8" ss:ExpandedColumnCount="18" ss:DefaultColumnWidth="54">
<Row>
<Cell>
<Data ss:Type="String">高度</Data>
</Cell>
<Cell>
<Data ss:Type="String">速度</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">113.8682922</Data>
</Cell>
<Cell>
<Data ss:Type="String">33.0761344</Data>
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
正因为有了前面那些execl格式的预处理指令加节点名称,所以导出的XML文件可以直接双击用EXCEL打开了
- Qt xml:DOM写可以直接用EXCEL打开的xml文件
- Qt用Dom写XML文件
- QT DOM的方式读写Xml文件
- Qt写xml文件
- QT 写XML文件
- Qt:用Dom方式读写xml文件
- QT Dom读xml文件
- Python:Dom生成XML文件(写XML)
- Python:Dom生成XML文件(写XML)
- qt使用QDomDocument写xml(qt,xml,dom
- ie无法直接打开xml 文件的解决方法:
- Qt下Dom方式修改xml文件的结点值
- VC DOM 直接创建XML(不加载XML文件的方式)
- Qt使用Dom操作XML文件
- QT DOM方式写入xml文件
- QT DOM方式读取xml文件(QDomElement)
- Qt使用Dom操作XML文件
- Qt使用Dom操作XML文件
- 读取Cookie及Cookie所有属性操作方法,Cookie大小
- 對一變數多次if判斷的代碼一般寫法
- 在VB6中使用集合
- 设计模式六大原则:里氏替换原则
- crontab定时功能
- Qt xml:DOM写可以直接用EXCEL打开的xml文件
- Alibaba --oracle数据库的CPU/IO信息采集
- 3.5
- POJ 3436 ACM Computer Factory
- session与cookie的生命周期
- HttpURLConnection的使用
- server.transfer与 response.redirect 的区别
- u-boot文档之 README摘要
- Android Service 的使用(二)----Service的绑定使用