描述:
请问:
用VC++控制EXCEL读写操作时,我要对单元格的字体、颜色、对齐方式等进行设置,怎么实现?
解决方案1:
我也刚用过这个东东,到处搜,下面是我到处搜刮整合的,可能不是很地道
_Application ExcelApp;
Workbooks workBooks;
_Workbook workBook;
Worksheets workSheets;
_Worksheet workSheet;
Range range;
COleException *e = new COleException;
try
{
if ( !ExcelApp.CreateDispatch("Excel.Application",e) )
{
// AfxMessageBox("创建Excel服务失败!");
// exit(1);
throw e;
}
}
catch ( COleDispatchException *e)
{
CString cStr;
if (!e->m_strSource.IsEmpty())
cStr = e->m_strSource + " - ";
if (!e->m_strDescription.IsEmpty())
cStr += e->m_strDescription;
else
cStr += "unknown error";
AfxMessageBox(cStr, MB_OK,
(e->m_strHelpFile.IsEmpty())? 0:e->m_dwHelpContext);
e->Delete();
}
catch (CException *e)
{
TRACE("%s(%d): OLE Execption caught: SCODE = %x",
__FILE__, __LINE__, COleException::Process(e));
e->Delete();
}
workBooks.AttachDispatch(ExcelApp.GetWorkbooks(),true);
workBook.AttachDispatch( workBooks.Add(
_variant_t(L"c:\\1.xls")));
workSheets.AttachDispatch( workBook.GetWorksheets());
workSheet.AttachDispatch( workSheets.GetItem(
_variant_t("sheet1")),true);
range.AttachDispatch( workSheet.GetCells(),true);
Range r1;
r1 = workSheet.GetRange(COleVariant("A1"),vtMissing);
r1.SetValue(COleVariant("hello"));
` workBook.Save();
ExcelApp.SetVisible(true);
workBook.Close(COleVariant((short)true),vtMissing,vtMissing);
ExcelApp.Quit();
range.ReleaseDispatch();
workSheet.ReleaseDispatch();
workSheets.ReleaseDispatch();
workBook.ReleaseDispatch();
workBooks.ReleaseDispatch();
}
ExcelApp.ReleaseDispatch();
我也正在學習研究﹐哎﹐假如是delphi就易如反掌﹐
解决方案3:我幫你up
解决方案4:mark
解决方案5: 利用Range对象获取单元格,利用Range对象的属性获取Style和Font对象来控制样式和字体.
具体实现查看Office的VBA帮助.