请教vc操作xls文件保存时报错问题
源代码如下int CExcelCtrl::CreateXls(CString strFilePath,CString strSheetName)
{
if(strFilePath == "")
return 1;
else
m_strFilePath = strFilePath;
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
m_Books.AttachDispatch(m_XlsApp.GetWorkbooks()); // 得到Workbooks
m_Book = m_Books.Add(covOptional); // 得到Workbook
m_Sheets = m_Book.GetWorksheets(); // 得到Worksheets
if(strSheetName == "")
m_Sheet = m_Sheets.GetItem(COleVariant((short)1)); // 得到Worksheet
else
m_Sheet = m_Sheets.GetItem(COleVariant((short)1)); // 得到Worksheet
return 0;
}
int CExcelCtrl::SetHead(CString strTitle, CString strStart, CString strEnd)
{
// 思路:1.先获取A1:C1的Range范围,然后重新定义此范围,最后合并
// 2.直接获得A1:C2的Range范围,直接合并。结果和第一种方法一样
Range unionRange;
VARIANT vResult;
unionRange = m_Sheet.GetRange(COleVariant(_T(strStart)), COleVariant(_T(strEnd)));
unionRange.Merge(COleVariant((long)0)); //合并单元格
// Excel2000 用函数SetValue()即可, Excel2003用函数SetValue2()
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
//range.AttachDispatch(sheet.GetCells(),TRUE);
//unionRange = m_Sheet.GetRange(COleVariant("A1"), covOptional); // 获取A1 Range
unionRange.SetItem(COleVariant((long)1),COleVariant((long)1),COleVariant(_T(strTitle))); // 添加数据
// 设置标题的格式
Font ft; // 要插入excel类库里面的Font类,下面类似
ft.AttachDispatch(unionRange.GetFont());
ft.SetName(COleVariant(_T("华文行楷"))); // 字体
ft.SetSize(COleVariant((long)22)); // 字号
//ft.SetColor( COleVariant((long) RGB(255, 0, 0) ) ); //字色
ft.SetBold(COleVariant((long)0)); // 1:粗体,0:非粗体
unionRange.SetHorizontalAlignment(COleVariant((long)-4108)); // -4108:居中,-4131:靠左,-4152:靠右
unionRange.SetVerticalAlignment(COleVariant((long)-4108)); // -4108:居中,-4160:靠上,-4107:靠下
unionRange.ReleaseDispatch(); // 释放Range对象
return 0;
}
int CExcelCtrl::SaveXls()
{
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
m_Book.SaveAs(COleVariant(_T(m_strFilePath)),covOptional,covOptional,
covOptional,covOptional,covOptional,0,
covOptional,covOptional,covOptional,covOptional,covOptional);
//m_Book.Save();
return 0;
}
在运行到m_Book.SaveAs时报错,说路径错误,但是我输入的是一个正确的路径,请教到底怎么回事