用Com访问Excel文件,怎么获取列数?

火鸟33 系统架构师  2007-11-01 04:48:29
...全文
129 点赞 收藏 3
写回复
3 条回复
UltraBejing 2008年05月01日
支持搂主,收藏
回复 点赞
hellopower 2008年01月04日
包含什么头文件才能用?
回复 点赞
Zhentiwei 2007年11月01日

_Application app;

_Workbook book;
_Worksheet sheet;
Workbooks books;
Worksheets sheets;
Range range;

// often reused variable
LPDISPATCH lpDisp;

COleVariant covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
try
{
// Initialize OLE libraries

if( !app.CreateDispatch("Excel.Application") )
{
AfxMessageBox("Couldn't CreateDispatch() for Excel");
return;
}
app.SetVisible(false);
// app.SetUserControl(true);

// Get an IDispatch pointer
lpDisp = app.GetWorkbooks();
ASSERT(lpDisp);

// Attach the IDispatch pointer
books.AttachDispatch(lpDisp);

// to the books object.
lpDisp = books.Open(CGlobal::sExcFilePath,covOptional,
covOptional,covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional,
covOptional);
ASSERT(lpDisp);

// Return workbook's IDispatch pointer
book.AttachDispatch(lpDisp);
lpDisp = book.GetSheets();
ASSERT(lpDisp);

sheets.AttachDispatch(lpDisp);
lpDisp = sheets.GetItem( COleVariant((short)(1)) ); // get first sheet of book
ASSERT(lpDisp);

// Get Current sheet used
sheet.AttachDispatch(lpDisp);
lpDisp = sheet.GetUsedRange();
ASSERT(lpDisp);

// Search....
Range usedRange;
usedRange.AttachDispatch(lpDisp);

// Get gross rows and columns used
lpDisp = usedRange.GetRows();
ASSERT(lpDisp);

range.AttachDispatch(lpDisp);
long iRowNum = range.GetCount(); // gross rows
lpDisp = usedRange.GetColumns();
ASSERT(lpDisp);

range.AttachDispatch(lpDisp);
long iColNum = range.GetCount(); // gross columns

// Get start row and column
// based 1
long iStartRow = usedRange.GetRow();
long iStartCol = usedRange.GetColumn();

usedRange.ReleaseDispatch();

// Read Cell value and comparing the value that you want to delete it
CString esType, esBrand, esLotNo, esPack, esCount, esMemo;

for(long i = iStartRow+1; i <= iRowNum; i++)
{
COleVariant vResult ;
CString index;
CString s;

// 第一列 型号(long)1
index = "A";
s.Format("%d",i);
index = index + s;

lpDisp = sheet.GetRange( COleVariant(index), COleVariant(index) );
ASSERT(lpDisp);
range.AttachDispatch(lpDisp);
vResult = range.GetValue2();
Match(esType,vResult); // Get esType

// 第二列 品牌(long)2
index = "B";
index = index + s;

lpDisp = sheet.GetRange( COleVariant(index), COleVariant(index) );
range.AttachDispatch(lpDisp);
vResult = range.GetValue2();
Match(esBrand, vResult);

}

}
catch(COleException *e)
{
char buf[1024];
sprintf(buf,"COleException. SCODE: %08lx.",(long)e->m_sc);
::MessageBox(NULL,buf,"COleException", MB_SETFOREGROUND | MB_OK);
}
catch(COleDispatchException *e)
{
char buf[1024];
sprintf(buf,"COleDispatchException. SCODE: %08lx, Description: \" %s \".",
(long)e->m_wCode, (LPSTR)e->m_strDescription.GetBuffer(512));
::MessageBox(NULL,buf,"COleDispatchException",MB_SETFOREGROUND | MB_OK);
}
catch(...)
{
::MessageBox(NULL,"General Exception caught.","Catch-All",MB_SETFOREGROUND | MB_OK);
}

book.Save();
book.Close( covOptional,covOptional,covOptional);
books.Close();

range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
app.Quit();


_Application app;

_Workbook book;
_Worksheet sheet;
Workbooks books;
Worksheets sheets;
Range range;
知道怎么用吧?
回复 点赞
发动态
发帖子
数据库
创建于2007-09-28

3443

社区成员

3.9w+

社区内容

VC/MFC 数据库
社区公告
暂无公告