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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

export data to excel?

Status
Not open for further replies.

jayjay60

Programmer
Jun 19, 2001
97
FR
Hi everybody,

I would like to export data from a safearray to range in excel. So, ii've read the articles Q186120 which gives an explantion and an example to do it. I've tried to test it in my application, first i ve done simply in trying to put in a specific set of range the figure "1". But when i test it it doesn't work the only first cell ("A1") is filled by "1", WHY??? So, you could find the code i've wrote, so, if someone could help me!

Thanks in advance
.
.
.
.
_Application FracApp;
_Workbook FracBook;
_Worksheet FracSheet;
Workbooks FracBooks;
Worksheets FracSheets;
Range FracRange;
LPDISPATCH lpDisp;
COleVariant Optional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
COleSafeArray FracArray;
DWORD NumOfElements[2];
int NumberOfRows,NumberOfCols; //pour le "SAFEARRAY"
long FracIndex[2];
long lRow,lCol;
double Temporary;

if(!FracApp.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Pb avec CreateDispatch sur Excel");
return;
}
lpDisp=FracApp.GetWorkbooks();
ASSERT(lpDisp);
FracBooks.AttachDispatch(lpDisp);
lpDisp=FracBooks.Open("D:\\FractaLab\\Fractal_v1_0\\Test.xls",Optional,Optional,Optional,Optional,Optional,Optional,Optional,Optional,Optional,Optional,Optional,Optional);
ASSERT(lpDisp);
FracBook.AttachDispatch(lpDisp);
lpDisp=FracBook.GetWorksheets();
ASSERT(lpDisp);
FracSheets.AttachDispatch(lpDisp);
lpDisp=FracSheets.GetItem(COleVariant((short)1));
ASSERT(lpDisp);
FracSheet.AttachDispatch(lpDisp);
lpDisp=FracSheet.GetRange(COleVariant("A1"),COleVariant("A1"));
ASSERT(lpDisp);
FracRange.AttachDispatch(lpDisp);

NumberOfRows=m_dlgNumHisto+1-m_dlgNumSample;
NumberOfCols=256;
FracRange.GetResize(COleVariant((short)(NumberOfRows)),COleVariant((short)(NumberOfCols)));
FracApp.SetVisible(TRUE);
FracRange.Clear();

//Création du tab de type SAFEARRAY.
NumOfElements[0]=NumberOfRows; NumOfElements[1]=NumberOfCols;
FracArray.Create(VT_R8,2,NumOfElements);

//Remplissage du SAFEARRAY
for(lCol=0;lCol {
for(lRow=0;lRow {
FracIndex[0]=lRow;
FracIndex[1]=lCol; Temporary=(double)1;
FracArray.PutElement(FracIndex,&Temporary);
}
}
FracRange.SetValue(COleVariant(FracArray));
FracArray.Detach();
FracApp.SetUserControl(TRUE);
.
.
.

jayjay
 
I'm no seasoned programmer but this is how I would do it in a simple form. Perhaps you want to get away from this.

take your array cycle through using a combination of 2 for statement.

check each value for NULL or 0 status (depending on if it is a char or int array)

if 0 set the value to whatever you would want to show up in the spread sheet (a null char isn't a good thing and you'll see why in a second)

print the value to a TAB text format or a comma separated format(CSF).

The code for this section would be about 15 lines and would get all the data you wanted from a 2d array into excel.

If you need help I'll post the code for you.

Good luck, =====================
Insider
4 year 'on the fly' programmer
C++ Basic Java
 
Hi jayjay60
I don't think there was any need to use SAFEARRAY in that .
Only to read or write data to/from Execl it is nowhere required if you/client have MS Excel.MS Excel library itself gives you pointers for this purpose. Refer msdn.microsoft.com to know more of it. If it is going teadious to you to understand, you can let me know.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top