如何读取ecxel?把程序copy到任何地方都能执行的!最好用ADO,ODBC好像还需要配置这个那个的,如果别的机器没配置就不能用了!
把程序copy到任何地方都能执行的!最好用ADO,ODBC好像还需要配置这个那个的,如果别的机器没配置就不能用了!
就读一下Excel的两列数据就可以了
没做过数据库方面的,麻烦兄弟把连接,读取,断开的代码给出来
100分酬谢
问题点数:100、回复次数:2Top
1 楼weakwater(我是河南人)回复于 2004-12-01 16:58:03 得分 0
就读当前目录下的一个excel文件就可以了Top
2 楼kingzai(stevenzhu)回复于 2004-12-01 17:21:47 得分 100
use excel automation interface
HOWTO: Automate Excel Using MFC and Worksheet Functions
Q178781
try
{
_Application app; // app is an _Application object.
_Workbook book; // More object declarations.
_Worksheet sheet;
Workbooks books;
Worksheets sheets;
Range range; // Used for Microsoft Excel 97 components.
LPDISPATCH lpDisp; // Often reused variable.
// Common OLE variants. Easy variants to use for calling arguments.
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
// Start Microsoft Excel, get _Application object,
// and attach to app object.
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't CreateDispatch() for Excel");
return;
}
// Set visible.
app.SetVisible(TRUE);
// Register the Analysis ToolPak.
CString sAppPath;
sAppPath.Format ("%s\\Analysis\\Analys32.xll", app.GetLibraryPath());
if(!app.RegisterXLL(sAppPath))
AfxMessageBox("Didn't register the Analys32.xll");
// Get the Workbooks collection.
lpDisp = app.GetWorkbooks(); // Get an IDispatch pointer.
ASSERT(lpDisp);
books.AttachDispatch(lpDisp); // Attach the IDispatch pointer
// to the books object.
// Open a new workbook and attach that IDispatch pointer to the
// Workbook object.
lpDisp = books.Add( covOptional );
ASSERT(lpDisp);
book.AttachDispatch( lpDisp );
// 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.
//
// lpDisp = books.Open("C:\\Test", // Test.xls is a workbook.
// covOptional, covOptional, covOptional, covOptional, covOptional,
// covOptional, covOptional, covOptional, covOptional, covOptional,
// covOptional, covOptional ); // Return Workbook's IDispatch
// pointer.
// Get the Sheets collection and attach the IDispatch pointer to your
// sheets object.
lpDisp = book.GetSheets();
ASSERT(lpDisp);
sheets.AttachDispatch(lpDisp);
// Get sheet #1 and attach the IDispatch pointer to your sheet
// object.
lpDisp = sheets.GetItem( COleVariant((short)(1)) );
//GetItem(const VARIANT &index)
ASSERT(lpDisp);
sheet.AttachDispatch(lpDisp);
...
Top




