Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dantez

Status
Not open for further replies.

dantez

Programmer
Nov 19, 2000
3
0
0
LV
How can I read Excel (XLS) files in Visual C is some modules whos work with them?
 
Well, I've been trying to read a range of cells, using the function GetRange, and then using GetValue.
First a use GetRange with only one parameter, the cell that I want to get.
Example
_Application m_ExcelOn;


COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR);
if (!m_bExcelStarted)
if (m_ExcelOn.CreateDispatch("Excel.Application"))
m_bExcelStarted=true;

Workbooks oBooks = m_ExcelOn.GetWorkbooks();
_Workbook oBook = oBooks.Add(vOpt);
Worksheets oSheets = oBook.GetWorksheets();
_Worksheet oSheet = oSheets.GetItem(COleVariant((short)1));
VARIANT l_cell;
COleVariant l_cell1;
CString var;
oBooks.Open("FileName.xls",vOpt,COleVariant((short)true),vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
Range oRange;

oRange = m_ExcelOn.GetRange(COleVariant("A1"),vOpt);
oRange = oSheet.GetUsedRange();
l_cell=oRange.GetValue();
l_cell1.ChangeType(VT_BSTR,(COleVariant *)&l_cell);
var = l_cell1.bstrVal;
AfxMessageBox(var, MB_OK | MB_ICONINFORMATION);

This procedure has a disadvantage, you only can read one cell.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top