5 个解决方案
#1
Steps:
1. HOWTO: Create an Automation Project Using MFC and a Type Library
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q178749
2. HOWTO: Use MFC to Automate Excel and Fill a Range with an Array (Q186120)
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q186120
REFERENCES
For more information about automating Microsoft Excel using MFC, please see the following articles in the Microsoft Knowledge Base:
Q186122 HOWTO: Use MFC to Automate Excel and Obtain an Array from a Range
Q184663 HOWTO: Embed and Automate a Microsoft Excel Worksheet with MFC
Q179706 Use MFC to Automate Excel and Create/Format a New Workbook
Q178781 HOWTO: Automate Excel Using MFC and Worksheet Functions
Q178783 HOWTO: Use MFC to Create a Microsoft Excel Chart
1. HOWTO: Create an Automation Project Using MFC and a Type Library
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q178749
2. HOWTO: Use MFC to Automate Excel and Fill a Range with an Array (Q186120)
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q186120
REFERENCES
For more information about automating Microsoft Excel using MFC, please see the following articles in the Microsoft Knowledge Base:
Q186122 HOWTO: Use MFC to Automate Excel and Obtain an Array from a Range
Q184663 HOWTO: Embed and Automate a Microsoft Excel Worksheet with MFC
Q179706 Use MFC to Automate Excel and Create/Format a New Workbook
Q178781 HOWTO: Automate Excel Using MFC and Worksheet Functions
Q178783 HOWTO: Use MFC to Create a Microsoft Excel Chart
#2
Sample Code
// OLE Variant for Optional.
COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
_Application objApp;
_Workbook objBook;
Workbooks objBooks;
Worksheets objSheets;
_Worksheet objSheet;
Range range;
if(!UpdateData(TRUE))
{
return;
}
// Instantiate Excel and start a new workbook.
objApp.CreateDispatch("Excel.Application");
objBooks = objApp.GetWorkbooks();
objBook = objBooks.Add(VOptional);
objSheets = objBook.GetWorksheets();
objSheet = objSheets.GetItem(COleVariant((short)1));
//Get the range where the starting cell has the address
//m_sStartingCell and it's dimensions are m_iNumRows x m_iNumCols.
range = objSheet.GetRange(COleVariant(m_sStartingCell),
COleVariant(m_sStartingCell));
range = range.GetResize(COleVariant(m_iNumRows),
COleVariant(m_iNumCols));
//*** Fill the range with an array of values.
//Create the SAFEARRAY.
COleSafeArray saRet;
DWORD numElements[2];
numElements[0]= m_iNumRows; //Number of rows in the range.
numElements[1]= m_iNumCols; //Number of columns in the range.
if(m_bFillWithStrings)
{
saRet.Create(VT_BSTR, 2, numElements);
}
else
{
saRet.Create(VT_R8, 2, numElements);
}
//Fill the SAFEARRAY.
long index[2];
long iRow;
long iCol;
for(iRow=0;iRow<=m_iNumRows-1;iRow++)
{
for(iCol=0;iCol<=m_iNumCols-1;iCol++)
{
index[0] = iRow;
index[1] = iCol;
if(m_bFillWithStrings) //Fill with Strings.
{
VARIANT v;
CString s;
VariantInit(&v);
v.vt = VT_BSTR;
s.Format("r%dc%d", iRow, iCol);
v.bstrVal = s.AllocSysString();
saRet.PutElement(index, v.bstrVal);
SysFreeString(v.bstrVal);
VariantClear(&v);
}
else //Fill with Numbers.
{
double d;
d = (iRow*1000) + iCol;
saRet.PutElement(index, &d);
}
}
}
//Set the range value to the SAFEARRAY.
range.SetValue(COleVariant(saRet));
saRet.Detach();
//Return control of Excel to the user.
objApp.SetVisible(TRUE);
objApp.SetUserControl(TRUE);
Compile and Run the project.
// OLE Variant for Optional.
COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
_Application objApp;
_Workbook objBook;
Workbooks objBooks;
Worksheets objSheets;
_Worksheet objSheet;
Range range;
if(!UpdateData(TRUE))
{
return;
}
// Instantiate Excel and start a new workbook.
objApp.CreateDispatch("Excel.Application");
objBooks = objApp.GetWorkbooks();
objBook = objBooks.Add(VOptional);
objSheets = objBook.GetWorksheets();
objSheet = objSheets.GetItem(COleVariant((short)1));
//Get the range where the starting cell has the address
//m_sStartingCell and it's dimensions are m_iNumRows x m_iNumCols.
range = objSheet.GetRange(COleVariant(m_sStartingCell),
COleVariant(m_sStartingCell));
range = range.GetResize(COleVariant(m_iNumRows),
COleVariant(m_iNumCols));
//*** Fill the range with an array of values.
//Create the SAFEARRAY.
COleSafeArray saRet;
DWORD numElements[2];
numElements[0]= m_iNumRows; //Number of rows in the range.
numElements[1]= m_iNumCols; //Number of columns in the range.
if(m_bFillWithStrings)
{
saRet.Create(VT_BSTR, 2, numElements);
}
else
{
saRet.Create(VT_R8, 2, numElements);
}
//Fill the SAFEARRAY.
long index[2];
long iRow;
long iCol;
for(iRow=0;iRow<=m_iNumRows-1;iRow++)
{
for(iCol=0;iCol<=m_iNumCols-1;iCol++)
{
index[0] = iRow;
index[1] = iCol;
if(m_bFillWithStrings) //Fill with Strings.
{
VARIANT v;
CString s;
VariantInit(&v);
v.vt = VT_BSTR;
s.Format("r%dc%d", iRow, iCol);
v.bstrVal = s.AllocSysString();
saRet.PutElement(index, v.bstrVal);
SysFreeString(v.bstrVal);
VariantClear(&v);
}
else //Fill with Numbers.
{
double d;
d = (iRow*1000) + iCol;
saRet.PutElement(index, &d);
}
}
}
//Set the range value to the SAFEARRAY.
range.SetValue(COleVariant(saRet));
saRet.Detach();
//Return control of Excel to the user.
objApp.SetVisible(TRUE);
objApp.SetUserControl(TRUE);
Compile and Run the project.
#3
请问上面的程序要如何才能通过编译呢?
#4
http://www.vckbase.com/document/finddoc.asp
#5
有例子程序,都可以编译成功
#1
Steps:
1. HOWTO: Create an Automation Project Using MFC and a Type Library
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q178749
2. HOWTO: Use MFC to Automate Excel and Fill a Range with an Array (Q186120)
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q186120
REFERENCES
For more information about automating Microsoft Excel using MFC, please see the following articles in the Microsoft Knowledge Base:
Q186122 HOWTO: Use MFC to Automate Excel and Obtain an Array from a Range
Q184663 HOWTO: Embed and Automate a Microsoft Excel Worksheet with MFC
Q179706 Use MFC to Automate Excel and Create/Format a New Workbook
Q178781 HOWTO: Automate Excel Using MFC and Worksheet Functions
Q178783 HOWTO: Use MFC to Create a Microsoft Excel Chart
1. HOWTO: Create an Automation Project Using MFC and a Type Library
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q178749
2. HOWTO: Use MFC to Automate Excel and Fill a Range with an Array (Q186120)
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q186120
REFERENCES
For more information about automating Microsoft Excel using MFC, please see the following articles in the Microsoft Knowledge Base:
Q186122 HOWTO: Use MFC to Automate Excel and Obtain an Array from a Range
Q184663 HOWTO: Embed and Automate a Microsoft Excel Worksheet with MFC
Q179706 Use MFC to Automate Excel and Create/Format a New Workbook
Q178781 HOWTO: Automate Excel Using MFC and Worksheet Functions
Q178783 HOWTO: Use MFC to Create a Microsoft Excel Chart
#2
Sample Code
// OLE Variant for Optional.
COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
_Application objApp;
_Workbook objBook;
Workbooks objBooks;
Worksheets objSheets;
_Worksheet objSheet;
Range range;
if(!UpdateData(TRUE))
{
return;
}
// Instantiate Excel and start a new workbook.
objApp.CreateDispatch("Excel.Application");
objBooks = objApp.GetWorkbooks();
objBook = objBooks.Add(VOptional);
objSheets = objBook.GetWorksheets();
objSheet = objSheets.GetItem(COleVariant((short)1));
//Get the range where the starting cell has the address
//m_sStartingCell and it's dimensions are m_iNumRows x m_iNumCols.
range = objSheet.GetRange(COleVariant(m_sStartingCell),
COleVariant(m_sStartingCell));
range = range.GetResize(COleVariant(m_iNumRows),
COleVariant(m_iNumCols));
//*** Fill the range with an array of values.
//Create the SAFEARRAY.
COleSafeArray saRet;
DWORD numElements[2];
numElements[0]= m_iNumRows; //Number of rows in the range.
numElements[1]= m_iNumCols; //Number of columns in the range.
if(m_bFillWithStrings)
{
saRet.Create(VT_BSTR, 2, numElements);
}
else
{
saRet.Create(VT_R8, 2, numElements);
}
//Fill the SAFEARRAY.
long index[2];
long iRow;
long iCol;
for(iRow=0;iRow<=m_iNumRows-1;iRow++)
{
for(iCol=0;iCol<=m_iNumCols-1;iCol++)
{
index[0] = iRow;
index[1] = iCol;
if(m_bFillWithStrings) //Fill with Strings.
{
VARIANT v;
CString s;
VariantInit(&v);
v.vt = VT_BSTR;
s.Format("r%dc%d", iRow, iCol);
v.bstrVal = s.AllocSysString();
saRet.PutElement(index, v.bstrVal);
SysFreeString(v.bstrVal);
VariantClear(&v);
}
else //Fill with Numbers.
{
double d;
d = (iRow*1000) + iCol;
saRet.PutElement(index, &d);
}
}
}
//Set the range value to the SAFEARRAY.
range.SetValue(COleVariant(saRet));
saRet.Detach();
//Return control of Excel to the user.
objApp.SetVisible(TRUE);
objApp.SetUserControl(TRUE);
Compile and Run the project.
// OLE Variant for Optional.
COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
_Application objApp;
_Workbook objBook;
Workbooks objBooks;
Worksheets objSheets;
_Worksheet objSheet;
Range range;
if(!UpdateData(TRUE))
{
return;
}
// Instantiate Excel and start a new workbook.
objApp.CreateDispatch("Excel.Application");
objBooks = objApp.GetWorkbooks();
objBook = objBooks.Add(VOptional);
objSheets = objBook.GetWorksheets();
objSheet = objSheets.GetItem(COleVariant((short)1));
//Get the range where the starting cell has the address
//m_sStartingCell and it's dimensions are m_iNumRows x m_iNumCols.
range = objSheet.GetRange(COleVariant(m_sStartingCell),
COleVariant(m_sStartingCell));
range = range.GetResize(COleVariant(m_iNumRows),
COleVariant(m_iNumCols));
//*** Fill the range with an array of values.
//Create the SAFEARRAY.
COleSafeArray saRet;
DWORD numElements[2];
numElements[0]= m_iNumRows; //Number of rows in the range.
numElements[1]= m_iNumCols; //Number of columns in the range.
if(m_bFillWithStrings)
{
saRet.Create(VT_BSTR, 2, numElements);
}
else
{
saRet.Create(VT_R8, 2, numElements);
}
//Fill the SAFEARRAY.
long index[2];
long iRow;
long iCol;
for(iRow=0;iRow<=m_iNumRows-1;iRow++)
{
for(iCol=0;iCol<=m_iNumCols-1;iCol++)
{
index[0] = iRow;
index[1] = iCol;
if(m_bFillWithStrings) //Fill with Strings.
{
VARIANT v;
CString s;
VariantInit(&v);
v.vt = VT_BSTR;
s.Format("r%dc%d", iRow, iCol);
v.bstrVal = s.AllocSysString();
saRet.PutElement(index, v.bstrVal);
SysFreeString(v.bstrVal);
VariantClear(&v);
}
else //Fill with Numbers.
{
double d;
d = (iRow*1000) + iCol;
saRet.PutElement(index, &d);
}
}
}
//Set the range value to the SAFEARRAY.
range.SetValue(COleVariant(saRet));
saRet.Detach();
//Return control of Excel to the user.
objApp.SetVisible(TRUE);
objApp.SetUserControl(TRUE);
Compile and Run the project.
#3
请问上面的程序要如何才能通过编译呢?
#4
http://www.vckbase.com/document/finddoc.asp
#5
有例子程序,都可以编译成功