先决条件
- 本机安装office2003、2007、2010、2016及以后版本,总之必须安装office
- 导入Excel库文件,导入方式可参考:
- 如何在vs2017及以前版本(vs2010、vs2015)上添加 添加类型库中的MFC类
- 如何在vs2019及以后版本(如vs2022)上添加 添加ActiveX控件中的MFC类
- 如何使用vs2022通过excel.exe生成VC、C++能够使用的头文件
一、导入头文件
#include "CApplication.h"
#include "CWorkbook.h"
#include "CWorkbooks.h"
#include "CWorksheet.h"
#include "CWorksheets.h"
#include "CRange.h"
#include "CFont0.h"
二、使用示例1
1、打开excel驱动及操作excel (读、写、新建sheet,选择rang,行、列等)
int nSheetNumber=1;
CApplication app;
if( !app.CreateDispatch(_T("Excel.Application")))
{
AfxMessageBox("没有安装office2007以上版本,无法操作excel文件");
return ;
}
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CFont0 font;
CRange range;
CString FieldName,FieldValue;
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
books=app.get_Workbooks();
book = books.Add(covOptional);
book.put_Title("Mybooks");
sheets=book.get_Sheets();
sheet=sheets.get_Item(COleVariant((short)nSheetNumber));
sheet.put_Name("wxpSheet");
app.put_Visible(TRUE);
app.put_UserControl(TRUE);
FieldName="a1";
FieldValue="数据A";
range=sheet.get_Range(COleVariant(FieldName),COleVariant(FieldName));
range.put_Value2(COleVariant(FieldValue));
FieldName="B1";
FieldValue="数据B";
range=sheet.get_Range(COleVariant(FieldName),COleVariant(FieldName));
range.put_Value2(COleVariant(FieldValue));
for(int row=1;row<10;++row)
{
for(int col=0;col<10;++col)
{
FieldName.Format("%c%d",'a'+col,(row+1));
FieldValue.Format("(%d,%d)",row+1,col+1);
range=sheet.get_Range(COleVariant(FieldName),COleVariant(FieldName));
range.put_Formula(_variant_t("=RAND()*100"));
range.put_NumberFormat(_variant_t("00.00"));
if(row %2==0)
{
font=range.get_Font();
font.put_Bold(COleVariant((short)TRUE));
font.put_Color(COleVariant((long)RGB(255, 0, 0)));
}
}
}
FieldName="a11";
range=sheet.get_Range(COleVariant(FieldName),COleVariant(FieldName));
range.put_Formula(COleVariant(_T("=sum(a2:a10")));
CRange cols;
cols=range.get_EntireColumn();
cols.AutoFit();
FieldName="a11";
range = sheet.get_Range(COleVariant(FieldName),COleVariant(FieldName));
COleVariant cov = range.get_Value2();
CString str="success";
cov.ChangeType(VT_BSTR);
str=cov.bstrVal;
AfxMessageBox(str);
books.Close();
app.Quit();
books.ReleaseDispatch();
app.ReleaseDispatch();
2、结果
三、使用示例2
1、打开指定excel,设置对齐方式,设置字体颜色,合并单元格
CApplication app;
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
CFont0 font;
CRange cols;
CRange iCell;
LPDISPATCH lpDisp;
COleVariant vResult;
COleVariant covTrue((short)TRUE);
COleVariant covFalse((short)FALSE);
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if (!app.CreateDispatch("Excel.Application"))
{
this->MessageBox("无法创建Excel应用!");
return;
}
app.put_Visible(FALSE);
app.put_UserControl(TRUE);
books.AttachDispatch(app.get_Workbooks());
lpDisp = books.Open("C:\\test\\excel\\test.xlsx",
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional);
book.AttachDispatch(lpDisp);
sheets.AttachDispatch(book.get_Worksheets());
lpDisp = book.get_ActiveSheet();
sheet.AttachDispatch(lpDisp);
CRange usedRange;
usedRange.AttachDispatch(sheet.get_UsedRange());
range.AttachDispatch(usedRange.get_Rows());
long iRowNum = range.get_Count();
range.AttachDispatch(usedRange.get_Columns());
long iColNum = range.get_Count();
long iStartRow = usedRange.get_Row();
long iStartCol = usedRange.get_Column();
range.AttachDispatch(sheet.get_Cells());
range.AttachDispatch(range.get_Item(COleVariant((long)1), COleVariant((long)1)).pdispVal);
vResult = range.get_Value2();
CString str;
if (vResult.vt == VT_BSTR)
{
str = vResult.bstrVal;
}
else if (vResult.vt == VT_R8)
{
str.Format("%f", vResult.dblVal);
}
else if (vResult.vt == VT_DATE)
{
SYSTEMTIME st;
VariantTimeToSystemTime((long)&vResult.date, &st);
}
else if (vResult.vt == VT_EMPTY)
{
str = "";
}
range.AttachDispatch(sheet.get_Cells());
iCell.AttachDispatch((range.get_Item(COleVariant(long(1)), COleVariant(long(1)))).pdispVal);
vResult.lVal = 0;
vResult = iCell.get_HorizontalAlignment();
if (vResult.lVal != 0)
{
switch (vResult.lVal)
{
case 1:
break;
case -4108:
break;
case -4131:
break;
case -4152:
break;
}
}
iCell.AttachDispatch((range.get_Item(COleVariant(long(1)), COleVariant(long(1)))).pdispVal);
vResult.lVal = 0;
vResult = iCell.get_VerticalAlignment();
if (vResult.lVal != 0)
{
switch (vResult.lVal)
{
case -4160:
break;
case -4108:
break;
case -4107:
break;
}
}
range.AttachDispatch(sheet.get_Cells());
range.AttachDispatch((range.get_Item(COleVariant(long(1)), COleVariant(long(1)))).pdispVal);
font.AttachDispatch(range.get_Font());
font.put_Color(COleVariant((long)0xFF0000));
CRange unionRange;
range.AttachDispatch(sheet.get_Cells());
unionRange.AttachDispatch(range.get_Item(COleVariant((long)1), COleVariant((long)1)).pdispVal);
vResult = unionRange.get_MergeCells();
if (vResult.boolVal == -1)
{
range.AttachDispatch(unionRange.get_Rows());
long iUnionRowNum = range.get_Count();
range.AttachDispatch(unionRange.get_Columns());
long iUnionColumnNum = range.get_Count();
long iUnionStartRow = unionRange.get_Row();
long iUnionStartCol = unionRange.get_Column();
}
else if (vResult.boolVal == 0)
{
}
range.AttachDispatch(sheet.get_Cells());
unionRange.AttachDispatch(range.get_Item(COleVariant((long)1), COleVariant((long)1)).pdispVal);
unionRange.AttachDispatch(unionRange.get_Resize(COleVariant((long)3), COleVariant((long)2)));
unionRange.Merge(COleVariant((long)0));
app.put_DisplayAlerts(FALSE);
book.SaveAs(COleVariant("C:\\test\\excel\\test1.xlsx"), covOptional, covOptional,
covOptional, covOptional, covOptional, 0,
covOptional, covOptional, covOptional, covOptional, covOptional, covOptional);
book.Close(covOptional, COleVariant("C:\\test\\excel\\test.xlsx"), covOptional);
books.Close();
range.ReleaseDispatch();
cols.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.Quit();
app.ReleaseDispatch();
2、结果
更多使用方式,可以参考VC 设置EXCEL单元格的格式