简单的XML2003电子表格如下:
<?mso-application progid="Excel.Sheet"?>
<Workbook >
<DocumentProperties></DocumentProperties>
<OfficeDocumentSettings></OfficeDocumentSettings>
<ExcelWorkbook></ExcelWorkbook>
<Styles></Styles>
<Worksheet>
<Table>
<Column/>
<Row>
<Cell><Data>520</Data></Cell>
</Row>
</Table>
<WorksheetOptions></WorksheetOptions>
</Worksheet>
</Workbook>
XML2003电子表格大致由上元素构成。
APP中加上com初始化:
BOOL CExceltestApp::InitInstance()
{
AfxEnableControlContainer();
CoInitialize(NULL); 先初始化COM
当然结束时要卸载:
int CExceltestApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
CoUninitialize();
return CWinApp::ExitInstance();
}
#include <afxtempl.h> CTypedPtrArray
#include "msxml.h"
#import "C:/windows/system32/msxml4.dll"
using namespace MSXML2;
struct YXYZWStruct
{
int nSerialNo;
int nYXID;
CString strYXInfo;
CString strHostIP;
int nVCHNo;
int nYZWNo;
CString strYZWInfo;
};
typedef CTypedPtrArray<CPtrArray,YXYZWStruct*> PYZWLinkArray;
class CXMLAnalysis
{
public:
CXMLAnalysis(CString fileName);
IXMLDOMElementPtr findElement(_bstr_t elementName,IXMLDOMElementPtr pEle);
///IXMLDOMElementPtr findTargetElement();
IXMLDOMElementPtr getStartRowOfWorksheet(IXMLDOMElementPtr pWorkSheet);
void getXMLData(PYZWLinkArray &m_YZWLinkArray);
virtual ~CXMLAnalysis();
private:
IXMLDOMDocumentPtr m_pDoc;
IXMLDOMElementPtr m_xmlRoot;
};
实现文件:
CXMLAnalysis::CXMLAnalysis(CString fileName)
{
HRESULT hr=m_pDoc.CreateInstance(_uuidof(MSXML2::DOMDocument));
if(!SUCCEEDED(hr))
{
AfxMessageBox("can't create dom object!");
return;
}
if(!m_pDoc->load((LPCTSTR)fileName))
{
AfxMessageBox("load failed!");
return;
}
m_xmlRoot=m_pDoc->GetdocumentElement();//documentElement; ///get root element
}
根据tagName找到元素的行
IXMLDOMElementPtr CXMLAnalysis::findElement(_bstr_t elementName,IXMLDOMElementPtr pEle)
{
IXMLDOMElementPtr elePtr=pEle->GetfirstChild();//pEle->firstChild;
while(elePtr!=NULL)
{
if(elePtr->tagName==elementName)
{
return elePtr;
}
elePtr=elePtr->GetnextSibling();//nextSibling;
}
return NULL;
}
///得到worksheet的元素的开始行
IXMLDOMElementPtr CXMLAnalysis::getStartRowOfWorksheet(IXMLDOMElementPtr pWorkSheet)
{
IXMLDOMElementPtr table=pWorkSheet->GetfirstChild();
IXMLDOMElementPtr row=table->GetfirstChild();//firstChild;
while(row!=NULL)
{
IXMLDOMElementPtr cell=row->GetfirstChild();//firstChild;
if(cell!=NULL)
{
IXMLDOMElementPtr data=cell->GetfirstChild();
if(data!=NULL)
{
CString str=(LPCTSTR)data->text;
if(!str.IsEmpty() && str=="序号")
{
row=row->GetnextSibling();
return row;
}
}
}
row=row->nextSibling;
}
return NULL;
}
void CXMLAnalysis::getXMLData(PYZWLinkArray & m_YZWLinkArray)
{
IXMLDOMElementPtr worksheet=findElement("Worksheet",m_xmlRoot);
while(worksheet!=NULL)
{
IXMLDOMElementPtr startRow=getStartRowOfWorksheet(worksheet);
while(startRow!=NULL) ///start data row
{
int currentRow=0;
IXMLDOMElementPtr cell=startRow->GetfirstChild();
while(cell!=NULL)
{
if(cell->GetfirstChild()!=NULL)
{
CString str=(LPCTSTR)cell->GetfirstChild()->text;
YXYZWStruct *pYXYZWS=new YXYZWStruct;
switch (currentRow)
{
case 0:
pYXYZWS->nSerialNo=_ttoi(str); break;
case 1:
pYXYZWS->nYXID=_ttoi(str); break;
case 2:
pYXYZWS->strYXInfo=str; break;
case 3:
pYXYZWS->strHostIP=str; break;
case 4:
pYXYZWS->nVCHNo=_ttoi(str); break;
case 5:
pYXYZWS->nYZWNo=_ttoi(str); break;
case 6:
pYXYZWS->strYZWInfo=str; break;
default:
break;
}
m_YZWLinkArray.Add(pYXYZWS);
currentRow++;
}
cell=cell->GetnextSibling();
}
startRow=startRow->GetnextSibling();
}
worksheet=worksheet->GetnextSibling();
}
}