我在网上照着人家做了MFC操作excel,现在的情况是,可以正确获取到单元格的内容,但是当我想修改指定单元格的内容时,那一段代码却无效。希望各位大神帮我看看,这是我照着人家看的文档,http://blog.csdn.net/shuilan0066/article/details/7936336。
附上我的源代码
UpdateData(TRUE);
if (m_path.GetLength()==0)
{
MessageBox("请先添加excel文件");
return;
}
m_Result="正在上传,请稍后....";
UpdateData(FALSE);
_Application app;
Workbooks books;
_Workbook book;
Worksheets sheets;
_Worksheet sheet;
Range range;
Range usedrange;
LPDISPATCH lpDisp; //接口指针
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if( !app.CreateDispatch("Excel.Application") ){
this->MessageBox("Failed to create excel Application!");
return ;
}
books=app.GetWorkbooks();
lpDisp = books.Open(m_path,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,covOptional,covOptional
);
book.AttachDispatch( lpDisp );
sheets=book.GetSheets();
sheet=sheets.GetItem(COleVariant((short)1));
//读取已经使用区域的信息,包括已经使用的行数、列数、起始行、起始列
usedrange.AttachDispatch(sheet.GetUsedRange());
range.AttachDispatch(usedrange.GetRows());
//取得已经使用的行数
long iRowNum = range.GetCount();
range.AttachDispatch(usedrange.GetColumns());
//取得已经使用的列数
long iColNum = range.GetCount();
//取得已使用区域的起始行,从1开始
long iStartRow = usedrange.GetRow();
//取得已使用区域的起始列,从1开始
long iStartCol = usedrange.GetColumn();
CString str_phone,str_iccid;
COleVariant vResult_phone,vResult_iccid;
char char_phone[32],char_iccid[32];
int pass_num=0,fail_num=0;
for (int j=iStartRow+1;j<=iRowNum;j++)
{
//读取单元格的值
range.AttachDispatch(sheet.GetCells());
range.AttachDispatch(range.GetItem (COleVariant((long)j),COleVariant((long)1)).pdispVal);
vResult_phone = range.GetValue(covOptional); //取出手机号
range.AttachDispatch(sheet.GetCells());
range.AttachDispatch(range.GetItem (COleVariant((long)j),COleVariant((long)4)).pdispVal);
vResult_iccid = range.GetValue(covOptional); //取出ICCID
if(vResult_phone.vt == VT_EMPTY ||vResult_iccid.vt==VT_EMPTY) //单元为空
{
str_phone="";
str_iccid="";
continue;
}else
{
vResult_phone.ChangeType(VT_BSTR);
str_phone=vResult_phone.bstrVal;
strcpy(char_phone,str_phone);
vResult_iccid.ChangeType(VT_BSTR);
str_iccid=vResult_iccid.bstrVal;
strcpy(char_iccid,str_iccid);
int istatus=insertdata(char_phone,char_iccid);
if (istatus>0)
{
pass_num++;
// range=sheet.GetRange(COleVariant("Z26"),COleVariant("Z26"));
// range.SetValue2(COleVariant("HELLO EXCEL!"));
// range.AttachDispatch(range.GetItem (COleVariant((long)j),COleVariant((long)26)).pdispVal );
//
// // range.SetValue2(COleVariant("上传成功"));
//
//
range=sheet.GetRange(COleVariant(IndexToString(j,26)),COleVariant(IndexToString(j,26)));
range.SetValue2(COleVariant("上传成功"));
//
//
// // range.SetItem(_variant_t((long)j), _variant_t((long)26), COleVariant("成功"));
// // char range_char[16];
// // sprintf(range_char,"Z%d",j);
//
// range = sheet.GetRange(COleVariant(range_char), covOptional); // 获取A1 Range
// range.SetValue2( COleVariant("上传成功"));
// lpDisp = sheets.GetItem(_variant_t((long)1));
// if (lpDisp)
// {
//
// sheet.AttachDispatch(lpDisp);
// range.AttachDispatch(sheet.GetCells());
// range.SetItem(_variant_t((long)j), _variant_t((long)26), COleVariant("上传成功"));
// }
}else
{
fail_num++;
}
}
}
//释放对象
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
//关闭工作薄对象,并释放对象(先关闭再释放)
book.SetSaved(TRUE);
books.Close();
book.ReleaseDispatch();
books.ReleaseDispatch();
//先退出Excel再释放,最后两句不能调换,如果先释放将调用不到quit()事件
app.Quit(); // 退出
app.ReleaseDispatch();
char message[512];
sprintf(message,"共上传%d条数据,其中成功%d条,失败%d条",pass_num+fail_num,pass_num,fail_num);
MessageBox(message,"提示");
m_Result="PASS,数据上传结束!";
UpdateData(FALSE);
------解决思路----------------------
void COptExcel::SetCellValue(int row, int col,int Align)
{
range=sheet.GetRange(COleVariant(IndexToString(row,col)),COleVariant(IndexToString(row,col)));
range.SetValue2(COleVariant(value));
cell.AttachDispatch((range.GetItem (COleVariant(long(1)), COleVariant(long(1)))).pdispVal);
cell.SetHorizontalAlignment(COleVariant((short)Align));
}
那个文档里 设置值的时候 range和cell是2个 Range 你for循环里面用的全是range 不知道是不是这个原因 话说你可以网上直接下载别人封装好的类来用不就行了?
------解决思路----------------------
百度 mfc操作excel封装类