3、适合用于将界面上的内容所见及所得的输出,可以设置单元格的的合并,大小,字体,颜色等等;
[cpp]
void CFileRecord::OutPutToExcelForMFD(GROUP* pGroup)
{
// TODO: Add extra validation here
//用m_ExlApp对象创建Excel2003进程
if(!m_ExlApp.CreateDispatch("Excel.Application",NULL))
{
AfxMessageBox("创建Excel服务失败!");
return;
}
//设置为可见
m_ExlApp.SetVisible(TRUE);
///下面得到应用程序所在的路径///
CString theAppPath,sPath, strSheet;
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer ();
int nPos;
nPos=sPath.ReverseFind ('\\');
sPath=sPath.Left (nPos);
theAppPath = sPath + "\\System\\";
CString TempPath="";
TempPath=theAppPath+"Template.xls";//EXCEL模板的路径
m_ExlBooks.AttachDispatch(m_ExlApp.GetWorkbooks(),TRUE);
m_ExlBook.AttachDispatch(m_ExlBooks.Add((_variant_t)TempPath),TRUE);//加载EXCEL模板
m_ExlSheets.AttachDispatch(m_ExlBook.GetSheets(),TRUE);//加载Sheet页面
//添加新的Sheet页面
m_ExlSheets.Add(vtMissing,vtMissing,_variant_t((long)1),vtMissing);
//删除第二个Sheet页面
m_ExlSheet.AttachDispatch(m_ExlSheets.GetItem(_variant_t((long)2)),TRUE);
m_ExlSheet.Delete();
//把第一个Sheet页面的名字改变为TestSheet
m_ExlSheet.AttachDispatch(m_ExlSheets.GetItem(_variant_t((long)1)),TRUE);
strSheet.LoadString(IDS_CHUFACTORYSETTING);
m_ExlSheet.SetName(strSheet);
///合并第一行单元格A1至D1//
//加载要合并的单元格
m_ExlRge.AttachDispatch(m_ExlSheet.GetRange(_variant_t("A1"),_variant_t("H2")),TRUE);
m_ExlRge.Merge(_variant_t((long)0));
设置表格内容
m_ExlRge.AttachDispatch(m_ExlSheet.GetCells(),TRUE);//加载所有单元格
m_ExlRge.SetItem(_variant_t((long)1),_variant_t((long)1),_variant_t(strSheet));
DBASE *pDBase = pGroup->DBase;
if(pDBase == NULL)
{
return;
}
int nConfirmNum = pDBase->GetCardCount(); //设备拥有的槽数
if (nConfirmNum == 0)
{
return;
}
int nMaxChuCount = 0; //设备单盘拥有的最大话路数
CString strChuType = "-";
CString strChuStatus = "-";
CString str,strCtrl;
int iTempCount = 0;
for(int iSlot=1; iSlot <= nConfirmNum; iSlot++)
{
str.LoadString(IDS_STRINGCHANNEL);
strCtrl.LoadString(IDS_SLOT);
str = strCtrl+ "|" + str;
m_ExlRge.SetItem(_variant_t((long)(3)),_variant_t((long)1),_variant_t(str));
str.Format("%d:",iSlot);
strCtrl.LoadString(IDS_SLOT);
str = strCtrl+str;
m_ExlRge.SetItem(_variant_t((long)(3+iSlot)),_variant_t((long)1),_variant_t(str));
CHHCardBase *pCardBaseCtrl = dynamic_cast( pDBase->GetSuperCardBase(iSlot));
if (pCardBaseCtrl != NULL)
{
int iSlotCtrl = 0;
if (pGroup->GetExtModelID() == MFA) //MFA特殊处理
{
switch(iSlot)
{
case 1: iSlotCtrl = 1;
break;
case 2: iSlotCtrl = 2;
break;
case 3: iSlotCtrl = 3;
break;
case 4: iSlotCtrl = 4;
break;
case 5: iSlotCtrl = 24;
break;
case 6: iSlotCtrl = 23;
break;
case 7: iSlotCtrl = 22;
break;
case 8: iSlotCtrl = 21;
break;
case 9: iSlotCtrl = 14;
break;
case 10:iSlotCtrl = 13;
break;
case 11:iSlotCtrl = 12;
break;
case 12:iSlotCtrl = 11;
break;
case 13:iSlotCtrl = 13;
break;
case 14:iSlotCtrl = 14;
break;
}
}
else
{
switch(iSlot)
{
case 1: iSlotCtrl = 1;
break;
case 2: iSlotCtrl = 2;
break;
case 3: iSlotCtrl = 3;
break;
case 4: iSlotCtrl = 24;
break;
case 5: iSlotCtrl = 23;
break;
case 6: iSlotCtrl = 22;
break;
case 7: iSlotCtrl = 21;
break;
case 8: iSlotCtrl = 14;
break;
case 9: iSlotCtrl = 13;
break;
case 10:iSlotCtrl = 12;
break;
case 11:iSlotCtrl = 11;
break;
case 12:iSlotCtrl = 12;
break;
case 13:iSlotCtrl = 13;
break;
case 14:iSlotCtrl = 14;
break;
}
}
strCtrl = pCardBaseCtrl->GetCardTypeName();
str.Format("%d:%s",iSlotCtrl,strCtrl);
strCtrl.LoadString(IDS_SLOT);
str = strCtrl+str;
m_ExlRge.SetItem(_variant_t((long)(3+iSlot)),_variant_t((long)1),_variant_t(str));
}
}
CHHCardBase *pCardBase = dynamic_cast( pDBase->GetSuperCardBase(ucSystemCardSlot)); //系统盘
if (pCardBase != NULL)
{
int iChuCount = pCardBase->GetSpecifyPortCount(Port_CHU);
for (int iChuNum = 0 ; iChuNum < iChuCount ; iChuNum++)
{
int iChuType = 0xff;
int iChuStatus = 0xff;
CHHPort* pChuPort = pCardBase->GetPortByType( iChuNum, Port_CHU );
if (pChuPort != NULL)
{
int iChuNumCtrl =0;
int iSlotCtrl = 0;
if (iChuNum < 30)
{
iChuNumCtrl = iChuNum%8;
iSlotCtrl = 10- iChuNum/8;
}
else
{
iChuNumCtrl = (iChuNum+2)%8;
iSlotCtrl = 10 - (iChuNum+2)/8 ;
}
if(!pChuPort->GetPortIsUsedForChu())
{
continue;
}
CHHChuIfModule *pModule = dynamic_cast(pChuPort->GetModule(Mod_ChuIf));
if(pModule == NULL)
{
continue;
}
iChuType = pModule->GetChuType(TRUE);
str.Format("%d", iChuNumCtrl+1);
str = "CH"+ str;
m_ExlRge.SetItem(_variant_t((long)3),_variant_t((long)(2+iChuNumCtrl)),_variant_t(str));
str = GetChuTypeString(iChuType);
if (pGroup->GetExtModelID() == MFA) //MFA特殊处理
{
m_ExlRge.SetItem(_variant_t((long)(5+iSlotCtrl)),_variant_t((long)(2+iChuNumCtrl)),_variant_t(str));
}
else
{
m_ExlRge.SetItem(_variant_t((long)(4+iSlotCtrl)),_variant_t((long)(2+iChuNumCtrl)),_variant_t(str));
}
if (iChuNumCtrl >= nMaxChuCount)
{
nMaxChuCount = iChuNumCtrl +1;
}
}
}
}
//加载要合并的单元格
switch (nMaxChuCount)
{
case 8:
m_ExlRge.AttachDispatch(m_ExlSheet.GetRange(_variant_t("A1"),_variant_t("I2")),TRUE);
m_ExlRge.Merge(_variant_t((long)0));
break;
case 10:
m_ExlRge.AttachDispatch(m_ExlSheet.GetRange(_variant_t("A1"),_variant_t("K2")),TRUE);
m_ExlRge.Merge(_variant_t((long)0));
break;
case 16:
m_ExlRge.AttachDispatch(m_ExlSheet.GetRange(_variant_t("A1"),_variant_t("Q2")),TRUE);
m_ExlRge.Merge(_variant_t((long)0));
break;
}
设置表格内容
m_ExlRge.AttachDispatch(m_ExlSheet.GetUsedRange());//加载已使用的单元格
m_ExlRge.SetWrapText(_variant_t((long)1));//设置单元格内的文本为自动换行
//设置齐方式为水平垂直居中
//水平对齐:默认=1,居中=-4108,左=-4131,右=-4152
//垂直对齐:默认=2,居中=-4108,左=-4160,右=-4107
m_ExlRge.SetHorizontalAlignment(_variant_t((long)-4108));
m_ExlRge.SetVerticalAlignment(_variant_t((long)-4108));
///设置整体的字体、字号及颜色//
Font ft;
ft.AttachDispatch(m_ExlRge.GetFont());
ft.SetName(_variant_t("宋体"));//字体
ft.SetColorIndex(_variant_t((long)1));//字的颜色
ft.SetSize(_variant_t((long)10));//字号
///设置标题字体及颜色//
m_ExlRge.AttachDispatch(m_ExlSheet.GetRange(_variant_t("A1"),_variant_t("H2")));
ft.AttachDispatch(m_ExlRge.GetFont());
ft.SetBold(_variant_t((long)1));//粗体
ft.SetSize(_variant_t((long)13));
ft.SetColorIndex(_variant_t((long)1));
CellFormat cf;
cf.AttachDispatch(m_ExlRge.GetCells());
// //设置底色/
//
// Interior it;
//
// it.AttachDispatch(m_ExlRge.GetInterior());
//
// it.SetColorIndex(_variant_t((long)11));//标题底色
//
// 表格内容的底色
//
// m_ExlRge.AttachDispatch(m_ExlSheet.GetRange(_variant_t("A2"),_variant_t("D5")));
//
// it.AttachDispatch(m_ExlRge.GetInterior());
//
// it.SetColorIndex(_variant_t((long)15));
//为表格设置边框/
Range UnitRge;
CString CellName;
for(int i=1;i<=nConfirmNum+3;i++)
{
for(int j=1;j<=nMaxChuCount+1;j++)
{
CellName.Format("%c%d",j+64,i);//单元格的名称
UnitRge.AttachDispatch(m_ExlRge.GetRange(_variant_t(CellName),_variant_t(CellName)));//加载单元格
//LineStyle=线型 Weight=线宽 ColorIndex=线的颜色(-4105为自动)
UnitRge.BorderAround(_variant_t((long)1),_variant_t((long)2),_variant_t((long)-4105),vtMissing);//设置边框
}
}
//释放对象(相当重要!)
m_ExlRge.ReleaseDispatch();
m_ExlSheet.ReleaseDispatch();
m_ExlSheets.ReleaseDispatch();
m_ExlBook.ReleaseDispatch();
m_ExlBooks.ReleaseDispatch();
//m_ExlApp一定要释放,否则程序结束后还会有一个Excel进程驻留在内存中,而且程序重复运行的时候会出错
m_ExlApp.ReleaseDispatch();
//退出程序
m_ExlApp.Quit();
// CDialog::OnOK();
}
3、excel 导出。适合用于将界面上的内容所见及所得的输出,可以设置单元格的的合并,大小,字体,颜色等等
最新推荐文章于 2024-04-22 10:48:22 发布