Tips SistemThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
Artikel ini menjelaskan bagaimana meng-embed Microsoft Excel
Lembar kerja ke objek pandangan SDI MFC aplikasi.
Artikel ini
termasuk petunjuk langkah demi langkah untuk melekatkan lembar kerja dan menambahkan beberapa
teks ke sel A1, serta komentar yang menjelaskan setiap langkah.
Meskipun
kode contoh dalam artikel ini dapat diambil dan dimasukkan ke dalam aplikasi Anda,
manfaat nyata berasal dari membaca dan memahami sampel kode.
Microsoft menyediakan contoh pemrograman hanya sebagai ilustrasi, tanpa jaminan apa pun baik tersurat maupun tersirat. Termasuk, namun tidak terbatas pada, jaminan tersirat mengenai kelayakan untuk diperdagangkan atau kesesuaian untuk keperluan tertentu. Artikel ini mengasumsikan bahwa Anda telah terbiasa dengan bahasa pemrograman yang ditunjukkan dan dengan alat yang digunakan untuk membuat dan mendebug prosedur. Insinyur dukungan Microsoft dapat membantu menjelaskan fungsionalitas prosedur tertentu. Namun, mereka tidak akan memodifikasi contoh ini untuk memberikan fungsionalitas tambahan atau menyusun prosedur untuk memenuhi persyaratan spesifik Anda.
Untuk membuat aplikasi MFC, ikuti langkah berikut:
Gunakan AppWizard untuk membuat baru MFC AppWizard (EXE)
proyek yang dinamai "Embed_Excel."
Pilih dokumen tunggal sebagai jenis aplikasi untuk
Buat, dan Pilih kontainer sebagai jenis senyawa dokumen dukungan untuk
termasuk. Menerima semua pengaturan default.
Kelas berikut
dihasilkan:
Aplikasi: CEmbed_ExcelApp di Embed_Excel.h dan Embed_Excel.cpp
Bingkai: CMainFrame di MainFrm.h dan MainFrm.cpp
Dokumen: CEmbed_ExcelDoc di Embed_ExcelDoc.h dan
Embed_ExcelDoc.CPP
Lihat: CEmbed_ExcelView di Embed_ExcelView.h dan
Embed_ExcelView.CPP
Wadah Item: CEmbed_ExcelCntrItem di CntrItem.h dan CntrItem.cpp
Pada Lihat menu, klik ClassWizard. Klik Otomatisasi tab, klik Menambahkan kelas, dan memilih Dari perpustakaan jenis. Cari Microsoft Excel Perpustakaan jenis, dan menambahkan semua kelas di perpustakaan jenis untuk proyek Anda. Untuk
Excel 97, perpustakaan jenis terletak di Excel8.olb. Untuk Excel 2000 jenis
Perpustakaan ini terletak di Excel9.olb, dan untuk Excel 2002 dan kemudian, perpustakaan jenis ini
terletak di Excel.exe.
Tambahkan baris berikut ke CntrItem.h:
LPDISPATCH GetIDispatch();
Kemudian tambahkan metode GetIDispatch untuk CntrItem.cpp:
Sample Code
-----------
/*******************************************************************
* This method returns the IDispatch* for the application linked to
* this container.
********************************************************************/
LPDISPATCH CEmbed_ExcelCntrItem::GetIDispatch()
{
//The this and m_lpObject pointers must be valid for this function
//to work correctly. The m_lpObject is the IUnknown pointer to
// this object.
ASSERT_VALID(this);
ASSERT(m_lpObject != NULL);
LPUNKNOWN lpUnk = m_lpObject;
//The embedded application must be running in order for the rest
//of the function to work.
Run();
//QI for the IOleLink interface of m_lpObject.
LPOLELINK lpOleLink = NULL;
if (m_lpObject->QueryInterface(IID_IOleLink,
(LPVOID FAR*)&lpOleLink) == NOERROR)
{
ASSERT(lpOleLink != NULL);
lpUnk = NULL;
//Retrieve the IUnknown interface to the linked application.
if (lpOleLink->GetBoundSource(&lpUnk) != NOERROR)
{
TRACE0("Warning: Link is not connected!\n");
lpOleLink->Release();
return NULL;
}
ASSERT(lpUnk != NULL);
}
//QI for the IDispatch interface of the linked application.
LPDISPATCH lpDispatch = NULL;
if (lpUnk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch)
!=NOERROR)
{
TRACE0("Warning: does not support IDispatch!\n");
return NULL;
}
//After assuring ourselves it is valid, return the IDispatch
//interface to the caller.
ASSERT(lpDispatch != NULL);
return lpDispatch;
}
Tambahkan baris berikut ke Embed_ExcelView.h:
void EmbedAutomateExcel();
Kemudian tambahkan EmbedAutomateExcel metode untuk
Embed_ExcelView.CPP:
Sample Code
-----------
/********************************************************************
* This method encapsulates the process of embedding an Excel
* Worksheet in a View object and automating that worksheet to add
* some text to cell A1.
********************************************************************/
void CEmbed_ExcelView::EmbedAutomateExcel()
{
//Change the cursor so the user knows something exciting is going
//on.
BeginWaitCursor();
CEmbed_ExcelCntrItem* pItem = NULL;
TRY
{
//Get the document associated with this view, and be sure it's
//valid.
CEmbed_ExcelDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//Create a new item associated with this document, and be sure
//it's valid.
pItem = new CEmbed_ExcelCntrItem(pDoc);
ASSERT_VALID(pItem);
// Get Class ID for Excel sheet.
// This is used in creation.
CLSID clsid;
if(FAILED(::CLSIDFromProgID(L"Excel.sheet",&clsid)))
//Any exception will do. We just need to break out of the
//TRY statement.
AfxThrowMemoryException();
// Create the Excel embedded item.
if(!pItem->CreateNewItem(clsid))
//Any exception will do. We just need to break out of the
//TRY statement.
AfxThrowMemoryException();
//Make sure the new CContainerItem is valid.
ASSERT_VALID(pItem);
// Launch the server to edit the item.
pItem->DoVerb(OLEIVERB_SHOW, this);
// As an arbitrary user interface design, this sets the
// selection to the last item inserted.
m_pSelection = pItem; // set selection to last inserted item
pDoc->UpdateAllViews(NULL);
//Query for the dispatch pointer for the embedded object. In
//this case, this is the Excel worksheet.
LPDISPATCH lpDisp;
lpDisp = pItem->GetIDispatch();
//Add text in cell A1 of the embedded Excel sheet
_Workbook wb;
Worksheets wsSet;
_Worksheet ws;
Range range;
_Application app;
//set _Workbook wb to use lpDisp, the IDispatch* of the
//actual workbook.
wb.AttachDispatch(lpDisp);
//Then get the worksheet's application.
app = wb.GetApplication();
//Then get the first worksheet in the workbook
wsSet = wb.GetWorksheets();
ws = wsSet.GetItem(COleVariant((short)1));
//From there, get a Range object corresponding to cell A1.
range = ws.GetRange(COleVariant("A1"), COleVariant("A1"));
//Fill A1 with the string "Hello, World!"
range.SetValue(COleVariant("Hello, World!"));
//NOTE: If you are automating Excel 2002, the Range.SetValue method has an
//additional optional parameter specifying the data type. Because the
//parameter is optional, existing code will still work correctly, but new
//code should use the new convention. The call for Excel2002 should look
//like the following:
//range.SetValue( C<?xm-insertion_mark_start author="v-thomr" time="20070326T121607-0600"?>O<?xm-insertion_mark_end?><?xm-deletion_mark author="v-thomr" time="20070326T121606-0600" data="o"?>leVariant( (long)DISP_E_PARAMNOTFOUND, VT_ERROR ),
// COleVariant("Hello, World!"));
}
//Here, we need to do clean up if something went wrong.
CATCH(CException, e)
{
if (pItem != NULL)
{
ASSERT_VALID(pItem);
pItem->Delete();
}
AfxMessageBox(IDP_FAILED_TO_CREATE);
}
END_CATCH
//Set the cursor back to normal so the user knows exciting stuff
//is no longer happening.
EndWaitCursor();
}
Tambahkan baris berikut ke Embed_ExcelView.h:
#include "excel8.h"
NOte Apabila Anda sedang mengotomatisasi Excel 2000, header file adalah "excel9.h."
Apabila Anda sedang mengotomatisasi Excel 2002 atau versi yang lebih baru dari Excel, header file adalah "excel.h."
Lihat metode OnInsertObject() kelas lihat. Itu
menarik untuk dicatat bahwa metode ini, dan metode yang kita baru saja ditulis, adalah
sangat mirip. Pada kenyataannya, kode yang kita tulis adalah hanya kasus khusus dari
OnInsertObject(), yang memungkinkan pengguna untuk memilih dari daftar tersedia OLE
objek untuk memasukkan ke dalam aplikasi. Karena kami hanya ingin mengotomatisasi
Lembar kerja Excel, kita mengabaikan perilaku ini. Untuk aplikasi kita, menghapus semua
kode dari interior InsertObject() dan menggantinya dengan panggilan untuk
EmbedAutomateExcel().
Mengkompilasi dan menjalankan aplikasi.
Pada Mengedit menu, klik Menyisipkan objek baru.
Hasil: Lembar kerja Microsoft Excel tertanam ke dalam pandangan. Selain itu, sel A1 diisi dengan
"Hello, World!" melalui otomatisasi.
PENTING: Artikel ini diterjemahkan menggunakan perangkat lunak mesin penerjemah Microsoft dan bukan oleh seorang penerjemah. Microsoft menawarkan artikel yang diterjemahkan oleh seorang penerjemah maupun artikel yang diterjemahkan menggunakan mesin sehingga Anda akan memiliki akses ke seluruh artikel baru yang diterbitkan di Pangkalan Pengetahuan (Knowledge Base) dalam bahasa yang Anda gunakan. Namun, artikel yang diterjemahkan menggunakan mesin tidak selalu sempurna. Artikel tersebut mungkin memiliki kesalahan kosa kata, sintaksis, atau tata bahasa, hampir sama seperti orang asing yang berbicara dalam bahasa Anda. Microsoft tidak bertanggung jawab terhadap akurasi, kesalahan atau kerusakan yang disebabkan karena kesalahan penerjemahan konten atau penggunaannya oleh para pelanggan. Microsoft juga sering memperbarui perangkat lunak mesin penerjemah.
Klik disini untuk melihat versi Inggris dari artikel ini:184663
(http://support.microsoft.com/kb/184663/en-us/
)
Seberapa besar upaya Anda untuk menggunakan artikel ini?
Sangat sedikit
Sedikit
Sedang
Besar
Sangat besar
Berikan saran tentang apa yang dapat kami lakukan untuk menyempurnakan informasi ini
Terima kasih! Masukan Anda akan digunakan untuk membantu kami meningkatkan konten dukungan. Untuk opsi bantuan lainnya, kunjungi Halaman Beranda Bantuan dan Dukungan.