Re: How to create an excel file using VC
On 16 Aug., 12:33, "Lee Tow" <fb...@pub3.fz.fj.cn> wrote:
Hello all:
I want to create an excel file,and write some data into i=
t using
vc,who could tell me how to create an excel file ,put into data and save
this file?Thanks.
This should get you started (it works with VC6.0):
// This include is generated automatically by the VC wizard, assuming
that you
// use pre-compiled headers.
#include "stdafx.h"
// Include of the windows header (not the original one "windows.h" but
the
// one that got a working-over from the MFC guys).
#include <afx.h>
// Include COM related stuff (smart pointers, COM definitions, ...)
#include "atlbase.h"
CComModule _Module;
#include "atlcom.h"
#include "atlimpl.cpp"
#include "atlctl.h"
#include "atlctl.cpp"
#include "comdef.h"
// In order to be able to include the type libraries from MS Excel we
have
// to undefine the ExitWindows macro from winuser.h since this macro
will
// clash with the ExitWindows method of the Tasks interface.
// See Q148223 at MSDN.
#undef ExitWindows
#undef DialogBox
#ifdef Range
# undef Range
#endif
// Basic types that are needed for all Office Automation libraries
// (the actual file name depends on
// version of Office that is installed, in my case it is Office 2003).
#import "C:\Program Files\Common Files\Microsoft Shared
\OFFICE11\MSO.DLL"
#import "C:\Program Files\Common Files\Microsoft Shared\VBA
\VBA6\VBE6EXT.OLB"
// Imports the types that are used for Excel (the actual file name
depends on
// version of Office that is installed, in my case it is Office 2003).
#import "C:\Program Files\Microsoft Office\Office11\xl5en32.olb"
int main(int argc, char* argv[])
{
// Since we want to use COM objects, we must initialize the COM sub-
system.
VERIFY (SUCCEEDED (CoInitialize (NULL)));
// Retrieve the CLSID of the MS Excel application.
CLSID clsidExcelApplication;
HRESULT hr = CLSIDFromProgID (L"Excel.Application",
&clsidExcelApplication);
_ASSERT(SUCCEEDED(hr));
// TODO: Proper error handling.
if (!SUCCEEDED(hr))
return 0;
// This flag indicates whether we will have to launch a new instance
of Excel. If
// we do so, we'll have to close it when we have finished.
bool bShouldCloseApplication = true;
// We introduce this code block, so that all smart pointers get
destroyed before
// this block is left. If we didn't do this, some of our smart
pointers would still
// be alive when we call CoUnitialize at the end of main, which
would cause memory
// access violations.
{
Excel::ApplicationPtr spExcelApplication;
try
{
// Try to open the running instance of Excel. If this fails,
// we try to launch a new instance of Excel
if (!(SUCCEEDED (spExcelApplication.GetActiveObject
(clsidExcelApplication))))
{
VERIFY (SUCCEEDED (spExcelApplication.CreateInstance
(clsidExcelApplication)));
if (!(bool) spExcelApplication)
throw _com_error (E_FAIL);
spExcelApplication->Visible = true;
// We have to create a new workbook.
Excel::WorkbooksPtr spWorksbooks = spExcelApplication-
Workbooks (vtMissing);
if (!(bool) spWorksbooks)
throw _com_error (E_FAIL);
spWorksbooks->Add ();
}
else
bShouldCloseApplication = false;
// Retrieve the currently active workbook and the currently
active
// worksheet from the Excel application.
Excel::WorkbookPtr spWorkbook = spExcelApplication-
GetActiveWorkbook ();
if (!(bool) spWorkbook)
throw _com_error (E_FAIL);
Excel::WorksheetPtr spWorksheet = spWorkbook->GetActiveSheet ();
if (!(bool) spWorksheet)
throw _com_error (E_FAIL);
// Put the text "Test" into Cell(A1).
Excel::RangePtr spRange = spWorksheet->Range ("A1");
ASSERT (SUCCEEDED (hr));
if (!(bool) spRange)
throw _com_error (E_FAIL);
spRange->Value = _variant_t("Test");
// Close the applications, if they are running.
if (bShouldCloseApplication)
spExcelApplication->Quit ();
}
catch (_com_error err)
{
MessageBox (NULL, err.ErrorMessage(), _T(""), MB_OK);
// Close the applications, if they are running.
if ((bool) spExcelApplication && bShouldCloseApplication)
spExcelApplication->Quit ();
}
} // At this point all our smart pointers will go out of scope, thus
// properly releasing any references to the objects.
CoUninitialize();
printf ("Finished!\n");
return 0;
}