您的位置:首页 > 其它

VC操作Excel写入数据源码

2011-07-22 09:39 489 查看
void ControlExcel() 
{
	// TODO: Add your command handler code here
	COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR), vtTrue((short)TRUE), vtFalse((short)FALSE);
	_Application *ExcelApp = new _Application;

	ExcelApp->CreateDispatch("excel.application");

	//设置为显示
	ExcelApp->SetVisible(TRUE);
	//得到WorkBooks
	Workbooks  ExcelBook=ExcelApp->GetWorkbooks();
	Sheets     ExcelSheet=ExcelBook.Add(vtOptional);
	_Workbook  workBook;
	_Worksheet workSheet;
	Range    range;
	workBook.AttachDispatch(ExcelApp->GetApplication());
	ExcelSheet=workBook.GetSheets();

	////////////////////////////////////////////
	workSheet=ExcelSheet.GetItem(COleVariant((short)1));
	workSheet.Activate();

	Range myallrange=workSheet.GetRange(COleVariant("A1"),COleVariant("D1"));
	//myallrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
	myallrange.SetHorizontalAlignment(COleVariant((short)3));
	myallrange.Merge(COleVariant((short)1));
	myallrange.SetValue(COleVariant("个人信息表"));

	Range myrange=workSheet.GetRange(COleVariant("A2"),COleVariant("A2"));
	myrange.SetValue(COleVariant("姓名"));
	myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
	myrange.SetHorizontalAlignment(COleVariant((short)3));
	myrange.SetVerticalAlignment(COleVariant((short)2));
	myrange.SetColumnWidth(COleVariant((short)15));

	myrange=workSheet.GetRange(COleVariant("B2"),COleVariant("B2"));
	myrange.SetValue(COleVariant("性别"));
	myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
	myrange.SetHorizontalAlignment(COleVariant((short)3));
	myrange.SetVerticalAlignment(COleVariant((short)2));
	myrange.SetColumnWidth(COleVariant((short)15));

	myrange=workSheet.GetRange(COleVariant("C2"),COleVariant("C2"));
	myrange.SetValue(COleVariant("QQ"));
	myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
	myrange.SetHorizontalAlignment(COleVariant((short)3));
	myrange.SetVerticalAlignment(COleVariant((short)2));
	myrange.SetColumnWidth(COleVariant((short)15));

	myrange=workSheet.GetRange(COleVariant("D2"),COleVariant("D2"));
	myrange.SetValue(COleVariant("Email"));
	myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
	myrange.SetHorizontalAlignment(COleVariant((short)3));
	myrange.SetVerticalAlignment(COleVariant((short)2));
	myrange.SetColumnWidth(COleVariant((short)17));

	CString str;
	for(int i=3;i<=5;i++)
	{
		str.Format("A%d",i);
		Range myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
		myrange.SetValue(COleVariant("相生昌"));
		myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
		myrange.SetHorizontalAlignment(COleVariant((short)3));
		myrange.SetVerticalAlignment(COleVariant((short)2));

		str.Format("B%d",i);
		myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
		myrange.SetValue(COleVariant("男"));
		myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
		myrange.SetHorizontalAlignment(COleVariant((short)3));
		myrange.SetVerticalAlignment(COleVariant((short)2));

		str.Format("C%d",i);
		myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
		myrange.SetValue(COleVariant("16432884"));
		myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
		myrange.SetHorizontalAlignment(COleVariant((short)3));
		myrange.SetVerticalAlignment(COleVariant((short)2));

		str.Format("D%d",i);
		myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
		myrange.SetValue(COleVariant("superxsc@126.com"));
		myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
		myrange.SetHorizontalAlignment(COleVariant((short)3));
		myrange.SetVerticalAlignment(COleVariant((short)2));
	}
	
	ExcelApp->SetVisible(TRUE);
	delete ExcelApp;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: