如下代码只能在新生成的worksheet中添加图表
    _Application ExcelApp; 
Workbooks    books; 
_Workbook    book; 
Worksheets   sheets; 
_Worksheet   sheet; 

COleVariant covTrue((short)TRUE),covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); if (!ExcelApp.CreateDispatch("Excel.Application",NULL)) 

AfxMessageBox("创建Excel服务失败!"); 
exit(1); 

    
ExcelApp.SetVisible(TRUE);
char sFileName[256];
::GetModuleFileName(NULL, sFileName, 256);
CString sFilePath;
sFilePath = sFileName;
for (int j = sFilePath.GetLength()-1; j >= 0, sFilePath[j] != '\\';--j);
CString sPath(sFilePath.Left(j));
CString sTempleteName;
sTempleteName=sPath+"\\tubiao.xlt"; books = ExcelApp.GetWorkbooks();
book = books.Open(sTempleteName,covOptional,covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional,
covOptional, covOptional,covOptional,covOptional,covOptional,covOptional); sheets = book.GetWorksheets();
sheet  = sheets.GetItem(COleVariant((short)1)); Charts charts = book.GetCharts();
sheet.SetVisible(TRUE);
    
Range range = sheet.GetCells();
range.Clear();
    
range = sheet.GetRange(COleVariant("A1"), covOptional);
range.SetValue2(COleVariant("月份"));
    
range = sheet.GetRange(COleVariant("B1"), covOptional);
    range.SetValue2(COleVariant("股票涨跌幅")); CString tmp,rg; for(int i = 1; i <= 12; i++)

tmp.Format("%d月份", i);
rg.Format("A%d:A%d", i+1, i+1);
range = sheet.GetRange(COleVariant(rg), covOptional);
range.SetValue2(COleVariant(tmp));
} range = sheet.GetRange(COleVariant("B2:b13"), covOptional); 
range.SetFormula(COleVariant("=RAND()*10000"));  _Chart Chart = charts.Add(vtMissing, vtMissing, COleVariant((short)1));
    Chart.SetChartType((long)51); 
range        = sheet.GetRange(COleVariant("A1:B13"), covOptional);
Chart.SetSourceData(range, COleVariant((short)2));
    
Chart.SetHasTitle(TRUE);   
ChartTitle ChartTtl = Chart.GetChartTitle();   
ChartTtl.SetText("<-股票涨跌幅分月统计图->");   
ChartTtl.SetShadow(TRUE);   
    
Chart.SetHasLegend(FALSE); 
Series oSeries = Chart.SeriesCollection(COleVariant((short)1));   
sheet.SetVisible(TRUE); 现在我需要在一个worksheet中根据已有数据插入图表,我想用函数LPDISPATCH _Chart::Location(long Where, const VARIANT& Name),但是我不知道怎么设置参数Where,请高手赐教!!!!!!!!

解决方案 »

  1.   

    先在excel中录制宏,录制手工插图的过程,然后看VBA怎么写的,照着写成++
      

  2.   

    问题是long Where要怎么填啊
      

  3.   

    Excel Developer Reference 
    Chart.Location Method 
    Moves the chart to a new location.
    Syntaxexpression.Location(Where, Name)expression   An expression that returns a Chart object.ParametersWhere XlChartLocation Where to move the chart. 
    Name Variant Required if Where is xlLocationAsObject. The name of the sheet where the chart will be embedded if Where is xlLocationAsObject or the name of the new sheet if Where is xlLocationAsNewSheet.
     
    Return Value
    ChartExample
    This example moves the embedded chart to a new chart sheet named "Monthly Sales."Visual Basic for Applications 
    Worksheets(1).ChartObjects(1).Chart _
        .Location xlLocationAsNewSheet, "Monthly Sales"
     XlChartLocation EnumerationName                   Value                   Description 
    xlLocationAsNewSheet     1          Chart is moved to a new sheet. 
    xlLocationAsObject       2          Chart is to be embedded in an existing sheet. 
    xlLocationAutomatic      3          Excel controls chart location. 
      

  4.   

    http://blog.csdn.net/bwmwm/archive/2008/11/01/3200553.aspx有说明,看看
      

  5.   

    http://support.microsoft.com/kb/178783/en-us