1)Ole自动化Init
int xxx::Init()
{
if(!SUCCEEDED(CoInitialize(NULL)))
{
return -1;
}
try
{
if(!m_oApp.CreateDispatch("Excel.Application", NULL))
{
return -1;
}
m_oApp.SetDisplayAlerts(FALSE);
}
catch(COleDispatchException *)
{
return RETCODE_EXCEPTION;
}
return 0;
}
int xxx::Open(CString strFileName)
{
COleVariant vOpval((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
try
{
m_oBooks = m_oApp.GetWorkbooks();
m_book = m_oBooks.Open(strFileName,
vOpval,
vOpval,
vOpval,
vOpval,
vOpval,
vOpval,
vOpval,
vOpval,
vOpval,
vOpval,
vOpval,
vOpval);
}
catch(COleDispatchException*)
{
return -1;
}
return 0;
}
void xxxx::Quit()
{
COleVariant vOpval((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
try
{
m_book.Close(vOpval,vOpval,vOpval);
m_book.ReleaseDispatch();
m_oBooks.ReleaseDispatch();
m_oApp.Quit();
m_oApp.ReleaseDispatch();
}
catch(COleException *)
{
return;
}
catch(COleDispatchException *)
{
return;
}
CoUninitialize();
return;
}
int xxxx::ParseExcel()
{
COleVariant vTrue ((short)TRUE);
COleVariant vFalse ((short)FALSE);
COleVariant vOpval((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
int nSheetCount;
int nRet;
Worksheets sheets;
_Worksheet sheet;
try
{
sheets = m_book.GetWorksheets();
nSheetCount = sheets.GetCount();
for(int i=1; i<=nSheetCount; i++)
{
sheet = sheets.GetItem(COleVariant((short)i));
nRet = ParseSheet();
if(! nRet)
{
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
return nRet;
}
}
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
}
catch(COleException *)
{
return RETCODE_EXCEPTION;
}
catch(COleDispatchException *)
{
return -1;
}
return 0;
}
int xxxx::ParseSheet(_Worksheet &sheet,
ListVal* pListCound,
ListVal*pListRes,
int nSheet)
{
ListColumn pList1,pList2,pList3;
int nMaxrow = 0;
int nRet;
try
{
nRet = ParseColumn(sheet,IDS_SERVERNAME,&pList1,TRUE,nMaxrow);
if(! nRet)
{
FreeListColumn(&pList1);
FreeListColumn(&pList2);
FreeListColumn(&pList3);
return nRet;
}
nRet = ParseColumn(sheet,IDS_DATAITEM,&pList2,FALSE,nMaxrow);
if(! nRet)
{
FreeListColumn(&pList1);
FreeListColumn(&pList2);
FreeListColumn(&pList3);
return nRet;
}
nRet = ParseColumn(sheet,IDS_ITEMVALUE,&pList3,FALSE,nMaxrow);
if(! nRet)
{
FreeListColumn(&pList1);
FreeListColumn(&pList2);
FreeListColumn(&pList3);
return nRet;
}
//Create InterfaceCound Table
if(!GetInterfaceCound(&pList1,&pList2,&pList3,pListCound,nSheet))
{
FreeListColumn(&pList1);
FreeListColumn(&pList2);
FreeListColumn(&pList3);
return -1;
}
//Create InterfaceRes Table
if(!GetInterfaceRes(&pList1,&pList2,&pList3,pListRes,nSheet))
{
FreeListColumn(&pList1);
FreeListColumn(&pList2);
FreeListColumn(&pList3);
return -1;
}
FreeListColumn(&pList1);
FreeListColumn(&pList2);
FreeListColumn(&pList3);
}
catch(COleException *)
{
return RETCODE_EXCEPTION;
}
catch(COleDispatchException *)
{
return RETCODE_EXCEPTION;
}
return 0;
}
int XXXXX::ParseColumn(_Worksheet &sheet,
CString strColumn,
ListColumn *pList,
BOOL blflag,
int &nMaxrow)
{
Range range,range2,range3,range4;
long nRowCnt,nColumnCnt,nRcnt,nCcnt,nCcnt2,nRcnt2,nStComun,nEndColumn;
Font font;
COleVariant vTrue ((short)TRUE);
COleVariant vFalse ((short)FALSE);
COleVariant vOpval((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
CString strVal,strSheet,strErr;
int nRet;
BOOL blColumnExist = FALSE;
try
{
range = sheet.GetUsedRange();
strSheet = sheet.GetName();
range2 = range.GetRows();
if(!blflag)
{
nRowCnt = nMaxrow;
}
else
{
nRowCnt = range2.GetCount();
}
range2 = range.GetColumns();
nColumnCnt = range2.GetCount();
int lRow = GetStartRow(range,nRowCnt,nColumnCnt);
for(int i=1; i<=nColumnCnt; i++)
{
COleVariant vt;
vt = range.GetItem(COleVariant((short)lRow),COleVariant((short)i));
range3 = vt.pdispVal;
font = range3.GetFont();
if(IsStrikethrough(font))
{
continue;
}
range4 = range3.GetMergeArea();
nRcnt = ((Range)range4.GetRows()).GetCount();
nCcnt = ((Range)range4.GetColumns()).GetCount();
vt.ChangeType(VT_BSTR,NULL);
strVal = vt.bstrVal;
nStComun = i;
nEndColumn = i+ nCcnt-1;
TrimString(strVal);
if(!strVal.Compare(strColumn))
{
blColumnExist = TRUE;
MergeCell stCell;
CellList mylist;
stCell.nStartColumn = 0;
stCell.nStopColumn = 0;
stCell.nStratRow = 0;
stCell.nStopRow = 0;
for(int j = nStComun; (! j<=nEndColumn); j++)
{
for(int k=nRcnt+lRow; (! k<=nRowCnt); k++)
{
if(!IsInMergeCell(k,j,&mylist))
{
COleVariant vt;
CString strCell;
CExcelColumn* pExcelColumn = new CExcelColumn();
vt = range.GetItem(COleVariant((short)k),COleVariant((short)j));
range3 = vt.pdispVal;
range4 = range3.GetMergeArea();
font = range3.GetFont();
if(IsStrikethrough(font))
{
pExcelColumn->blDellineFlag = TRUE;
}
nRcnt2 = ((Range)range4.GetRows()).GetCount();
nCcnt2 = ((Range)range4.GetColumns()).GetCount();
if(blflag)
{
if(nCcnt2 > nCcnt)
{
pExcelColumn->blMergeColumnflag = TRUE;
}
}
stCell.nStratRow = k;
stCell.nStopRow = k + nRcnt2 -1;
stCell.nStartColumn = j;
stCell.nStopColumn = j+ nCcnt2 -1 ;
mylist.AddTail(stCell);
nRet = ParseCell(range,stCell,strCell);
if(RETCODE_NORMAL != nRet)
{
range4.ReleaseDispatch();
range2.ReleaseDispatch();
range3.ReleaseDispatch();
range.ReleaseDispatch();
font.ReleaseDispatch();
mylist.RemoveAll();
delete pExcelColumn;
return nRet;
}
if(blflag)
{
if(0 < strCell.GetLength())
{
pExcelColumn->nStartRow = k;
pExcelColumn->nStopRow = k+nRcnt2-1;
pExcelColumn->strColumnName = strColumn;
pExcelColumn->strContent = strCell;
pList->AddTail(pExcelColumn);
}
}
else
{
pExcelColumn->nStartRow = k;
pExcelColumn->nStopRow = k+nRcnt2-1;
pExcelColumn->strColumnName = strColumn;
pExcelColumn->strContent = strCell;
pList->AddTail(pExcelColumn);
}
}
}
}
mylist.RemoveAll();
}
}
if(blflag)
{
nMaxrow = GetMaxRow(pList);
}
range4.ReleaseDispatch();
range2.ReleaseDispatch();
range3.ReleaseDispatch();
range.ReleaseDispatch();
font.ReleaseDispatch();
}
catch(COleException *)
{
return RETCODE_EXCEPTION;
}
catch(COleDispatchException *)
{
return RETCODE_EXCEPTION;
}
if(!blColumnExist)
{
TrimString(strSheet);
if(!strSheet.Compare("") ||
!strSheet.Compare("") ||
!strSheet.Compare(""))
{
return 0;
}
if(!strcmp(strColumn,IDS_SERVERNAME))
{
strErr.Format(XXXXXX,strSheet);
}
else if(!strcmp(strColumn,IDS_DATAITEM))
{
strErr.Format(XXXXXX,strSheet);
}
else if(!strcmp(strColumn,IDS_ITEMVALUE))
{
strErr.Format(XXXXXX,strSheet);
}
}
return 0;
}
int xxxxxxx::ParseCell(Range &range, MergeCell &stCell,CString &strCell)
{
CString strTemp;
try
{
COleVariant vt = range.GetItem(COleVariant((short)stCell.nStratRow),
COleVariant((short)stCell.nStartColumn));
vt.ChangeType(VT_BSTR,NULL);
strTemp = vt.bstrVal;
TrimString(strTemp);
if(0<strTemp.GetLength())
{
strCell = strTemp;
}
else
{
strCell = "";
}
}
catch(COleException *)
{
return RETCODE_EXCEPTION;
}
catch(COleDispatchException *)
{
return RETCODE_EXCEPTION;
}
return 0;
}