我相信有兩種方法可以解決你的問題
1)首先將情節圖表動態。那是當你的數據集,你可以 在運行時創建圖表和分配您Datarange那是你的圖表數據源
一個簡單的例子如下:
Excel.Application XlApp = null;
Excel.Workbook workbook = null;
Excel.Worksheet Ws = null;
Excel.Range Range1 = null;
Excel.RangeDataRange = null;
XlApp = new Excel.Application();
XlApp.Visible = true;
workbook = XlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Ws = (Excel.Worksheet)workbook.Worksheets[1];//Lets say you have your graph at 1 sheet
XlApp.WindowState = XlWindowState.xlMaximized;
Ws.Cells.Clear();//Clear your sheet of any existing data
ChartObjs = null;
ChartObj = null;
xlChart = null;
Range1 = Ws.get_Range(Ws.Cells[39, 6], Ws.Cells[48, 10]);
ChartObjects WsChartObjs = (ChartObjects)Ws.ChartObjects(Type.Missing);
ChartObj = WsChartObjs.Add((double)Range1.Left + 115, (double)Range1.Top - 1, (double)Range1.Width - 25, (double)Range1.Height + 5);
xlChart = ChartObj.Chart;
DataRange = null;
if (MyDataset.Tables[0].Rows.Count > 10)
{
DataRange = Ws.get_Range(Ws.Cells[12, 14], Ws.Cells[12 +
MyDataset.Tables[0].Rows.Count, 15]);//or whatever your range may be
}
xlChart.SetSourceData(DataRange, System.Type.Missing);
xlChart.ChartType = XlChartType.xlBarClustered;
xlChart.ChartArea.Fill.Visible =
Microsoft.Office.Core.MsoTriState.msoFalse;
xlChart.ChartArea.Border.Color =
System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(255, 255, 255));
xlChart.PlotArea.Interior.Color =
System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(255, 255, 255));
xlChart.PlotArea.Border.Color = System.Drawing.ColorTranslator.ToOle(255, 255, 255));
這是第apporach
2)第二種方法是,在你在Excel中選擇現有圖表,並改變其數據源
簡單的例子是
Excel.ChartObject MyChartObject = (Excel.ChartObject)Ws.ChartObjects(1);//You can run
//macro and get your chart number and pass that instead of "1"
MyChartObject .Activate();
xlChart = MyChartObject .Chart;
xlChart.SetSourceData(DataRange, System.Type.Missing);
我想這就是所有
希望這幫助
請按照以下步驟'1'查找報告'2'的最後一行和最後一列'從該信息'3'構建您的數據範圍更改酸圖表的cedata。你可能想嘗試一下,併發布你試過的代碼,如果你被卡住了,我們會從那裏拿走它? – 2013-04-06 08:30:04
或者如果你想在VB.Net準備好的例子,那麼我可以指導你一個鏈接,然後你可以修改它們? – 2013-04-06 08:34:12
請提供鏈接。 – RKh 2013-04-06 08:47:17