添加类 TypeLib中的MFC类,
总共7个类
CApplication、CFont0、CRange、CWorkbook、CWorkbooks、CWorksheet、CWorksheets
进入刚添加进来的几个类头文件中将#import开头的这句注掉
// #import "C:\\Program Files (x86)\\Microsoft Office\\Office12\\EXCEL.EXE" no_namespace
将修改过的工程编译一下,出现如下错误:双击提示,在DialogBox()前加下划线,
多线程时用CreateDispatch函数创建组件对象是会出错
解决方法是在线程开始位置加上下面的代码:
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CoInitialize(NULL);
AfxEnableControlContaine
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CApplication app;
if (!app.CreateDispatch(_T("Excel.Application")))
{
theSampleSystem.PopMessageBox(TEXT("Create Excel Application Error!"));
return;
}
CWorkbook book;
CWorkbooks books = app.get_Workbooks();
LPDISPATCH lpDisp = books.Open(
lpszPathName
,covOptional,covOptional,covOptional
,covOptional,covOptional,covOptional
,covOptional,covOptional,covOptional
,covOptional,covOptional,covOptional
,covOptional,covOptional);
book.AttachDispatch(lpDisp);
CWorksheets sheets = book.get_Worksheets();
CWorksheet sheet = sheets.get_Item(COleVariant((short)1));
COleVariant vResult;
CRange range;
// 读取已经使用区域的信息,包括已经使用的行数、列数、起始行、起始列
range.AttachDispatch(sheet.get_UsedRange());
range.AttachDispatch(range.get_Rows());
// 取得已经使用的行数
long iRowNum = range.get_Count();
range.AttachDispatch(range.get_Columns());
// 取得已经使用的列数
long iColNum = range.get_Count();
// 取得已使用区域的起始行,从1开始
long iStartRow = range.get_Row();
// 取得已使用区域的起始列,从1开始
long iStartCol = range.get_Column();
RemoveAll();
int nColNumCount = iColNum - iStartCol + 1;
for(int i=iStartRow;i<=iRowNum;i++)
{
CBCGPGridRow * pRow = CreateRow(nColNumCount);
AddRow(pRow);
for(int j=iStartCol;j<=iColNum;j++)
{
range.AttachDispatch(sheet.get_Cells());
range.AttachDispatch(range.get_Item(COleVariant((long)i),COleVariant((long)j)).pdispVal);
vResult = range.get_Value2();
SYSTEMTIME st;
CString str, stry, strm, strd;
if(vResult.vt == VT_BSTR) //若是字符串
{
str = vResult.bstrVal;
}
else if (vResult.vt == VT_R8) //8字节的数字
{
str.Format(TEXT("%f"), vResult.dblVal);
}
else if(vResult.vt == VT_DATE) //时间格式
{
VariantTimeToSystemTime(vResult.date, &st);
stry.Format(TEXT("%d"),st.wYear);
strm.Format(TEXT("%d"),st.wMonth);
strd.Format(TEXT("%d"),st.wDay);
str = stry+L"-"+strm+L"-"+strd;
}
else if(vResult.vt == VT_EMPTY) //单元为空
{
str = TEXT("");
}
else if (vResult.vt ==VT_I4)
{
str.Format(TEXT("%ld"), (int)vResult.lVal);
}
CBCGPGridItem * pItem = pRow->GetItem(j-iStartCol);
pItem->SetValue((LPCTSTR)str);
pItem->SetReadOnly(TRUE);
pItem->AllowEdit(FALSE);
}
}
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.Quit();
app.ReleaseDispatch();
COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CApplication app;
if (!app.CreateDispatch(_T("Excel.Application")))
{
theSampleSystem.PopMessageBox(TEXT("Create Excel Application Error!"));
return;
}
CWorkbooks books = app.get_Workbooks();
CWorkbook book = books.Add(covOptional);
CWorksheets sheets = book.get_Worksheets();
CWorksheet sheet = sheets.get_Item(COleVariant((short)1));
int nRowCount = GetRowCount();
int nColCount = GetColumnCount();
for (int i=0; i<nRowCount; i++)
{
CBCGPGridRow * pRow = GetRow(i);
for (int j=0; j<nColCount; j++)
{
CBCGPGridItem * pItem = pRow->GetItem(j);
CString strCellName;
CDUFunction::GetCellName(i+1, j+1, strCellName);
CRange range = sheet.get_Range(COleVariant(strCellName), COleVariant(strCellName));
range.put_Value2(COleVariant(pItem->GetValue()));
}
}
book.SaveCopyAs(COleVariant(lpszPathName));
book.put_Saved(true);
book.ReleaseDispatch();
books.ReleaseDispatch();
app.Quit();
app.ReleaseDispatch();