主题:vc控制excel
void write_to_excel(void)
{
_Application app;
Workbooks books;
_Workbook book;
Worksheets sheets;
_Worksheet sheet;
Range range;
Range iCell;
LPDISPATCH lpDisp;
COleVariant vResult;
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if(!AfxOleInit())
{
AfxMessageBox("无法初始化COM的动态连接库!");
return ;
}
//*****
//创建Excel 2000服务器(启动Excel)
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("无法启动Excel服务器!");
return;
}
app.SetVisible(TRUE); //使Excel可见
app.SetUserControl(TRUE); //允许其它用户控制Excel
books.AttachDispatch(app.GetWorkbooks());
lpDisp = books.Open("F:\\1.xls",
covOptional,covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional,covOptional,covOptional );
//*****
//得到Workbook
book.AttachDispatch(lpDisp);
sheets.AttachDispatch(book.GetWorksheets());
//得到当前活跃sheet
//如果有单元格正处于编辑状态中,此操作不能返回,会一直等待
lpDisp=book.GetActiveSheet();
sheet.AttachDispatch(lpDisp);
range.SetItem(COleVariant(long(1)),COleVariant(long(1)),COleVariant(("ASHHHHHHHHHH")));
Range usedRange;
usedRange.AttachDispatch(sheet.GetUsedRange());
range.AttachDispatch(usedRange.GetRows());
long iRowNum=range.GetCount(); //已经使用的行数
range.AttachDispatch(usedRange.GetColumns());
long iColNum=range.GetCount(); //已经使用的列数
// range.SetItem(COleVariant(long(1)),COleVariant(long(1)),COleVariant(_T("命令结构")));
// range.SetItem(_variant_t((short) 1),_variant_t((short) 1),_variant_t("HI,EXCEL!"));
//book.SaveAs(COleVariant("F:\\1.xls"),covOptional,covOptional,covOptional,covOptional,covOptional,0,covOptional,covOptional,covOptional,covOptional,covOptional);
Range unionRange;
range.AttachDispatch(sheet.GetCells());
unionRange.AttachDispatch(range.GetItem (COleVariant((long)1),COleVariant((long)1)).pdispVal );
vResult=unionRange.GetMergeCells();
if(vResult.boolVal==-1) //是合并的单元格
{
//合并单元格的行数
range.AttachDispatch (unionRange.GetRows ());
long iUnionRowNum=range.GetCount ();
//合并单元格的列数
range.AttachDispatch (unionRange.GetColumns ());
long iUnionColumnNum=range.GetCount ();
//合并区域的起始行,列
long iUnionStartRow=unionRange.GetRow(); //起始行,从1开始
long iUnionStartCol=unionRange.GetColumn(); //起始列,从1开始
}
else if(vResult.boolVal==0)
{//不是合并的单元格}
//将第一个单元格合并成2行,3列
range.AttachDispatch(sheet.GetCells());
unionRange.AttachDispatch(range.GetItem (COleVariant((long)3),COleVariant((long)1)).pdispVal );
unionRange.AttachDispatch(unionRange.GetResize(COleVariant((long)10),COleVariant((long)2)));
unionRange.Merge(COleVariant((long)0)); //合并单元格
}
//*****
//关闭所有的book,退出Excel
book.Close (covOptional,covOptional,covOptional);
books.Close();
app.Quit();
}
程序中的range.SetItem(COleVariant(long(1)),COleVariant(long(1)),COleVariant(("ASHHHHHHHHHH")));
语句不好用~~~
然后执行语句还总是出错~~~
高人帮忙解决下~~谢谢啊~~
{
_Application app;
Workbooks books;
_Workbook book;
Worksheets sheets;
_Worksheet sheet;
Range range;
Range iCell;
LPDISPATCH lpDisp;
COleVariant vResult;
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if(!AfxOleInit())
{
AfxMessageBox("无法初始化COM的动态连接库!");
return ;
}
//*****
//创建Excel 2000服务器(启动Excel)
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("无法启动Excel服务器!");
return;
}
app.SetVisible(TRUE); //使Excel可见
app.SetUserControl(TRUE); //允许其它用户控制Excel
books.AttachDispatch(app.GetWorkbooks());
lpDisp = books.Open("F:\\1.xls",
covOptional,covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional,covOptional,covOptional );
//*****
//得到Workbook
book.AttachDispatch(lpDisp);
sheets.AttachDispatch(book.GetWorksheets());
//得到当前活跃sheet
//如果有单元格正处于编辑状态中,此操作不能返回,会一直等待
lpDisp=book.GetActiveSheet();
sheet.AttachDispatch(lpDisp);
range.SetItem(COleVariant(long(1)),COleVariant(long(1)),COleVariant(("ASHHHHHHHHHH")));
Range usedRange;
usedRange.AttachDispatch(sheet.GetUsedRange());
range.AttachDispatch(usedRange.GetRows());
long iRowNum=range.GetCount(); //已经使用的行数
range.AttachDispatch(usedRange.GetColumns());
long iColNum=range.GetCount(); //已经使用的列数
// range.SetItem(COleVariant(long(1)),COleVariant(long(1)),COleVariant(_T("命令结构")));
// range.SetItem(_variant_t((short) 1),_variant_t((short) 1),_variant_t("HI,EXCEL!"));
//book.SaveAs(COleVariant("F:\\1.xls"),covOptional,covOptional,covOptional,covOptional,covOptional,0,covOptional,covOptional,covOptional,covOptional,covOptional);
Range unionRange;
range.AttachDispatch(sheet.GetCells());
unionRange.AttachDispatch(range.GetItem (COleVariant((long)1),COleVariant((long)1)).pdispVal );
vResult=unionRange.GetMergeCells();
if(vResult.boolVal==-1) //是合并的单元格
{
//合并单元格的行数
range.AttachDispatch (unionRange.GetRows ());
long iUnionRowNum=range.GetCount ();
//合并单元格的列数
range.AttachDispatch (unionRange.GetColumns ());
long iUnionColumnNum=range.GetCount ();
//合并区域的起始行,列
long iUnionStartRow=unionRange.GetRow(); //起始行,从1开始
long iUnionStartCol=unionRange.GetColumn(); //起始列,从1开始
}
else if(vResult.boolVal==0)
{//不是合并的单元格}
//将第一个单元格合并成2行,3列
range.AttachDispatch(sheet.GetCells());
unionRange.AttachDispatch(range.GetItem (COleVariant((long)3),COleVariant((long)1)).pdispVal );
unionRange.AttachDispatch(unionRange.GetResize(COleVariant((long)10),COleVariant((long)2)));
unionRange.Merge(COleVariant((long)0)); //合并单元格
}
//*****
//关闭所有的book,退出Excel
book.Close (covOptional,covOptional,covOptional);
books.Close();
app.Quit();
}
程序中的range.SetItem(COleVariant(long(1)),COleVariant(long(1)),COleVariant(("ASHHHHHHHHHH")));
语句不好用~~~
然后执行语句还总是出错~~~
高人帮忙解决下~~谢谢啊~~