vc在word中插入图表

来源:互联网 发布:禁止手机上网软件 编辑:程序博客网 时间:2024/05/18 13:48

关于在word中生成图表的说明:在给word中插入图表的时候,可以插入graph图表,和excel图表,可以用word中带的可以用Shapes 或者InlineShapesAddOLEObject()方法,如:AddOLEObject(COleVariant("Excel.Chart.8"), vOpt1, vFalse1, vFalse1, vOpt1, vOpt1, vOpt1, COleVariant((long)200), COleVariant((long)200), COleVariant((long)200), COleVariant((long)200), vOpt1);但是,但是插入后的图表不能去改变图表中的数据,从而使的图表改变,根据实验后,发现在word中插入Graph图表,和Excel图表时,都会产生另外的进程,而这个进程就是控制图表中数据的变化的程序,从而,就可以在excel中插入图表,当然在excel中插入图表的时候,excel会提供插入图表数据的接口和生成图表格式的接口的,于是,可以先打开一个word然后,在用exce生成图表,然后将此图片复制到剪贴板,然后贴到word上。具体实现可以先将word的类型库导入到工程中,然后在导入excel的类型库,但是,在导入excel类型库的时候,一定要注意,由于excel类型库和word类型库有写类名是相同的,所以,可以将excel类型库中用到的类改变名称,我在用到的每个类后加"1",防止excel类型库中类和word类型库中类重复,具体实现如下:

void CWordDrawTestDlg::OnButton2() { COleVariant vTrue((short)TRUE), vFalse((short)FALSE),  vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR),  covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);  try {  CoInitialize(NULL);  //操作word  _Application App;//定义Word提供的应用程序对象;  Documents Docs;//定义Word提供的文档对象;  Selection Sel;//定义Word提供的选择对象;  Docs.ReleaseDispatch();  Sel.ReleaseDispatch();  App.m_bAutoRelease=true;    if(!App.CreateDispatch("Word.Application"))  {    AfxMessageBox("创建Word2003服务失败!");    exit(1);   }  App.SetVisible(TRUE);  //下面是定义VARIANT变量;  COleVariant varFilePath("D://123.doc");  COleVariant varstrNull("");  COleVariant varZero((short)0);  COleVariant varTrue(short(1),VT_BOOL);  COleVariant varFalse(short(0),VT_BOOL);  Docs.AttachDispatch(App.GetDocuments());//将Documents类对象m_Docs和Idispatch接口关联起来;  Docs.Open(varFilePath,varFalse,varFalse,varFalse,   varstrNull,varstrNull,varFalse,varstrNull,   varstrNull,varTrue,varTrue,varTrue,vOpt,vOpt,vOpt,vOpt);   //打开Word文档;   Sel.AttachDispatch(App.GetSelection());//将Selection类对象m_Sel和Idispatc    //_Document oActiveDoc;   //oActiveDoc = App.GetActiveDocument();    //操作excel  _Application1 app;  // app is the Excel _Application object.  _Workbook1 book;  _Worksheet1 sheet;  _Chart1 chart;    Workbooks1 books;  Worksheets1 sheets;  Range1 range;  ChartObjects1 chartobjects;  LPDISPATCH lpDisp;    // Start Excel and get the Application object.  if(!app.CreateDispatch("Excel.Application"))  {   AfxMessageBox("Couldn't start Excel 2003 and get an application 0bject");   return;  }    app.SetVisible(TRUE);    // Get Workbooks collection.  lpDisp = app.GetWorkbooks();  // Get an IDispatch pointer.  ASSERT(lpDisp);  books.AttachDispatch( lpDisp );  // Attach the IDispatch pointer  // to the books object.  // Open a workbook.  lpDisp = books.Open("C://Test",   covOptional, covOptional, covOptional, covOptional,   covOptional, covOptional, covOptional, covOptional,   covOptional, covOptional, covOptional, covOptional,   covOptional, covOptional); // Excel 2000 requires only 13 arguments  ASSERT(lpDisp);  // It should have worked.    // Attach to a Workbook object.  book.AttachDispatch( lpDisp );  // Attach the IDispatch pointer  // to the Workbook object.    // Get sheets.  lpDisp = book.GetSheets();  ASSERT(lpDisp);  sheets.AttachDispatch(lpDisp);    lpDisp = sheets.GetItem( COleVariant((short)(1)) );  ASSERT(lpDisp);    // Attach the lpDisp pointer to a Worksheet object.  sheet.AttachDispatch(lpDisp);    lpDisp = sheet.GetRange(COleVariant("A1"), COleVariant("W40"));//得到A1到W40中的区域  // The range is from A1 to W40.  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  // Attach the IDispatch pointer  // to the range object.  range.Clear();  // Could be ClearContents().    //::Sleep(500); // So you can see it happen.      lpDisp = sheet.GetRange(COleVariant("A3"), COleVariant("A3"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  // Attach the IDispatch pointer  range.SetValue2(COleVariant("March")); // Insert March into range.      // Following is a series of repetitive steps to populate the  // worksheet's cells with a series of Months and values to be  // used in the Chart object, which is yet to be constructed.  lpDisp = sheet.GetRange(COleVariant("B2"), COleVariant("B2"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("shenghuo"));    lpDisp = sheet.GetRange(COleVariant("C2"), COleVariant("C2"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("mingtian"));    lpDisp = sheet.GetRange(COleVariant("B3"), COleVariant("B3"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("12")); //  Value for March.    lpDisp = sheet.GetRange(COleVariant("C3"), COleVariant("C3"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("20"));    lpDisp = sheet.GetRange(COleVariant("A4"), COleVariant("A4"));  // Months will be in column A, values in column B.  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("April")); // Excel 2002    lpDisp = sheet.GetRange(COleVariant("B4"), COleVariant("B4"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("8")); // Excel 2002    lpDisp = sheet.GetRange(COleVariant("C4"), COleVariant("C4"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("10"));    lpDisp = sheet.GetRange(COleVariant("A5"), COleVariant("A5"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("May"));    lpDisp = sheet.GetRange(COleVariant("B5"), COleVariant("B5"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("2"));    lpDisp = sheet.GetRange(COleVariant("C5"), COleVariant("C5"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("20"));    lpDisp = sheet.GetRange(COleVariant("A6"), COleVariant("A6"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("June"));    lpDisp = sheet.GetRange(COleVariant("B6"), COleVariant("B6"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("11"));    lpDisp = sheet.GetRange(COleVariant("C6"), COleVariant("C6"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("11"));    lpDisp = sheet.GetRange(COleVariant("A7"), COleVariant("A7"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("July"));    lpDisp = sheet.GetRange(COleVariant("B7"), COleVariant("B7"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("16"));    lpDisp = sheet.GetRange(COleVariant("C7"), COleVariant("C7"));  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);  range.SetValue2(COleVariant("18"));    // The cells are populated. To start the chart,  // declare some long variables and site the chart.  long left, top, width, height;  left = 100;  top = 10;  width = 350;  height = 250;    lpDisp = sheet.ChartObjects(covOptional);    ASSERT(lpDisp);  chartobjects.AttachDispatch(lpDisp); // Attach the lpDisp pointer  // for ChartObjects to the chartobjects  // object.  ChartObject1 chartobject = chartobjects.Add(left, top, width, height);     chart.AttachDispatch(chartobject.GetChart()); // GetChart() returns  // LPDISPATCH, and this attaches   // it to your chart object.    lpDisp = sheet.GetRange(COleVariant("A2"), COleVariant("C7"));  // The range containing the data to be charted.  ASSERT(lpDisp);  range.AttachDispatch(lpDisp);    VARIANT var; // ChartWizard needs a Variant for the Source range.  var.vt = VT_DISPATCH;  var.pdispVal = lpDisp; // Assign IDispatch pointer  // of the Source range to var.    /*  xlArea   xlBar   xlColumn   xlLine  xlPie  xlRadar   xlXYScatter     xlCombination   xl3DArea      xl3DBar    xl3DColumn      xl3DLine     xl3Dpie       xl3DSurface      xlDoughnut        xlDefaultAutoFormat  */    chart.ChartWizard(var,                    // Source.   COleVariant((short)11),  // Gallery: 3d Column.  共15种   covOptional,             // Format, use default.   COleVariant((short)1),   // PlotBy: xlRows.//指定图表数字来自行还是列,有1为行,2为列   COleVariant((short)1),   // CategoryLabels.//包还分类标签中的整数,从0到最大   COleVariant((short)1),   // SeriesLabels.  //包还分类标签中的整数,从零到最大   COleVariant((short)TRUE),  // HasLegend.   COleVariant("Use by Month"),  // Title.   COleVariant("Month"),    // CategoryTitle.   COleVariant("Usage in Thousands"),  // ValueTitles.   covOptional              // ExtraTitle.   );    ::Sleep(3000);  ChartArea1 area;  area = chart.GetChartArea();  area.Copy();  Sel.TypeParagraph();  Sel.TypeText("我的生活是多么的饿么妈妈么妈妈妈妈 地方饿发饿的大饿饭发饿发饿饭饿士大夫地方的反对反对发地方的反对反对发地方的发的反对发的发的反对发大幅度法地方的发地方的发的反对发的反对发地方的的地方地方 你们");  Sel.TypeParagraph();  Sel.PasteAndFormat((long)2); //2为粘贴excel图表为图片  //Sel.Paste();  chartobject.Delete();  // Removes the first chartobject, sets the    range.Clear();  // Erase the usage data.    book.SetSaved(true); // Avoids the 'Save changes?' dialog box.  app.Quit(); // Excel departs.  CoUninitialize();        }          catch(COleException *e)      {    char buf[1024];        sprintf(buf, "COleException. SCODE: %08lx.", (long)e->m_sc);    ::MessageBox(NULL, buf, "COleException", MB_SETFOREGROUND | MB_OK);      }         catch(COleDispatchException *e)      {    char buf[1024];    sprintf(buf,     "COleDispatchException. SCODE: %08lx, Description: /"%s/".",     (long)e->m_wCode,     (LPSTR)e->m_strDescription.GetBuffer(1024));    ::MessageBox(NULL, buf, "COleDispatchException",     MB_SETFOREGROUND | MB_OK);      }         catch(...)      {    ::MessageBox(NULL, "General Exception caught.", "Catch-All",     MB_SETFOREGROUND | MB_OK);      }  } 

原创粉丝点击