如何调用excel的API?用VC 6.0…………
我是要写一个程序去查找两个excel文件的表头的内容,如果有相同的,则查找表头下的内容看有没有相同的,再导出相同内容。但我之前没接触过excel的API,谁推荐本书也行…...
我是要写一个程序去查找两个excel文件的表头的内容,如果有相同的,则查找表头下的内容看有没有相同的,再导出相同内容。但我之前没接触过excel的API,谁推荐本书也行…………
展开
展开全部
这是我以前写的一段程序,可以参考。 现在没有安装Office,没法试了。
你在百度文库里找“使用MFC操作EXCEL文件”就可以找到资料。
_Application app; // app is an _Application object.
Workbooks books;
LPDISPATCH lpDisp; // Often reused variable.
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't CreateDispatch() for Excel");
return;
}
// Set visible.
app.SetVisible( FALSE );
lpDisp = app.GetWorkbooks(); // Get an IDispatch pointer.
ASSERT(lpDisp);
books.AttachDispatch(lpDisp); // Attach the IDispatch pointer
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
// To open an existing workbook, you need to provide all
// 13 arguments for the Open member function.
// The code below opens a workbook and adds it to the Workbook's
// Collection object.
// You need to modify the path and file name for your own
// workbook.
//
CString strFile = filename;
lpDisp = books.Open( strFile, // is a workbook.
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional ); // Return Workbook's IDispatch pointer.
ASSERT(lpDisp);
book.AttachDispatch( lpDisp );
// Get the Sheets collection and attach the IDispatch pointer to your
// sheets object.
lpDisp = book.GetSheets();
ASSERT(lpDisp);
sheets.AttachDispatch(lpDisp);
long nSheets = sheets.GetCount();
char outfile[256];
for (long no = 1; no <= nSheets; no++)
{
lpDisp = sheets.GetItem( COleVariant((short)( no )) );
ASSERT(lpDisp);
sheet.AttachDispatch(lpDisp);
lpDisp = sheet.GetUsedRange();
ASSERT(lpDisp);
range.AttachDispatch(lpDisp);
/*
Range r = range.GetColumns();
int cc = r.GetCount();
r = range.GetRows();
int rr = r.GetCount();
*/
VARIANT ret = range.GetValue();
if ( ret.vt == VT_EMPTY )
continue;
strcpy(outfile, filename);
char *p = strchr( outfile, '.' );
if ( p )
*p = 0;
CString str = outfile;
str += "_";
str += sheet.GetName();
if ( str.Find( "说明" ) == -1 &&
str.Find( "说明" ) == -1)
{
str_command[ 0 ].Add( str );
int sign = -1;
ftalk->write( (const char*)&sign, 4 );
int n = str_command[ 0 ].GetSize();
ftalk->write( (const char*)&n, 4 );
strcat(outfile, (LPCSTR)sheet.GetName());
strcat(outfile, ".cxx");
//std::ofstream ar(outfile);
std::ofstream ar;
//ar << (LPCSTR)sheet.GetName() << std::endl;
//Create the SAFEARRAY from the VARIANT ret.
COleSafeArray sa(ret);
SaveAsTalk( ar, sa );
}
}
你在百度文库里找“使用MFC操作EXCEL文件”就可以找到资料。
_Application app; // app is an _Application object.
Workbooks books;
LPDISPATCH lpDisp; // Often reused variable.
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't CreateDispatch() for Excel");
return;
}
// Set visible.
app.SetVisible( FALSE );
lpDisp = app.GetWorkbooks(); // Get an IDispatch pointer.
ASSERT(lpDisp);
books.AttachDispatch(lpDisp); // Attach the IDispatch pointer
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
// To open an existing workbook, you need to provide all
// 13 arguments for the Open member function.
// The code below opens a workbook and adds it to the Workbook's
// Collection object.
// You need to modify the path and file name for your own
// workbook.
//
CString strFile = filename;
lpDisp = books.Open( strFile, // is a workbook.
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional ); // Return Workbook's IDispatch pointer.
ASSERT(lpDisp);
book.AttachDispatch( lpDisp );
// Get the Sheets collection and attach the IDispatch pointer to your
// sheets object.
lpDisp = book.GetSheets();
ASSERT(lpDisp);
sheets.AttachDispatch(lpDisp);
long nSheets = sheets.GetCount();
char outfile[256];
for (long no = 1; no <= nSheets; no++)
{
lpDisp = sheets.GetItem( COleVariant((short)( no )) );
ASSERT(lpDisp);
sheet.AttachDispatch(lpDisp);
lpDisp = sheet.GetUsedRange();
ASSERT(lpDisp);
range.AttachDispatch(lpDisp);
/*
Range r = range.GetColumns();
int cc = r.GetCount();
r = range.GetRows();
int rr = r.GetCount();
*/
VARIANT ret = range.GetValue();
if ( ret.vt == VT_EMPTY )
continue;
strcpy(outfile, filename);
char *p = strchr( outfile, '.' );
if ( p )
*p = 0;
CString str = outfile;
str += "_";
str += sheet.GetName();
if ( str.Find( "说明" ) == -1 &&
str.Find( "说明" ) == -1)
{
str_command[ 0 ].Add( str );
int sign = -1;
ftalk->write( (const char*)&sign, 4 );
int n = str_command[ 0 ].GetSize();
ftalk->write( (const char*)&n, 4 );
strcat(outfile, (LPCSTR)sheet.GetName());
strcat(outfile, ".cxx");
//std::ofstream ar(outfile);
std::ofstream ar;
//ar << (LPCSTR)sheet.GetName() << std::endl;
//Create the SAFEARRAY from the VARIANT ret.
COleSafeArray sa(ret);
SaveAsTalk( ar, sa );
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询