Home > Software engineering >  MFC insert images to excel
MFC insert images to excel

Time:09-26

Want to excel to insert images, has not been implemented, the total is that the
On the Internet to find a routine
 
Void CInsertImageDlg: : OnOpenexcel ()
{
//TODO: Add your the control notification handler code here
CFileDialog DLG (TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"All Files (*. XLS) | *. XLS | |", AfxGetMainWnd ());//structure file open dialog
Cstrings strPath;//declare variables
If (DLG) DoModal ()==IDOK)//determine whether "open" button
{
StrPath=DLG. GetPathName ();//get the file path
M_ePath. SetWindowText (strPath);//show the path to the file
}

}

Void CInsertImageDlg: : OnOpenimage ()
{
//TODO: Add your the control notification handler code here
CFileDialog DLG (TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"Image file | *. BMP; *.jpg; *. Jpeg | | ", AfxGetMainWnd ());//structure file open dialog
Cstrings strPath;//declare variables
If (DLG) DoModal ()==IDOK)//determine whether "open" button
{
StrPath=DLG. GetPathName ();//get image file path
M_iPath. SetWindowText (strPath);//show the path to the file
}

}

Void CInsertImageDlg: : OnButinsert ()
{
//TODO: Add your the control notification handler code here
Cstrings ePath, iPath;
M_ePath. GetWindowText (ePath);
M_iPath. GetWindowText (iPath);

_Application app;
Workbooks books;
_Workbook book;
Worksheets sheets;
_Worksheet sheet;
Shapes SHP;

//create Excel 2000 server (start Excel)
if (! App. CreateDispatch (" Excel Application ", NULL))
{
AfxMessageBox (" failed to create Excel services!" );
The exit (1);
}
Books. AttachDispatch (app. GetWorkbooks ());
Book. AttachDispatch (books. The Add (_variant_t (ePath)));
//get the Worksheets
Sheets. AttachDispatch (book. GetWorksheets ());
Sheet. AttachDispatch (sheets. The GetItem (_variant_t (" Sheet1 ")));
SHP. AttachDispatch (sheet. GetShapes ());
SHP. AddPicture (iPath, false, true, 0,0,400,300);

App. SetVisible (true);
//release object
Sheet. ReleaseDispatch ();
Sheets. ReleaseDispatch ();
Book. ReleaseDispatch ();
Books. ReleaseDispatch ();
App. ReleaseDispatch ();
}


is incorrect.
Here to ask you

CodePudding user response:

Tracking code
Found the following problems,
Here is not very understand, thank you which friend help resolve,

CodePudding user response:

Continue to follow up on the
after the position as shown in figure
InsertImage. Exe of 0 x7524c54f (KernelBase. DLL) the most possible exceptions: Microsoft c + + exceptions: memory location 0 x0037e864 COleDispatchException,

CodePudding user response:

Common function like this, look at the others comments can be run normally,
Don't know is what to do on the environment Settings?

CodePudding user response:

 
//Excel12. CPP: Defines the entry point for the console application.
//

# include "stdafx. H"
# include & lt; stdio.h>
# include & lt; Tchar. H>

# import "C:/Program Files/Microsoft Office/OFFICE12/the oledata.mso. DLL" \
Rename (" RGB ", "RBGMSO") rename (" SearchPath ", "SearchPathMSO") \
Rename (" DocumentProperties DocumentPropertiesMSO ", "") no_auto_exclude
# import "C:/Program Files/Microsoft Office/OFFICE12/VBE6EXT OLB" no_namespace
# import "C:/Program Files/Microsoft Office/OFFICE12/excel. Exe" \
Rename (" DialogBox ExcelDialogBox ", "") rename (" RGB", "ExcelRGB") \
Rename (" used by CopyFile ExcelCopyFile ", "") rename (" ReplaceText", "ExcelReplaceText") \
Rename (" IFont IFontXL ", "")

Void dump_com_error (_com_error & amp; E)
{
_tprintf (_T (" Oops - hit an error! \ n "));
_tprintf (_T (" \ \ a tCode=% 08 lx \ n "), e.E rror ());
_tprintf (_T (" \ \ a tCode fancy=% s \ n "), e.E rrorMessage ());
_bstr_t bstrSource (e.S ource ());
_bstr_t bstrDescription (e.D escription ());
_tprintf (_T (" \ \ a tSource=% s \ n "), (LPCTSTR) bstrSource);
_tprintf (_T (" \ \ a tDescription=% s \ n "), (LPCTSTR) bstrDescription);
}
Struct StartOle {
StartOle () {CoInitialize (NULL); }
~ StartOle () {CoUninitialize (); }
} _inst_StartOle;


Int main (int arg c, char * argv [])
{
Using the namespace Office;
Using the namespace Excel;

_ApplicationPtr pXL;

Try
{
PXL. CreateInstance (L "Excel. Application");

The pXL - & gt; PutVisible (0, VARIANT_TRUE);

WorkbooksPtr pBooks=pXL - & gt; Workbooks.
_WorkbookPtr pBook=pBooks - & gt; Add ((long) xlWorksheet);

_WorksheetPtr pSheet=pXL - & gt; ActiveSheet.

Try
{
//one will fail
PSheet - & gt; Name="Market Share?" ;
}
The catch (_com_error & amp; E)
{
Dump_com_error (e);
}

PSheet - & gt; Name="Market Share!" ;

Excel: : ShapesPtr pShapes=pSheet - & gt; Shapes;
RangePtr range=pSheet - & gt; Range [H1: O10 "];
Float l=range - & gt; Left, t=range - & gt; Top;
Float w=range - & gt; Width, h=range - & gt; Height;
Excel: : ShapePtr pShape=pShapes - & gt; AddPicture (_bstr_t (" C: \ \ tt. JPG "),
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related