VC6.0操作XML电子表格2003

简单的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();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值