VC生成word文档的部分代码

来源:互联网 发布:重复测量数据分析 编辑:程序博客网 时间:2024/04/28 04:30

 protected://变量

_Application  wordApp;
    Documents  wordDocs;
    _Document  wordDoc;
    Selection  wordSelection;
    Range   wordRange;
    Tables  wordTables;
    Table   wordTable;      
    Cell   wordCell;    
    Cells   wordCells;
    _Font   wordFont;

 

 

void CAUTOYAOCE::OnCreateReport()
{
// CurrentCount  MotherVoltageCount  CommonVoltageCount   PowerCount   PowerFactorCount    FrequencyCount 

// TODO:初始化WORD
 ::CoInitialize(NULL);  //初始化COM组件(必须添加,否则无法启动WORD)调用完毕后 CoUninitialize();

    if (!wordApp.CreateDispatch(_T("Word.Application")))
     AfxMessageBox("Word CreateDispatch Failed!");

       wordApp.SetVisible(TRUE);//make visible
       wordApp.Activate();//激活
       wordDocs=wordApp.GetDocuments();//get documents object
       //
       CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);
       wordDoc=wordDocs.Add(&tpl,&NewTemplate,&DocType,&Visble);
       wordSelection=wordApp.GetSelection();

       SYSTEMTIME sysTime;
       GetLocalTime(&sysTime);
       CString strReport;
       strReport.Format("                    日期: %d-%d-%d   时间: %d:%d:%d  /r/n  /r/n",sysTime.wYear,sysTime.wMonth,sysTime.wDay,sysTime.wHour,sysTime.wMinute,sysTime.wSecond);   
    wordSelection.TypeText(strReport);

    UpdateData();
    CString  m_PCT;
       m_PCT.Format("CT变比:%f",m_CT);
    wordSelection.TypeText(m_PCT+"    ");
       m_PCT.Format("PT变比:%f",m_PT);
    wordSelection.TypeText(m_PCT+"/r/n");

///////////////////////////////////////////////////////////////////////
//TODO:在WORD中插入表格

       if(CurrentCount)        CreateDoc("遥测电流幅值检验",CurrentBuf,CurrentCount,3);  //生成电流报表
       if(MotherVoltageCount)  CreateDoc("遥测电压母线幅值检验",MotherVoltageBuf,CurrentCount,3);  //生成母线电压报表
       if(CommonVoltageCount)  CreateDoc("遥测电压普线幅值检验",CommonVoltageBuf,CurrentCount,3);  //生成普线电压报表
       if(PowerCount)          CreateDoc("遥测功率测量检验",PowerBuf,CurrentCount,2);          //生成功率报表
    if(PowerFactorCount)    CreateDoc("遥测功率因数测量检验",PowerFactorBuf,CurrentCount,1); //生成功率因数报表
    if(FrequencyCount)      CreateDoc("遥测频率测量检验",FrequencyBuf,CurrentCount,1);     //生成频率报表

///////////////////////////////////////////////////////////////////////
//TODO:保存WORD
    CComVariant FileName,FileFormat(0),LockComments(false),Password(_T("")),AddToRecentFiles(true),WritePassword(_T(""));
    CComVariant ReadOnlyRecommended(false),EmbedTrueTypeFonts(false),SaveNativePictureFormat(false),SaveFormsData(false),SaveAsAOCELetter(false);
    CComVariant Encoding(false),InsertLineBreaks(false),AllowSubstitutions(false),LineEnding(false),AddBiDiMarks(false);

       strReport.Format("d://%d年%d月%d日%d时%d分%d秒 报告",sysTime.wYear,sysTime.wMonth,sysTime.wDay,sysTime.wHour,sysTime.wMinute,sysTime.wSecond);
       FileName=strReport;  //文件名

    wordDoc.SaveAs(&FileName,&FileFormat,&LockComments,&Password,&AddToRecentFiles,&WritePassword,
                &ReadOnlyRecommended,&EmbedTrueTypeFonts,&SaveNativePictureFormat,&SaveFormsData, &SaveAsAOCELetter,
                &Encoding,&InsertLineBreaks,&AllowSubstitutions,&LineEnding,&AddBiDiMarks);

       wordFont.ReleaseDispatch();wordCells.ReleaseDispatch();wordTable.ReleaseDispatch();wordTables.ReleaseDispatch();wordRange.ReleaseDispatch();
       wordSelection.ReleaseDispatch();wordDoc.ReleaseDispatch();wordDocs.ReleaseDispatch();wordApp.ReleaseDispatch();
 
    ::CoInitialize(NULL);

}

 

 

void CAUTOYAOCE::CreateCurrentDoc()
{
       wordSelection.TypeText("                     遥测电流幅值检验报表/r/n  /r/n");
       wordSelection.TypeText("                    日期: 2008-**-**时间: **:**/r/n  /r/n");

       wordTables=wordDoc.GetTables();
       wordRange=wordSelection.GetRange();

       CComVariant defaultBehavior(1),AutoFitBehavior(0);
       wordTable=wordTables.Add(wordRange,7,7,&defaultBehavior,&AutoFitBehavior);

///////////////////////////////////////////////////////////////////////

    UpdateData();
  
       wordCell=wordTable.Cell(1,1);
       wordCell.Select();
       wordSelection.MoveDown(&CComVariant(5),&CComVariant(1),&CComVariant(1));
       wordCells=wordSelection.GetCells();
       wordCells.Merge();
    wordCells.SetVerticalAlignment(1);
    wordSelection.TypeText("二次电流");

       wordCell=wordTable.Cell(1,2);
       wordCell.Select();
       wordSelection.MoveRight(&CComVariant(1),&CComVariant(2),&CComVariant(1));  //合并单元格时要使用&CComVariant(1)
       wordCells=wordSelection.GetCells();
       wordCells.Merge();
    wordSelection.TypeText("测控装置显示值");

       wordCell=wordTable.Cell(1,3);
       wordCell.Select();
       wordSelection.MoveRight(&CComVariant(1),&CComVariant(2),&CComVariant(1));
       wordCells=wordSelection.GetCells();
       wordCells.Merge();
    wordSelection.TypeText("操作员工作站显示值");

    wordSelection.MoveRight(&CComVariant(12),&CComVariant(2),&CComVariant(0));

       CString temp[6]={"Ia(A)","Ib(A)","Ic(A)","Ia(A)","Ib(A)","Ic(A)"};

       for(int i=0;i<6;i++)
    {
       wordSelection.TypeText(temp[i]);
       wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));  //单纯右移时使用&CComVariant(12)
    }

    CString str[21]={"1.0000","x","x","x","x","x","x","5.0000","xx","xx","xx","xx","xx","xx","基本误差","xxx","xxx","xxx","xxx","xxx","xxx"};


    for(i=0;i<21;i++)
    {
       wordSelection.TypeText(str[i]);    
       wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));
    }

    wordSelection.TypeText("结论");

       wordCell=wordTable.Cell(6,2);
       wordCell.Select();
       wordSelection.MoveRight(&CComVariant(1),&CComVariant(5),&CComVariant(1));
       wordCells=wordSelection.GetCells();
       wordCells.Merge();
    wordSelection.TypeText("合格");

    wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));
    wordSelection.TypeText("备注");

       wordCell=wordTable.Cell(7,2);
       wordCell.Select();
       wordSelection.MoveRight(&CComVariant(1),&CComVariant(5),&CComVariant(1));
       wordCells=wordSelection.GetCells();
       wordCells.Merge();
    wordSelection.TypeText("基本误差为各测点误差的最大值;基本误差绝对值应小于0.20%");

}

 

原创粉丝点击